You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: README.md
+71-21Lines changed: 71 additions & 21 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -2,6 +2,7 @@
2
2
3
3
As developer you want to reuse existing code.
4
4
As with node.js and web all file are already in the same language, but it is extra work to use your code with the node.js module system and the browser.
5
+
5
6
The goal of `webpack` is to bundle CommonJs modules into javascript files which can be loaded by `<script>`-tags.
6
7
Simply concating all required files has a disadvantage: many code to download (and execute) on page load.
7
8
Therefore `webpack` uses the `require.ensure` function ([CommonJs/Modules/Async/A](http://wiki.commonjs.org/wiki/Modules/Async/A)) to split your code automatically into multiple bundles which are loaded on demand.
@@ -12,16 +13,16 @@ The result is a smaller inital code download which results in faster page load.
12
13
13
14
* bundle CommonJs modules for browser
14
15
* reuse server-side code (node.js) on client-side
15
-
* create multiple files which are loaded on demand (faster page load in big webapps)
16
-
* dependencies managed for you, on compile time
16
+
* create multiple files which are loaded on demand (faster page load in big webapps or on mobile connections)
17
+
* dependencies managed for you, on compile time (no resolution on runtime needed)
17
18
18
19
## Goals
19
20
20
-
* minimize code size
21
+
* make node.js and browser development similar
22
+
* minimize code size (mobile connection)
21
23
* minimize code size on inital download
22
24
* download code only on demand
23
-
* hide development details, like module names and folder structure
24
-
* require minimal configuration
25
+
* require minimal configuration, but offer a maximum
25
26
* load polyfills for node-specific things if used
26
27
* offer replacements for node buildin libaries
27
28
@@ -83,6 +84,10 @@ File 2: 1.web.js
83
84
- code of module d and dependencies
84
85
```
85
86
87
+
Initially only `web.js` is included (and loaded) by your application.
88
+
`1.web.js` is loaded when the call to `require.ensure` happens.
89
+
After the second bundle (`1.web.js`) is loaded, the callback function will be invoked.
90
+
86
91
See [details](modules-webpack/tree/master/examples/code-splitting) for exact output.
87
92
88
93
## Reusing node.js code
@@ -143,7 +148,7 @@ function getTemplate(templateName) {
143
148
// which compiles to: return require(123)("./"+templateName)
144
149
```
145
150
146
-
See [details](modules-webpack/tree/master/examples/require.context) for complete example.
151
+
See [details](/sokra/modules-webpack/tree/master/examples/require.context) for complete example.
147
152
148
153
When try to store the `require` function in another variable or try to pass it as parameter,
149
154
`webpack` convert it to a `require.context(".")` to be compatible.
@@ -168,7 +173,7 @@ The `raw` loader plugin is looked up at modules `raw-webpack-web-loader`, `raw-w
168
173
and the following files are looked up: `index.webpack-web-loader.js`, `index.webpack-loader.js`, `index.web-loader.js`, `index.loader.js`, `index`, `index.js`.
169
174
Note that the `web-` versions are omitted if loaders are used in node.js.
170
175
171
-
See [example](modules-webpack/tree/master/examples/loader).
176
+
See [example](/sokra/modules-webpack/tree/master/examples/loader).
172
177
173
178
The following loaders are included as optional dependencies:
174
179
@@ -218,29 +223,30 @@ try `--min` to minimize with `uglify-js`.
218
223
219
224
### `require`-function
220
225
221
-
*`require` should not be overwritten
226
+
*`require` should not be overwritten, except from polyfill
222
227
*`require.ensure` should not be overwritten or called indirect
223
228
*`require.context` should not be overwritten or called indirect
224
229
* the argument to `require.context` should be a literal or addition of multiple literals
225
230
* An indirect call of `require` should access a file in current directory: This throws an exception: `var r = require; r("../file");`
226
231
227
232
The following cases could result in **too much code** in result file if used wrong:
228
233
229
-
* indirect call of `require`: `var r = require; r("./file");`
234
+
* indirect call of `require`: `var r = require; r("./file");`. It includes the whole directory.
230
235
*`require.context`. It includes the whole directory.
231
-
* expressions in require arguments: `require(variable)` (but `webpack` is smart enough for this`require(condition ? "a" : "b")`)
232
-
* the function passed to `require.ensure` is not inlined in the call.
236
+
* expressions in require arguments: `require(variable)`. It includes the whole directory. (except from `?:`-operator`require(condition ? "a" : "b")`)
237
+
* the function passed to `require.ensure` is not inlined in the call. Only requires in inlined function move into the second bundle.
233
238
234
239
235
240
### node.js specific modules
236
241
237
-
As node.js specific modules like `fs` will not work in browser they are not included and cause an error.
242
+
As node.js specific modules like `fs` will not work in browser they are not included (by default) and cause an exception.
238
243
You should replace them by own modules if you want to use them.
239
-
For some modules are replacements included in `webpack`.
240
-
Some credit goes to the browserify contributors, as I took some replacements from them.
244
+
For some simple modules are replacements included in `webpack`.
241
245
Expensive replacements are not needed by everyone, so that are not included by default.
242
246
You need to specify `--alias [module]=[replacement]` to use them.
243
-
A warning saying that some module is missing is emitted in the case you use it.
247
+
A warning saying that some module is missing is emitted in the case you use it without providing a replacement.
248
+
249
+
Some credit goes to the browserify contributors, you can use replacements provided by them.
0 commit comments