Skip to content

Commit 817e678

Browse files
committed
added DllPlugin and dll example
1 parent 1d0cf82 commit 817e678

19 files changed

+543
-32
lines changed

examples/dll/README.md

Lines changed: 231 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,231 @@
1+
# webpack.config.js
2+
3+
``` javascript
4+
var path = require("path");
5+
var DllPlugin = require("../../lib/DllPlugin");
6+
module.exports = {
7+
entry: {
8+
alpha: ["./alpha", "./a"],
9+
beta: ["./beta", "./b"]
10+
},
11+
output: {
12+
path: path.join(__dirname, "js"),
13+
filename: "MyDll.[name].js",
14+
library: "[name]_[hash]"
15+
},
16+
plugins: [
17+
new DllPlugin({
18+
path: path.join(__dirname, "js", "[name]-manifest.json"),
19+
name: "[name]_[hash]"
20+
})
21+
]
22+
};
23+
```
24+
25+
# js/MyDll.alpha.js
26+
27+
``` javascript
28+
var alpha_fda802f3c408a66ef744 =
29+
/******/ (function(modules) { // webpackBootstrap
30+
/******/ // The module cache
31+
/******/ var installedModules = {};
32+
33+
/******/ // The require function
34+
/******/ function __webpack_require__(moduleId) {
35+
36+
/******/ // Check if module is in cache
37+
/******/ if(installedModules[moduleId])
38+
/******/ return installedModules[moduleId].exports;
39+
40+
/******/ // Create a new module (and put it into the cache)
41+
/******/ var module = installedModules[moduleId] = {
42+
/******/ exports: {},
43+
/******/ id: moduleId,
44+
/******/ loaded: false
45+
/******/ };
46+
47+
/******/ // Execute the module function
48+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
49+
50+
/******/ // Flag the module as loaded
51+
/******/ module.loaded = true;
52+
53+
/******/ // Return the exports of the module
54+
/******/ return module.exports;
55+
/******/ }
56+
57+
58+
/******/ // expose the modules object (__webpack_modules__)
59+
/******/ __webpack_require__.m = modules;
60+
61+
/******/ // expose the module cache
62+
/******/ __webpack_require__.c = installedModules;
63+
64+
/******/ // __webpack_public_path__
65+
/******/ __webpack_require__.p = "js/";
66+
67+
/******/ // Load entry module and return exports
68+
/******/ return __webpack_require__(0);
69+
/******/ })
70+
/************************************************************************/
71+
/******/ ([
72+
/* 0 */
73+
/*!*****************!*\
74+
!*** dll alpha ***!
75+
\*****************/
76+
/***/ function(module, exports, __webpack_require__) {
77+
78+
module.exports = __webpack_require__;
79+
80+
/***/ },
81+
/* 1 */
82+
/*!******************!*\
83+
!*** ./alpha.js ***!
84+
\******************/
85+
/***/ function(module, exports, __webpack_require__) {
86+
87+
module.exports = "alpha";
88+
89+
/***/ },
90+
/* 2 */
91+
/*!**************!*\
92+
!*** ./a.js ***!
93+
\**************/
94+
/***/ function(module, exports, __webpack_require__) {
95+
96+
module.exports = "a";
97+
98+
/***/ }
99+
/******/ ]);
100+
```
101+
102+
# js/MyDll.beta.js
103+
104+
``` javascript
105+
var beta_fda802f3c408a66ef744 =
106+
/******/ (function(modules) { // webpackBootstrap
107+
/******/ // The module cache
108+
/******/ var installedModules = {};
109+
110+
/******/ // The require function
111+
/******/ function __webpack_require__(moduleId) {
112+
113+
/******/ // Check if module is in cache
114+
/******/ if(installedModules[moduleId])
115+
/******/ return installedModules[moduleId].exports;
116+
117+
/******/ // Create a new module (and put it into the cache)
118+
/******/ var module = installedModules[moduleId] = {
119+
/******/ exports: {},
120+
/******/ id: moduleId,
121+
/******/ loaded: false
122+
/******/ };
123+
124+
/******/ // Execute the module function
125+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
126+
127+
/******/ // Flag the module as loaded
128+
/******/ module.loaded = true;
129+
130+
/******/ // Return the exports of the module
131+
/******/ return module.exports;
132+
/******/ }
133+
134+
135+
/******/ // expose the modules object (__webpack_modules__)
136+
/******/ __webpack_require__.m = modules;
137+
138+
/******/ // expose the module cache
139+
/******/ __webpack_require__.c = installedModules;
140+
141+
/******/ // __webpack_public_path__
142+
/******/ __webpack_require__.p = "js/";
143+
144+
/******/ // Load entry module and return exports
145+
/******/ return __webpack_require__(0);
146+
/******/ })
147+
/************************************************************************/
148+
/******/ ([
149+
/* 0 */
150+
/*!****************!*\
151+
!*** dll beta ***!
152+
\****************/
153+
/***/ function(module, exports, __webpack_require__) {
154+
155+
module.exports = __webpack_require__;
156+
157+
/***/ },
158+
/* 1 */,
159+
/* 2 */,
160+
/* 3 */
161+
/*!*****************!*\
162+
!*** ./beta.js ***!
163+
\*****************/
164+
/***/ function(module, exports, __webpack_require__) {
165+
166+
module.exports = "beta";
167+
168+
/***/ },
169+
/* 4 */
170+
/*!**************!*\
171+
!*** ./b.js ***!
172+
\**************/
173+
/***/ function(module, exports, __webpack_require__) {
174+
175+
module.exports = "b";
176+
177+
/***/ }
178+
/******/ ]);
179+
```
180+
181+
# Info
182+
183+
## Uncompressed
184+
185+
```
186+
Hash: fda802f3c408a66ef744
187+
Version: webpack 1.9.5
188+
Time: 54ms
189+
Asset Size Chunks Chunk Names
190+
MyDll.alpha.js 1.88 kB 0 [emitted] alpha
191+
MyDll.beta.js 1.89 kB 1 [emitted] beta
192+
chunk {0} MyDll.alpha.js (alpha) 58 bytes [rendered]
193+
> alpha [0] dll alpha
194+
[0] dll alpha 12 bytes {0} [built]
195+
[1] ./alpha.js 25 bytes {0} [built]
196+
single entry ./alpha [0] dll alpha
197+
[2] ./a.js 21 bytes {0} [built]
198+
single entry ./a [0] dll alpha
199+
chunk {1} MyDll.beta.js (beta) 57 bytes [rendered]
200+
> beta [0] dll beta
201+
[0] dll beta 12 bytes {1} [built]
202+
[3] ./beta.js 24 bytes {1} [built]
203+
single entry ./beta [0] dll beta
204+
[4] ./b.js 21 bytes {1} [built]
205+
single entry ./b [0] dll beta
206+
```
207+
208+
## Minimized (uglify-js, no zip)
209+
210+
```
211+
Hash: 28b01778c3ed267edad7
212+
Version: webpack 1.9.5
213+
Time: 129ms
214+
Asset Size Chunks Chunk Names
215+
MyDll.beta.js 330 bytes 0 [emitted] beta
216+
MyDll.alpha.js 330 bytes 1 [emitted] alpha
217+
chunk {0} MyDll.beta.js (beta) 57 bytes [rendered]
218+
> beta [0] dll beta
219+
[0] dll beta 12 bytes {0} [built]
220+
[3] ./b.js 21 bytes {0} [built]
221+
single entry ./b [0] dll beta
222+
[4] ./beta.js 24 bytes {0} [built]
223+
single entry ./beta [0] dll beta
224+
chunk {1} MyDll.alpha.js (alpha) 58 bytes [rendered]
225+
> alpha [0] dll alpha
226+
[0] dll alpha 12 bytes {1} [built]
227+
[1] ./a.js 21 bytes {1} [built]
228+
single entry ./a [0] dll alpha
229+
[2] ./alpha.js 25 bytes {1} [built]
230+
single entry ./alpha [0] dll alpha
231+
```

examples/dll/a.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "a";

examples/dll/alpha.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "alpha";

examples/dll/b.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "b";

examples/dll/beta.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
module.exports = "beta";

examples/dll/build.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
global.NO_TARGET_ARGS = true;
2+
require("../build-common");

examples/dll/template.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# webpack.config.js
2+
3+
``` javascript
4+
{{webpack.config.js}}
5+
```
6+
7+
# js/MyDll.alpha.js
8+
9+
``` javascript
10+
{{js/MyDll.alpha.js}}
11+
```
12+
13+
# js/MyDll.beta.js
14+
15+
``` javascript
16+
{{js/MyDll.beta.js}}
17+
```
18+
19+
# Info
20+
21+
## Uncompressed
22+
23+
```
24+
{{stdout}}
25+
```
26+
27+
## Minimized (uglify-js, no zip)
28+
29+
```
30+
{{min:stdout}}
31+
```

examples/dll/webpack.config.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
var path = require("path");
2+
var DllPlugin = require("../../lib/DllPlugin");
3+
module.exports = {
4+
entry: {
5+
alpha: ["./alpha", "./a"],
6+
beta: ["./beta", "./b"]
7+
},
8+
output: {
9+
path: path.join(__dirname, "js"),
10+
filename: "MyDll.[name].js",
11+
library: "[name]_[hash]"
12+
},
13+
plugins: [
14+
new DllPlugin({
15+
path: path.join(__dirname, "js", "[name]-manifest.json"),
16+
name: "[name]_[hash]"
17+
})
18+
]
19+
};

lib/DelegatedModuleFactoryPlugin.js

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
Author Tobias Koppers @sokra
44
*/
55
var DelegatedModule = require("./DelegatedModule");
6-
var path = require("path");
76

87
function DelegatedModuleFactoryPlugin(options) {
98
this.options = options;
@@ -17,22 +16,14 @@ function DelegatedModuleFactoryPlugin(options) {
1716
module.exports = DelegatedModuleFactoryPlugin;
1817

1918
DelegatedModuleFactoryPlugin.prototype.apply = function(normalModuleFactory) {
20-
normalModuleFactory.plugin("create-module", function(data) {
21-
var request = DelegatedModuleFactoryPlugin.contextify(this.options.context, data.userRequest);
22-
if(request in this.options.content) {
23-
var resolved = this.options.content[request];
24-
return new DelegatedModule(this.options.source, resolved, this.options.type);
19+
normalModuleFactory.plugin("module", function(module) {
20+
if(module.libIdent) {
21+
var request = module.libIdent(this.options);
22+
if(request && request in this.options.content) {
23+
var resolved = this.options.content[request];
24+
return new DelegatedModule(this.options.source, resolved, this.options.type);
25+
}
2526
}
27+
return module;
2628
}.bind(this));
2729
};
28-
29-
DelegatedModuleFactoryPlugin.contextify = function(context, request) {
30-
return request.split("!").map(function(r) {
31-
var rp = path.relative(context, r);
32-
if(path.sep === "\\")
33-
rp = rp.replace(/\\/g, "/");
34-
if(rp.indexOf("../") !== 0)
35-
rp = "./" + rp;
36-
return rp;
37-
}).join("!");
38-
};

lib/DllEntryPlugin.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var DllEntryDependency = require("./dependencies/DllEntryDependency");
6+
var SingleEntryDependency = require("./dependencies/SingleEntryDependency");
7+
var DllModuleFactory = require("./DllModuleFactory");
8+
9+
function DllEntryPlugin(context, entries, name, type) {
10+
this.context = context;
11+
this.entries = entries;
12+
this.name = name;
13+
this.type = type;
14+
}
15+
module.exports = DllEntryPlugin;
16+
DllEntryPlugin.prototype.apply = function(compiler) {
17+
compiler.plugin("compilation", function(compilation, params) {
18+
var dllModuleFactory = new DllModuleFactory();
19+
var normalModuleFactory = params.normalModuleFactory;
20+
21+
compilation.dependencyFactories.set(DllEntryDependency, dllModuleFactory);
22+
23+
compilation.dependencyFactories.set(SingleEntryDependency, normalModuleFactory);
24+
});
25+
compiler.plugin("make", function(compilation, callback) {
26+
compilation.addEntry(this.context, new DllEntryDependency(this.entries.map(function(e, idx) {
27+
var dep = new SingleEntryDependency(e);
28+
dep.loc = this.name + ":" + idx;
29+
return dep;
30+
}, this), this.name, this.type), this.name, callback);
31+
}.bind(this));
32+
};

0 commit comments

Comments
 (0)