webpack/examples/loader
Tobias Koppers a79fecfb6a Updated examples for 1.1.0 2014-03-25 10:44:10 +01:00
..
README.md Updated examples for 1.1.0 2014-03-25 10:44:10 +01:00
build.js better free var filling, node.js test 2012-04-05 14:59:01 +02:00
enhanced-require.config.js fixed example 2013-02-24 02:25:25 +01:00
example.js fixed example 2013-02-24 02:25:25 +01:00
file.js added examples 2012-03-27 00:00:51 +02:00
loader.js fixed loader example 2012-04-05 15:08:49 +02:00
template.md Added note about chat to README, fixes some missing stats in examples 2013-12-15 23:30:25 +01:00
test.json Normalize line endings #31 2012-11-05 09:05:39 +01:00
webpack.config.js fixed some examples stuff 2013-02-13 11:51:08 +01:00

README.md

example.js

// use our loader
console.dir(require("./loader!./file"));

// use buildin json loader
console.dir(require("./test.json")); // default by extension
console.dir(require("!json!./test.json")); // manual

file.js

exports.foo = "bar";

loader.js

module.exports = function(content) {
	return "exports.answer = 42;\n" + content;
}

test.json

{
	"foobar": 1234
}

js/output.js

/******/ (function(modules) { // webpackBootstrap
/******/ 	
/******/ 	// The module cache
/******/ 	var installedModules = {};
/******/ 	
/******/ 	// The require function
/******/ 	function __webpack_require__(moduleId) {
/******/ 		// Check if module is in cache
/******/ 		if(installedModules[moduleId])
/******/ 			return installedModules[moduleId].exports;
/******/ 		
/******/ 		// Create a new module (and put it into the cache)
/******/ 		var module = installedModules[moduleId] = {
/******/ 			exports: {},
/******/ 			id: moduleId,
/******/ 			loaded: false
/******/ 		};
/******/ 		
/******/ 		// Execute the module function
/******/ 		modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
/******/ 		
/******/ 		// Flag the module as loaded
/******/ 		module.loaded = true;
/******/ 		
/******/ 		// Return the exports of the module
/******/ 		return module.exports;
/******/ 	}
/******/ 	
/******/ 	
/******/ 	// expose the modules object (__webpack_modules__)
/******/ 	__webpack_require__.m = modules;
/******/ 	
/******/ 	// expose the module cache
/******/ 	__webpack_require__.c = installedModules;
/******/ 	
/******/ 	// __webpack_public_path__
/******/ 	__webpack_require__.p = "js/";
/******/ 	
/******/ 	
/******/ 	// Load entry module and return exports
/******/ 	return __webpack_require__(0);
/******/ })
/************************************************************************/
/******/ ([
/* 0 */
/*!********************!*\
  !*** ./example.js ***!
  \********************/
/***/ function(module, exports, __webpack_require__) {

	// use our loader
	console.dir(__webpack_require__(/*! ./loader!./file */ 2));

	// use buildin json loader
	console.dir(__webpack_require__(/*! ./test.json */ 1)); // default by extension
	console.dir(__webpack_require__(/*! json!./test.json */ 1)); // manual

/***/ },
/* 1 */
/*!*******************************************!*\
  !*** (webpack)/~/json-loader!./test.json ***!
  \*******************************************/
/***/ function(module, exports, __webpack_require__) {

	module.exports = {
		"foobar": 1234
	}

/***/ },
/* 2 */
/*!*****************************!*\
  !*** ./loader.js!./file.js ***!
  \*****************************/
/***/ function(module, exports, __webpack_require__) {

	exports.answer = 42;
	exports.foo = "bar";

/***/ }
/******/ ])

Console output

Prints in node.js (enhanced-require example.js) and in browser:

{ answer: 42, foo: 'bar' }
{ foobar: 1234 }
{ foobar: 1234 }

Info

Uncompressed

Hash: 547826c0f7b5903a42b6
Version: webpack 1.1.0
Time: 78ms
    Asset  Size  Chunks             Chunk Names
output.js  2360       0  [emitted]  main       
chunk    {0} output.js (main) 282 [rendered]
    > main [0] ./example.js 
    [0] ./example.js 205 {0} [built]
    [1] (webpack)/~/json-loader!./test.json 36 {0} [built]
        cjs require !json!./test.json [0] ./example.js 6:12-40
        cjs require ./test.json [0] ./example.js 5:12-34
    [2] ./loader.js!./file.js 41 {0} [not cacheable] [built]
        cjs require ./loader!./file [0] ./example.js 2:12-38

Minimized (uglify-js, no zip)

Hash: 2c75b96c30dd80131187
Version: webpack 1.1.0
Time: 144ms
    Asset  Size  Chunks             Chunk Names
output.js   352       0  [emitted]  main       
chunk    {0} output.js (main) 282 [rendered]
    > main [0] ./example.js 
    [0] ./example.js 205 {0} [built]
    [1] (webpack)/~/json-loader!./test.json 36 {0} [built]
        cjs require !json!./test.json [0] ./example.js 6:12-40
        cjs require ./test.json [0] ./example.js 5:12-34
    [2] ./loader.js!./file.js 41 {0} [not cacheable] [built]
        cjs require ./loader!./file [0] ./example.js 2:12-38