mirror of https://github.com/webpack/webpack.git
removed LabeledModules
This commit is contained in:
parent
ac6b9541b3
commit
a14e563f67
|
@ -1,169 +0,0 @@
|
|||
This example show how to add support for Labeled Modules by adding the plugin to the configuration.
|
||||
|
||||
# example.js
|
||||
|
||||
``` javascript
|
||||
require: "./increment";
|
||||
var a = 1;
|
||||
increment(a); // 2
|
||||
```
|
||||
|
||||
# webpack.config.js
|
||||
|
||||
``` javascript
|
||||
var webpack = require("../../");
|
||||
module.exports = {
|
||||
plugins: [
|
||||
new webpack.dependencies.LabeledModulesPlugin()
|
||||
]
|
||||
}
|
||||
```
|
||||
|
||||
# increment.js
|
||||
|
||||
``` javascript
|
||||
require: "./math";
|
||||
exports: function increment(val) {
|
||||
return add(val, 1);
|
||||
};
|
||||
```
|
||||
|
||||
# math.js
|
||||
|
||||
``` javascript
|
||||
exports: function add() {
|
||||
var sum = 0, i = 0, args = arguments, l = args.length;
|
||||
while (i < l) {
|
||||
sum += args[i++];
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
```
|
||||
|
||||
# js/output.js
|
||||
|
||||
``` javascript
|
||||
/******/ (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] = {
|
||||
/******/ i: moduleId,
|
||||
/******/ l: false,
|
||||
/******/ exports: {}
|
||||
/******/ };
|
||||
|
||||
/******/ // Execute the module function
|
||||
/******/ modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);
|
||||
|
||||
/******/ // Flag the module as loaded
|
||||
/******/ module.l = 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;
|
||||
|
||||
/******/ // identity function for calling harmory imports with the correct context
|
||||
/******/ __webpack_require__.i = function(value) { return value; };
|
||||
|
||||
/******/ // __webpack_public_path__
|
||||
/******/ __webpack_require__.p = "js/";
|
||||
|
||||
/******/ // Load entry module and return exports
|
||||
/******/ return __webpack_require__(__webpack_require__.s = 1);
|
||||
/******/ })
|
||||
/************************************************************************/
|
||||
/******/ ([
|
||||
/* 0 */
|
||||
/*!**********************!*\
|
||||
!*** ./increment.js ***!
|
||||
\**********************/
|
||||
/***/ function(module, exports, __webpack_require__) {
|
||||
|
||||
var __WEBPACK_LABELED_MODULE__ = __webpack_require__(/*! ./math */ 2), add = __WEBPACK_LABELED_MODULE__.add;
|
||||
exports: exports["increment"] = function increment(val) {
|
||||
return add(val, 1);
|
||||
};
|
||||
|
||||
/***/ },
|
||||
/* 1 */
|
||||
/*!********************!*\
|
||||
!*** ./example.js ***!
|
||||
\********************/
|
||||
/***/ function(module, exports, __webpack_require__) {
|
||||
|
||||
var __WEBPACK_LABELED_MODULE__ = __webpack_require__(/*! ./increment */ 0), increment = __WEBPACK_LABELED_MODULE__.increment;
|
||||
var a = 1;
|
||||
increment(a); // 2
|
||||
|
||||
/***/ },
|
||||
/* 2 */
|
||||
/*!*****************!*\
|
||||
!*** ./math.js ***!
|
||||
\*****************/
|
||||
/***/ function(module, exports, __webpack_require__) {
|
||||
|
||||
exports: exports["add"] = function add() {
|
||||
var sum = 0, i = 0, args = arguments, l = args.length;
|
||||
while (i < l) {
|
||||
sum += args[i++];
|
||||
}
|
||||
return sum;
|
||||
};
|
||||
|
||||
/***/ }
|
||||
/******/ ]);
|
||||
```
|
||||
|
||||
The remaining labels are removed while minimizing.
|
||||
|
||||
# Info
|
||||
|
||||
## Uncompressed
|
||||
|
||||
```
|
||||
Hash: be12e2072fd7c78b192b
|
||||
Version: webpack 2.1.0-beta.11
|
||||
Time: 55ms
|
||||
Asset Size Chunks Chunk Names
|
||||
output.js 2.5 kB 0 [emitted] main
|
||||
chunk {0} output.js (main) 299 bytes [rendered]
|
||||
> main [1] ./example.js
|
||||
[0] ./increment.js 83 bytes {0} [built]
|
||||
labeled require ./increment [1] ./example.js 1:0-23
|
||||
[1] ./example.js 55 bytes {0} [built]
|
||||
[2] ./math.js 161 bytes {0} [built]
|
||||
labeled require ./math [0] ./increment.js 1:0-18
|
||||
```
|
||||
|
||||
## Minimized (uglify-js, no zip)
|
||||
|
||||
```
|
||||
Hash: be12e2072fd7c78b192b
|
||||
Version: webpack 2.1.0-beta.11
|
||||
Time: 130ms
|
||||
Asset Size Chunks Chunk Names
|
||||
output.js 448 bytes 0 [emitted] main
|
||||
chunk {0} output.js (main) 299 bytes [rendered]
|
||||
> main [1] ./example.js
|
||||
[0] ./increment.js 83 bytes {0} [built]
|
||||
labeled require ./increment [1] ./example.js 1:0-23
|
||||
[1] ./example.js 55 bytes {0} [built]
|
||||
[2] ./math.js 161 bytes {0} [built]
|
||||
labeled require ./math [0] ./increment.js 1:0-18
|
||||
```
|
|
@ -1 +0,0 @@
|
|||
require("../build-common");
|
|
@ -1,3 +0,0 @@
|
|||
require: "./increment";
|
||||
var a = 1;
|
||||
increment(a); // 2
|
|
@ -1,4 +0,0 @@
|
|||
require: "./math";
|
||||
exports: function increment(val) {
|
||||
return add(val, 1);
|
||||
};
|
|
@ -1,7 +0,0 @@
|
|||
exports: function add() {
|
||||
var sum = 0, i = 0, args = arguments, l = args.length;
|
||||
while (i < l) {
|
||||
sum += args[i++];
|
||||
}
|
||||
return sum;
|
||||
};
|
|
@ -1,47 +0,0 @@
|
|||
This example show how to add support for Labeled Modules by adding the plugin to the configuration.
|
||||
|
||||
# example.js
|
||||
|
||||
``` javascript
|
||||
{{example.js}}
|
||||
```
|
||||
|
||||
# webpack.config.js
|
||||
|
||||
``` javascript
|
||||
{{webpack.config.js}}
|
||||
```
|
||||
|
||||
# increment.js
|
||||
|
||||
``` javascript
|
||||
{{increment.js}}
|
||||
```
|
||||
|
||||
# math.js
|
||||
|
||||
``` javascript
|
||||
{{math.js}}
|
||||
```
|
||||
|
||||
# js/output.js
|
||||
|
||||
``` javascript
|
||||
{{js/output.js}}
|
||||
```
|
||||
|
||||
The remaining labels are removed while minimizing.
|
||||
|
||||
# Info
|
||||
|
||||
## Uncompressed
|
||||
|
||||
```
|
||||
{{stdout}}
|
||||
```
|
||||
|
||||
## Minimized (uglify-js, no zip)
|
||||
|
||||
```
|
||||
{{min:stdout}}
|
||||
```
|
|
@ -1,6 +0,0 @@
|
|||
var webpack = require("../../");
|
||||
module.exports = {
|
||||
plugins: [
|
||||
new webpack.dependencies.LabeledModulesPlugin()
|
||||
]
|
||||
}
|
|
@ -1,11 +1,10 @@
|
|||
// AMD Module Format
|
||||
define(
|
||||
"app/amd", // anonym is also supported
|
||||
["./commonjs", "./labeled", "./harmony"],
|
||||
function(commonjs1, labeled1, harmony1) {
|
||||
["./commonjs", "./harmony"],
|
||||
function(commonjs1, harmony1) {
|
||||
// but you can use CommonJs-style requires:
|
||||
var commonjs2 = require("./commonjs");
|
||||
var labeled2 = require("./labeled");
|
||||
var harmony2 = require("./harmony");
|
||||
// Do something...
|
||||
return 456;
|
||||
|
|
|
@ -3,10 +3,9 @@ module.exports = 123;
|
|||
|
||||
// but you can use amd style requires
|
||||
require(
|
||||
["./amd", "./labeled", "./harmony"],
|
||||
function(amd1, labeled1, harmony) {
|
||||
["./amd", "./harmony"],
|
||||
function(amd1, harmony) {
|
||||
var amd2 = require("./amd");
|
||||
var labeled2 = require("./labeled");
|
||||
var harmony2 = require("./harmony");
|
||||
}
|
||||
);
|
|
@ -1,20 +1,14 @@
|
|||
// CommonJs-style requires
|
||||
var commonjs1 = require("./commonjs");
|
||||
var amd1 = require("./amd");
|
||||
var labeled1 = require("./labeled");
|
||||
var harmony1 = require("./harmony");
|
||||
|
||||
// AMD-style requires (with all webpack features)
|
||||
require([
|
||||
"./commonjs", "./amd", "./labeled",
|
||||
"./commonjs", "./amd",
|
||||
"../require.context/templates/"+amd1+".js",
|
||||
Math.random() < 0.5 ? "./commonjs" : "./amd"],
|
||||
function(commonjs2, amd2, labeled2, template, randModule) {
|
||||
function(commonjs2, amd2, template, randModule) {
|
||||
// Do something with it...
|
||||
}
|
||||
);
|
||||
|
||||
// labeled modules requires
|
||||
require: "./labeled";
|
||||
// with the require label you are only allowed to import labeled modules
|
||||
// the module needs static information about exports
|
|
@ -1,6 +1,5 @@
|
|||
// ES6 Modules
|
||||
import commonjs from "./commonjs";
|
||||
import amd from "./amd";
|
||||
import labeled from "./labeled";
|
||||
|
||||
export default 456;
|
||||
|
|
|
@ -1,11 +0,0 @@
|
|||
// Labeled Module Format
|
||||
exports: var a = 123;
|
||||
|
||||
// but you can use amd and commonjs style requires
|
||||
require(
|
||||
["./commonjs", "./amd"],
|
||||
function(amd1) {
|
||||
var commonjs2 = require("./commonjs");
|
||||
var amd2 = require("./amd");
|
||||
}
|
||||
);
|
|
@ -1,4 +1,4 @@
|
|||
This example shows how you can mix different module styles in webpack. Here CommonJs, AMD and Labeled Modules are used. In addition to that there are different types of dynamic requires (`"../require.context/templates/"+amd1+".js"` and `Math.random() < 0.5 ? "./commonjs" : "./amd"`).
|
||||
This example shows how you can mix different module styles in webpack. Here CommonJs, AMD and Harmony Modules (ES6 Modules) are used. In addition to that there are different types of dynamic requires (`"../require.context/templates/"+amd1+".js"` and `Math.random() < 0.5 ? "./commonjs" : "./amd"`).
|
||||
|
||||
You see that everything is working nicely together.
|
||||
|
||||
|
@ -20,13 +20,6 @@ You see that everything is working nicely together.
|
|||
{{commonjs.js}}
|
||||
```
|
||||
|
||||
# labeled.js
|
||||
|
||||
``` javascript
|
||||
{{labeled.js}}
|
||||
```
|
||||
|
||||
|
||||
# js/output.js
|
||||
|
||||
``` javascript
|
||||
|
|
|
@ -1,6 +0,0 @@
|
|||
var webpack = require("../../");
|
||||
module.exports = {
|
||||
plugins: [
|
||||
new webpack.dependencies.LabeledModulesPlugin()
|
||||
]
|
||||
}
|
|
@ -1,21 +0,0 @@
|
|||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
var NullDependency = require("./NullDependency");
|
||||
|
||||
function LabeledExportsDependency(name, pos) {
|
||||
NullDependency.call(this);
|
||||
this.name = name;
|
||||
this.pos = pos;
|
||||
}
|
||||
module.exports = LabeledExportsDependency;
|
||||
|
||||
LabeledExportsDependency.prototype = Object.create(NullDependency.prototype);
|
||||
LabeledExportsDependency.prototype.constructor = LabeledExportsDependency;
|
||||
|
||||
LabeledExportsDependency.Template = function LabeledExportsDependencyTemplate() {};
|
||||
|
||||
LabeledExportsDependency.Template.prototype.apply = function(dep, source) {
|
||||
source.insert(dep.pos, "exports[" + JSON.stringify(dep.name) + "] = ");
|
||||
};
|
|
@ -1,36 +0,0 @@
|
|||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
var ModuleDependency = require("./ModuleDependency");
|
||||
var Template = require("../Template");
|
||||
|
||||
function LabeledModuleDependency(request, range) {
|
||||
ModuleDependency.call(this, request);
|
||||
this.range = range;
|
||||
}
|
||||
module.exports = LabeledModuleDependency;
|
||||
|
||||
LabeledModuleDependency.prototype = Object.create(ModuleDependency.prototype);
|
||||
LabeledModuleDependency.prototype.constructor = LabeledModuleDependency;
|
||||
LabeledModuleDependency.prototype.type = "labeled require";
|
||||
|
||||
LabeledModuleDependency.Template = function LabeledModuleDependencyTemplate() {};
|
||||
|
||||
LabeledModuleDependency.Template.prototype.apply = function(dep, source, outputOptions, requestShortener) {
|
||||
var comment = "",
|
||||
content;
|
||||
if(outputOptions.pathinfo) comment = "/*! " + requestShortener.shorten(dep.request) + " */ ";
|
||||
if(dep.module && dep.module.meta && dep.module.meta.exports) {
|
||||
content = "var __WEBPACK_LABELED_MODULE__" + Template.toIdentifier(dep.module.id) + " = __webpack_require__(" + comment + JSON.stringify(dep.module.id) + ")";
|
||||
dep.module.meta.exports.forEach(function(e) {
|
||||
content += ", " + e + " = __WEBPACK_LABELED_MODULE__" + Template.toIdentifier(dep.module.id) + "." + e;
|
||||
});
|
||||
content += ";";
|
||||
} else if(dep.module) {
|
||||
content = require("./WebpackMissingModule").moduleMetaInfo(dep.request);
|
||||
} else {
|
||||
content = require("./WebpackMissingModule").module(dep.request);
|
||||
}
|
||||
source.replace(dep.range[0], dep.range[1] - 1, content);
|
||||
};
|
|
@ -1,78 +0,0 @@
|
|||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
var AbstractPlugin = require("../AbstractPlugin");
|
||||
var LabeledModuleDependency = require("./LabeledModuleDependency");
|
||||
var LabeledExportsDependency = require("./LabeledExportsDependency");
|
||||
|
||||
module.exports = AbstractPlugin.create({
|
||||
"label require": function(stmt) {
|
||||
if(stmt.body.type !== "ExpressionStatement") return;
|
||||
switch(stmt.body.expression.type) {
|
||||
case "Literal":
|
||||
var param = this.evaluateExpression(stmt.body.expression);
|
||||
return this.applyPluginsBailResult("label require:item", stmt, param);
|
||||
case "SequenceExpression":
|
||||
stmt.body.expression.expressions.forEach(function(expression) {
|
||||
var param = this.evaluateExpression(expression);
|
||||
return this.applyPluginsBailResult("label require:item", stmt, param);
|
||||
}, this);
|
||||
return true;
|
||||
}
|
||||
},
|
||||
"label require:item": function(stmt, param) {
|
||||
if(param.isString()) {
|
||||
var dep = new LabeledModuleDependency(param.string, stmt.range);
|
||||
dep.loc = stmt.loc;
|
||||
dep.optional = !!this.scope.inTry;
|
||||
this.state.current.addDependency(dep);
|
||||
return true;
|
||||
}
|
||||
},
|
||||
"label exports": function(stmt) {
|
||||
var name;
|
||||
var dep;
|
||||
switch(stmt.body.type) {
|
||||
case "VariableDeclaration":
|
||||
stmt.body.declarations.forEach(function(decl) {
|
||||
if(!decl.init) return;
|
||||
var dep = new LabeledExportsDependency(decl.id.name, decl.init.range[0]);
|
||||
dep.loc = stmt.loc;
|
||||
this.state.current.addDependency(dep);
|
||||
if(!this.state.module.meta.exports) this.state.module.meta.exports = [];
|
||||
this.state.module.meta.exports.push(decl.id.name);
|
||||
}, this);
|
||||
return true;
|
||||
case "FunctionDeclaration":
|
||||
name = stmt.body.id.name;
|
||||
dep = new LabeledExportsDependency(name, stmt.body.range[0]);
|
||||
dep.loc = stmt.loc;
|
||||
this.state.current.addDependency(dep);
|
||||
if(!this.state.module.meta.exports) this.state.module.meta.exports = [];
|
||||
this.state.module.meta.exports.push(name);
|
||||
return true;
|
||||
case "ExpressionStatement":
|
||||
if(stmt.body.expression.type === "Identifier") {
|
||||
name = stmt.body.expression.name;
|
||||
dep = new LabeledExportsDependency(name, stmt.body.expression.range[0]);
|
||||
dep.loc = stmt.loc;
|
||||
this.state.current.addDependency(dep);
|
||||
if(!this.state.module.meta.exports) this.state.module.meta.exports = [];
|
||||
this.state.module.meta.exports.push(name);
|
||||
return true;
|
||||
} else if(stmt.body.expression.type === "SequenceExpression") {
|
||||
stmt.body.expression.expressions.forEach(function(e) {
|
||||
if(e.type !== "Identifier") return;
|
||||
name = e.name;
|
||||
dep = new LabeledExportsDependency(name, e.range[0]);
|
||||
dep.loc = stmt.loc;
|
||||
this.state.current.addDependency(dep);
|
||||
if(!this.state.module.meta.exports) this.state.module.meta.exports = [];
|
||||
this.state.module.meta.exports.push(name);
|
||||
}, this);
|
||||
return true;
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
|
@ -1,26 +0,0 @@
|
|||
/*
|
||||
MIT License http://www.opensource.org/licenses/mit-license.php
|
||||
Author Tobias Koppers @sokra
|
||||
*/
|
||||
var LabeledModuleDependency = require("./LabeledModuleDependency");
|
||||
var LabeledExportsDependency = require("./LabeledExportsDependency");
|
||||
|
||||
var NullFactory = require("../NullFactory");
|
||||
|
||||
var LabeledModuleDependencyParserPlugin = require("./LabeledModuleDependencyParserPlugin");
|
||||
|
||||
function LabeledModulesPlugin() {}
|
||||
module.exports = LabeledModulesPlugin;
|
||||
|
||||
LabeledModulesPlugin.prototype.apply = function(compiler) {
|
||||
compiler.plugin("compilation", function(compilation, params) {
|
||||
var normalModuleFactory = params.normalModuleFactory;
|
||||
|
||||
compilation.dependencyFactories.set(LabeledModuleDependency, normalModuleFactory);
|
||||
compilation.dependencyTemplates.set(LabeledModuleDependency, new LabeledModuleDependency.Template());
|
||||
|
||||
compilation.dependencyFactories.set(LabeledExportsDependency, new NullFactory());
|
||||
compilation.dependencyTemplates.set(LabeledExportsDependency, new LabeledExportsDependency.Template());
|
||||
});
|
||||
compiler.parser.apply(new LabeledModuleDependencyParserPlugin());
|
||||
};
|
|
@ -107,5 +107,4 @@ exportPlugins(exports.optimize = {}, "./optimize", [
|
|||
"UglifyJsPlugin"
|
||||
]);
|
||||
exportPlugins(exports.dependencies = {}, "./dependencies", [
|
||||
"LabeledModulesPlugin"
|
||||
]);
|
||||
|
|
|
@ -160,7 +160,6 @@ describe("TestCases", function() {
|
|||
}]
|
||||
},
|
||||
plugins: (config.plugins || []).concat(
|
||||
new webpack.dependencies.LabeledModulesPlugin(),
|
||||
function() {
|
||||
this.plugin("compilation", function(compilation) {
|
||||
["optimize", "optimize-modules-basic", "optimize-chunks-basic", "after-optimize-tree", "after-optimize-assets"].forEach(function(hook) {
|
||||
|
|
|
@ -34,10 +34,10 @@ var library1 = cp.spawn("node", join(["../../bin/webpack.js", "--output-pathinfo
|
|||
bindOutput(library1);
|
||||
library1.on("exit", function(code) {
|
||||
if(code === 0) {
|
||||
// node ../../bin/webpack --output-pathinfo --color --resolve-alias vm=vm-browserify --output-public-path js/ --output-chunk-filename [name].web.js --module-bind json --module-bind css=style!css --module-bind less=style!css!less --module-bind coffee --module-bind jade --prefetch ./lib/stylesheet.less --optimize-dedupe --labeled-modules ./lib/index "js/web.js?h=[hash]"
|
||||
// node ../../bin/webpack --output-pathinfo --color --resolve-alias vm=vm-browserify --output-public-path js/ --output-chunk-filename [name].web.js --module-bind json --module-bind css=style!css --module-bind less=style!css!less --module-bind coffee --module-bind jade --prefetch ./lib/stylesheet.less ./lib/index "js/web.js?h=[hash]"
|
||||
var main = cp.spawn("node", join(["../../bin/webpack.js", "--output-pathinfo", "--color", "--resolve-alias", "vm=vm-browserify",
|
||||
"--output-public-path", "js/", "--output-chunk-filename", "[name].web.js",
|
||||
"--module-bind", "json", "--module-bind", "css=style!css", "--module-bind", "less=style/url!file?postfix=.css&string!less", "--module-bind", "coffee", "--module-bind", "jade", "--prefetch", "./lib/stylesheet.less", "--optimize-dedupe", "--labeled-modules", "./lib/index", "js/web.js?h=[hash]", "--progress"], extraArgs));
|
||||
"--module-bind", "json", "--module-bind", "css=style!css", "--module-bind", "less=style/url!file?postfix=.css&string!less", "--module-bind", "coffee", "--module-bind", "jade", "--prefetch", "./lib/stylesheet.less", "./lib/index", "js/web.js?h=[hash]", "--progress"], extraArgs));
|
||||
bindOutput(main);
|
||||
}
|
||||
});
|
||||
|
|
|
@ -1,9 +0,0 @@
|
|||
it("should parse and evaluate labeled modules", function() {
|
||||
var lbm = require("./labeledModuleA");
|
||||
lbm.should.have.property("x").be.eql("x");
|
||||
lbm.should.have.property("y").have.type("function");
|
||||
lbm.y().should.be.eql("y");
|
||||
lbm.should.have.property("z").be.eql("z");
|
||||
lbm.should.have.property("foo").have.type("function");
|
||||
lbm.foo().should.be.eql("foo");
|
||||
});
|
|
@ -1,5 +0,0 @@
|
|||
require: "./labeledModuleB";
|
||||
|
||||
exports: x, y, z;
|
||||
|
||||
exports: function foo(){ return "foo"; };
|
|
@ -1,4 +0,0 @@
|
|||
exports: var x = "x", y = function() { return "y"; };
|
||||
|
||||
var z = "z";
|
||||
exports: z;
|
Loading…
Reference in New Issue