webpack/examples/loader/README.md

168 lines
3.9 KiB
Markdown
Raw Normal View History

2012-03-27 06:00:51 +08:00
# example.js
``` javascript
// Polyfill require for node.js usage of loaders
2012-10-25 01:28:17 +08:00
require = require("enhanced-require")(module);
2012-03-27 06:00:51 +08:00
// use our loader
console.dir(require("./loader!./file"));
// use buildin json loader
console.dir(require("./test.json")); // default by extension
2013-02-13 18:58:13 +08:00
console.dir(require("!json!./test.json")); // manual
2012-03-27 06:00:51 +08:00
```
# file.js
``` javascript
exports.foo = "bar";
```
# loader.js
``` javascript
2012-04-05 21:08:49 +08:00
module.exports = function(content) {
return "exports.answer = 42;\n" + content;
2012-03-27 06:00:51 +08:00
}
```
# test.json
``` javascript
{
"foobar": 1234
}
```
# js/output.js
``` javascript
2013-01-31 01:49:25 +08:00
/******/ (function webpackBootstrap(modules) {
/******/ var installedModules = {};
/******/ function require(moduleId) {
/******/ if(installedModules[moduleId])
/******/ return installedModules[moduleId].exports;
/******/ var module = installedModules[moduleId] = {
/******/ exports: {},
/******/ id: moduleId,
/******/ loaded: false
/******/ };
/******/ modules[moduleId].call(null, module, module.exports, require);
/******/ module.loaded = true;
/******/ return module.exports;
/******/ }
/******/ require.e = function requireEnsure(chunkId, callback) {
/******/ callback.call(null, require);
/******/ };
/******/ require.modules = modules;
/******/ require.cache = installedModules;
/******/ return require(0);
/******/ })({
/******/ c: "",
/***/ 0:
/*!********************!*\
!*** ./example.js ***!
\********************/
/***/ function(module, exports, require) {
/* WEBPACK VAR INJECTION */(function(module) {// Polyfill require for node.js usage of loaders
require = require(/*! enhanced-require */ 3)(module);
// use our loader
console.dir(require(/*! ./loader!./file */ 4));
// use buildin json loader
2013-02-13 18:58:13 +08:00
console.dir(require(/*! ./test.json */ 1)); // default by extension
2013-01-31 01:49:25 +08:00
console.dir(require(/*! json!./test.json */ 1)); // manual
/* WEBPACK VAR INJECTION */}(require(/*! (webpack)/buildin/module.js */ 2)(module)))
/***/ },
/***/ 1:
2013-02-13 18:58:13 +08:00
/*!*******************************************!*\
!*** (webpack)/~/json-loader!./test.json ***!
\*******************************************/
2013-01-31 01:49:25 +08:00
/***/ function(module, exports, require) {
module.exports = {
"foobar": 1234
2012-10-25 01:28:17 +08:00
}
2013-01-31 01:49:25 +08:00
/***/ },
/***/ 2:
/*!***********************************!*\
!*** (webpack)/buildin/module.js ***!
\***********************************/
/***/ function(module, exports, require) {
module.exports = function(module) {
if(!module.webpackPolyfill) {
module.deprecate = function() {};
module.paths = [];
// module.parent = undefined by default
module.children = [];
module.webpackPolyfill = 1;
}
return module;
}
2012-10-26 21:07:50 +08:00
2013-01-31 01:49:25 +08:00
/***/ },
2012-10-26 21:07:50 +08:00
2013-01-31 01:49:25 +08:00
/***/ 3:
/*!*******************************************!*\
!*** (webpack)/buildin/return-require.js ***!
\*******************************************/
/***/ function(module, exports, require) {
2012-10-26 21:07:50 +08:00
2013-01-31 01:49:25 +08:00
module.exports = function() { return require; };
2012-07-11 23:53:46 +08:00
2013-01-31 01:49:25 +08:00
/***/ },
2012-03-27 06:00:51 +08:00
2013-01-31 01:49:25 +08:00
/***/ 4:
/*!*****************************!*\
!*** ./loader.js!./file.js ***!
\*****************************/
/***/ function(module, exports, require) {
2012-11-07 19:49:01 +08:00
2013-01-31 01:49:25 +08:00
exports.answer = 42;
exports.foo = "bar";
2012-11-07 19:49:01 +08:00
2013-01-31 01:49:25 +08:00
/***/ }
/******/ })
2012-11-07 19:49:01 +08:00
2012-03-27 06:00:51 +08:00
```
2012-03-27 10:23:11 +08:00
# Console output
Prints in node.js (`node example.js`) and in browser:
```
{ answer: 42, foo: 'bar' }
{ foobar: 1234 }
{ foobar: 1234 }
```
2012-03-27 06:00:51 +08:00
# Info
## Uncompressed
```
2013-02-21 03:50:53 +08:00
Hash: 1fc63791e8921db5e3ee47d7b25fa6a8
Time: 84ms
2013-01-31 01:49:25 +08:00
Asset Size Chunks Chunk Names
2013-02-21 03:50:53 +08:00
output.js 2558 0 main
chunk {0} output.js (main) 693
[0] ./example.js 305 [built] {0}
2013-02-13 18:58:13 +08:00
[1] (webpack)/~/json-loader!./test.json 36 [built] {0}
cjs require ./test.json [0] ./example.js 8:12-34
cjs require !json!./test.json [0] ./example.js 9:12-40
2013-02-21 03:50:53 +08:00
[2] (webpack)/buildin/module.js 251 [built] {0}
cjs require module [0] ./example.js 1:0-108
2013-01-31 01:49:25 +08:00
[3] (webpack)/buildin/return-require.js 60 [built] {0}
cjs require enhanced-require [0] ./example.js 2:10-37
[4] ./loader.js!./file.js 41 [not cacheable] [built] {0}
cjs require ./loader!./file [0] ./example.js 5:12-38
2012-03-27 06:00:51 +08:00
```