We read every piece of feedback, and take your input very seriously.
To see all available qualifiers, see our documentation.
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
本例测试require-ensure和require-amd的区别。
require(dependencies: String[], [callback: function(...)])
require.ensure(dependencies: String[], callback: function([require]), [chunkName: String])
webpack.config.amd.js
var path = require("path"); module.exports = { entry: "./example.amd.js", output: { path: path.join(__dirname, "amd"), filename: "[name].bundle.js", chunkFilename: "[id].chunk.js" } };
example.amd.js
require(["./module1"], function(module1) { console.log("aaa"); var module2 = require("./module2"); console.log("bbb"); });
module1.js
console.log("module1"); module.exports = 1;
module2.js
console.log("module2"); module.exports = 2;
命令行中运行webpack --config webpack.config.amd.js
webpack --config webpack.config.amd.js
浏览器中运行amd/index.html,控制台输出:
module1 aaa module2 bbb
webpack.config.ensure.js
var path = require("path"); module.exports = { entry: "./example.ensure.js", output: { path: path.join(__dirname, "ensure"), filename: "[name].bundle.js", chunkFilename: "[name].chunk.js" } };
example.ensure.js
require.ensure(["./module1"], function(require) { console.log("aaa"); var module2 = require("./module2"); console.log("bbb"); require("./module1"); }, 'test');
module1.js 同上
module2.js 同上
命令行中运行webpack --config webpack.config.ensure.js
webpack --config webpack.config.ensure.js
浏览器中运行ensure/index.html,控制台输出:
aaa module2 bbb module1
webpack.config.ensure.chunk.js
var path = require("path"); module.exports = { entry: "./example.ensur.chunk.js", output: { path: path.join(__dirname, "ensure-chunk"), filename: "[name].bundle.js", chunkFilename: "[name].chunk.js" } };
example.ensur.chunk.js
require.ensure(["./module1"], function(require) { console.log("aaa"); require("./module1"); console.log("bbb"); }, 'common'); require.ensure(["./module2"], function(require) { console.log("ccc"); require("./module2"); console.log("ddd"); }, 'common');
aaa module1 bbb ccc 1module2 ddd
The text was updated successfully, but these errors were encountered:
No branches or pull requests
简介
本例测试require-ensure和require-amd的区别。
require(dependencies: String[], [callback: function(...)])
require.ensure(dependencies: String[], callback: function([require]), [chunkName: String])
示例
require-amd
源代码
webpack.config.amd.js
example.amd.js
module1.js
module2.js
构建结果
命令行中运行
webpack --config webpack.config.amd.js
运行结果
浏览器中运行amd/index.html,控制台输出:
require-ensure
源代码
webpack.config.ensure.js
example.ensure.js
module1.js
同上
module2.js
同上
构建结果
命令行中运行
webpack --config webpack.config.ensure.js
运行结果
浏览器中运行ensure/index.html,控制台输出:
require-ensure-chunk
源代码
webpack.config.ensure.chunk.js
example.ensur.chunk.js
module1.js
同上
module2.js
同上
构建结果
命令行中运行
webpack --config webpack.config.ensure.js
运行结果
浏览器中运行ensure/index.html,控制台输出:
The text was updated successfully, but these errors were encountered: