Skip to content

Commit a7f9340

Browse files
committed
added externals option
1 parent a9d6afc commit a7f9340

11 files changed

Lines changed: 337 additions & 19 deletions

‎examples/externals/README.md‎

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
2+
# example.js
3+
4+
``` javascript
5+
var add = require("add");
6+
7+
module.exports = add(40, 2);
8+
```
9+
10+
# webpack.config.js
11+
12+
``` javascript
13+
module.exports = {
14+
output: {
15+
libraryTarget: "this"
16+
},
17+
externals: {
18+
"add": true
19+
}
20+
}
21+
```
22+
23+
# js/output.js
24+
25+
``` javascript
26+
(function(e, a) { for(var i in a) e[i] = a[i]; }(this, /******/ (function(modules) { // webpackBootstrap
27+
/******/
28+
/******/ // The module cache
29+
/******/ var installedModules = {};
30+
/******/
31+
/******/ // The require function
32+
/******/ function __webpack_require__(moduleId) {
33+
/******/ // Check if module is in cache
34+
/******/ if(installedModules[moduleId])
35+
/******/ return installedModules[moduleId].exports;
36+
/******/
37+
/******/ // Create a new module (and put it into the cache)
38+
/******/ var module = installedModules[moduleId] = {
39+
/******/ exports: {},
40+
/******/ id: moduleId,
41+
/******/ loaded: false
42+
/******/ };
43+
/******/
44+
/******/ // Execute the module function
45+
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
46+
/******/
47+
/******/ // Flag the module as loaded
48+
/******/ module.loaded = true;
49+
/******/
50+
/******/ // Return the exports of the module
51+
/******/ return module.exports;
52+
/******/ }
53+
/******/
54+
/******/
55+
/******/ // expose the modules object (__webpack_modules__)
56+
/******/ __webpack_require__.m = modules;
57+
/******/
58+
/******/ // expose the module cache
59+
/******/ __webpack_require__.c = installedModules;
60+
/******/
61+
/******/ // __webpack_public_path__
62+
/******/ __webpack_require__.p = "js/";
63+
/******/
64+
/******/
65+
/******/ // Load entry module and return exports
66+
/******/ return __webpack_require__(0);
67+
/******/ })
68+
/************************************************************************/
69+
/******/ ([
70+
/* 0 */
71+
/*!********************!*\
72+
!*** ./example.js ***!
73+
\********************/
74+
/***/ function(module, exports, __webpack_require__) {
75+
76+
var add = __webpack_require__(/*! add */ 1);
77+
78+
module.exports = add(40, 2);
79+
80+
/***/ },
81+
/* 1 */
82+
/*!********************!*\
83+
!*** external add ***!
84+
\********************/
85+
/***/ function(module, exports, __webpack_require__) {
86+
87+
(function() { module.exports = this["add"]; }());
88+
89+
/***/ }
90+
/******/ ])))
91+
```
92+
93+
# Info
94+
95+
## Uncompressed
96+
97+
```
98+
Hash: 4b1215a5724ebdb340c6
99+
Version: webpack 1.1.0-beta1
100+
Time: 213ms
101+
Asset Size Chunks Chunk Names
102+
output.js 1949 0 [emitted] main
103+
chunk {0} output.js (main) 99 [rendered]
104+
> main [0] ./example.js
105+
[0] ./example.js 57 {0} [built]
106+
```
107+
108+
## Minimized (uglify-js, no zip)
109+
110+
```
111+
Hash: 9397fce116b2fe76bdb7
112+
Version: webpack 1.1.0-beta1
113+
Time: 293ms
114+
Asset Size Chunks Chunk Names
115+
output.js 344 0 [emitted] main
116+
chunk {0} output.js (main) 99 [rendered]
117+
> main [0] ./example.js
118+
[0] ./example.js 57 {0} [built]
119+
```

‎examples/externals/build.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require("../build-common");

‎examples/externals/example.js‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
var add = require("add");
2+
3+
module.exports = add(40, 2);

‎examples/externals/template.md‎

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

‎lib/AmdMainTemplateDecorator.js‎

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,19 @@ function AmdMainTemplateDecorator(mainTemplate, name) {
1111
module.exports = AmdMainTemplateDecorator;
1212
AmdMainTemplateDecorator.prototype.render = function(hash, chunk, moduleTemplate, dependencyTemplates) {
1313
var source = this.mainTemplate.render(hash, chunk, moduleTemplate, dependencyTemplates);
14+
var externals = chunk.modules.filter(function(m) {
15+
return m.external;
16+
});
17+
var externalsDepsArray = JSON.stringify(externals.map(function(m) {
18+
return m.request;
19+
}));
20+
var externalsArguments = externals.map(function(m) {
21+
return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__";
22+
}).join(", ");
1423
if(this.name) {
15-
return new ConcatSource("define(" + JSON.stringify(this.name) + ", [], function() { return ", source, "});");
24+
return new ConcatSource("define(" + JSON.stringify(this.name) + ", " + externalsDepsArray + ", function(" + externalsArguments + ") { return ", source, "});");
25+
} else if(externalsArguments) {
26+
return new ConcatSource("define(" + externalsDepsArray + ", function(" + externalsArguments + ") { return ", source, "});");
1627
} else {
1728
return new ConcatSource("define(function() { return ", source, "});");
1829
}

‎lib/ExternalModule.js‎

Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var Module = require("./Module");
6+
var OriginalSource = require("webpack-core/lib/OriginalSource");
7+
var RawSource = require("webpack-core/lib/RawSource");
8+
9+
function ExternalModule(request, type) {
10+
Module.call(this);
11+
this.request = request;
12+
this.type = type;
13+
this.built = false;
14+
}
15+
module.exports = ExternalModule;
16+
17+
ExternalModule.prototype = Object.create(Module.prototype);
18+
19+
ExternalModule.prototype.external = true;
20+
21+
ExternalModule.prototype.identifier = function() {
22+
return "external " + this.request;
23+
};
24+
25+
ExternalModule.prototype.readableIdentifier = function(requestShortener) {
26+
return "external " + this.request;
27+
};
28+
29+
ExternalModule.prototype.needRebuild = function(fileTimestamps, contextTimestamps) {
30+
return false;
31+
};
32+
33+
ExternalModule.prototype.build = function(options, compilation, resolver, fs, callback) {
34+
this.builtTime = new Date().getTime();
35+
callback();
36+
};
37+
38+
ExternalModule.prototype.source = function(dependencyTemplates, outputOptions, requestShortener) {
39+
var str = "throw new Error('Externals not supported');";
40+
var request = this.request;
41+
switch(this.type) {
42+
case "var":
43+
str = "module.exports = " + request + ";";
44+
break;
45+
case "this":
46+
str = "(function() { module.exports = this[" + JSON.stringify(request) + "]; }());";
47+
break;
48+
case "commonjs":
49+
case "commonjs2":
50+
if(Array.isArray(request)) {
51+
str = "module.exports = require(" + JSON.stringify(request[0]) + ")" + request.slice(1).map(function(r) {
52+
return "[" + JSON.stringify(r) + "]";
53+
}).join("") + ";";
54+
} else
55+
str = "module.exports = require(" + JSON.stringify(request) + ");";
56+
break;
57+
case "amd":
58+
case "umd":
59+
str = "module.exports = __WEBPACK_EXTERNAL_MODULE_" + this.id + "__;";
60+
break;
61+
}
62+
if(this.useSourceMap) {
63+
return new OriginalSource(str, this.identifier());
64+
} else {
65+
return new RawSource(str);
66+
}
67+
};
68+
69+
ExternalModule.prototype.size = function() {
70+
return 42;
71+
};
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var ExternalModule = require("./ExternalModule");
6+
7+
function ExternalModuleFactoryDecorator(factory, type, externals) {
8+
this.factory = factory;
9+
this.type = type;
10+
this.externals = externals;
11+
}
12+
module.exports = ExternalModuleFactoryDecorator;
13+
14+
ExternalModuleFactoryDecorator.prototype.plugin = function() {
15+
return this.factory.plugin.apply(this.factory, arguments);
16+
};
17+
18+
ExternalModuleFactoryDecorator.prototype.create = function(context, dependency, callback) {
19+
if(Object.prototype.hasOwnProperty.call(this.externals, dependency.request)) {
20+
var value = this.externals[dependency.request];
21+
if(value === true) value = dependency.request;
22+
return callback(null, new ExternalModule(value, this.type));
23+
}
24+
return this.factory.create(context, dependency, callback);
25+
};

‎lib/ExternalsPlugin.js‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var ExternalModuleFactoryDecorator = require("./ExternalModuleFactoryDecorator");
6+
7+
function ExternalsPlugin(type, externals) {
8+
this.type = type;
9+
this.externals = externals;
10+
}
11+
module.exports = ExternalsPlugin;
12+
ExternalsPlugin.prototype.apply = function(compiler) {
13+
compiler.plugin("compile", function(params) {
14+
params.normalModuleFactory = new ExternalModuleFactoryDecorator(params.normalModuleFactory, this.type, this.externals);
15+
}.bind(this));
16+
};

‎lib/UmdMainTemplateDecorator.js‎

Lines changed: 46 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -12,28 +12,56 @@ function UmdMainTemplateDecorator(mainTemplate, name) {
1212
module.exports = UmdMainTemplateDecorator;
1313
UmdMainTemplateDecorator.prototype.render = function(hash, chunk, moduleTemplate, dependencyTemplates) {
1414
var source = this.mainTemplate.render(hash, chunk, moduleTemplate, dependencyTemplates);
15+
var externals = chunk.modules.filter(function(m) {
16+
return m.external;
17+
});
18+
var externalsDepsArray = JSON.stringify(externals.map(function(m) {
19+
return m.request;
20+
}));
21+
var externalsRootArray = externals.map(function(m) {
22+
return "root[" + JSON.stringify(m.request) + "]";
23+
}).join(", ");
24+
var externalsVarArray = externals.map(function(m) {
25+
return m.request;
26+
}).join(", ");
27+
var externalsRequireArray = externals.map(function(m) {
28+
var request = m.request;
29+
if(Array.isArray(request)) {
30+
return "require(" + JSON.stringify(request[0]) + ")" + request.slice(1).map(function(r) {
31+
return "[" + JSON.stringify(r) + "]";
32+
}).join("");
33+
} else
34+
return "require(" + JSON.stringify(request) + ")";
35+
}).join(", ");
36+
var externalsArguments = externals.map(function(m) {
37+
return "__WEBPACK_EXTERNAL_MODULE_" + m.id + "__";
38+
}).join(", ");
1539
return new ConcatSource(new OriginalSource(
16-
"(function webpackUniversalModuleDefinition(root) {\n" +
17-
" return function webpackUniversalModuleDefinitionWrapBootstrap(fn) {\n" +
18-
" return function webpackUniversalModuleDefinitionBootstrap(modules) {\n" +
19-
" if(typeof exports === 'object' && typeof module === 'object')\n" +
20-
" module.exports = fn(modules);\n" +
21-
" else if(typeof define === 'function' && define.amd)\n" +
22-
" define(function() { return fn(modules); });\n" +
40+
"(function webpackUniversalModuleDefinition(root, factory) {\n" +
41+
" if(typeof exports === 'object' && typeof module === 'object')\n" +
42+
" module.exports = factory(" + externalsRequireArray + ");\n" +
43+
" else if(typeof define === 'function' && define.amd)\n" +
44+
(externalsArguments ?
45+
" define(" + externalsDepsArray + ", factory);\n"
46+
:
47+
" define(factory);\n"
48+
) +
2349
(this.name ?
24-
" else if(typeof exports === 'object')\n" +
25-
" exports[" + JSON.stringify(this.name) + "] = fn(modules);\n" +
26-
" else\n" +
27-
" root[" + JSON.stringify(this.name) + "] = fn(modules);\n"
50+
" else if(typeof exports === 'object')\n" +
51+
" exports[" + JSON.stringify(this.name) + "] = factory(" + externalsRequireArray + ");\n" +
52+
" else\n" +
53+
" root[" + JSON.stringify(this.name) + "] = factory(" + externalsRootArray + ");\n"
2854
:
29-
" else {\n" +
30-
" var a = fn(modules);\n" +
31-
" for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
32-
" }\n"
55+
" else {\n" +
56+
(externalsArguments ?
57+
" var a = typeof exports === 'object' ? factory(" + externalsRequireArray + ") : factory(" + externalsRootArray + ");\n"
58+
:
59+
" var a = factory();\n"
60+
) +
61+
" for(var i in a) (typeof exports === 'object' ? exports : root)[i] = a[i];\n" +
62+
" }\n"
3363
) +
34-
" }\n" +
35-
" }\n" +
36-
"})(this)\n", "webpack/universalModuleDefinition"), source);
64+
"})(this, function(" + externalsArguments + ") {\nreturn ", "webpack/universalModuleDefinition"), source, "\n})\n");
3765
};
3866
UmdMainTemplateDecorator.prototype.updateHash = function(hash) {
3967
hash.update("umd");

0 commit comments

Comments
 (0)