require-ensure和require-amd的区别:javascript
require(dependencies: String[], [callback: function(...)])
require.ensure(dependencies: String[], callback: function([require]), [chunkName: String])
webpack.config.amd.jshtml
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.jsvue
require(["./module1"], function(module1) { console.log("aaa"); var module2 = require("./module2"); console.log("bbb"); });
module1.jsjava
console.log("module1"); module.exports = 1;
module2.jswebpack
console.log("module2"); module.exports = 2;
命令行中运行webpack --config webpack.config.amd.js
- main.bundle.js
- example.amd.js
- 1.chunk.js
- module1.js
- module2.jsgit
浏览器中运行amd/index.html,控制台输出:github
module1 aaa module2 bbb
webpack.config.ensure.jsweb
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.jsvue-router
require.ensure(["./module1"], function(require) { console.log("aaa"); var module2 = require("./module2"); console.log("bbb"); require("./module1"); }, 'test');
module1.js
同上vue-cli
命令行中运行webpack --config webpack.config.ensure.js
- main.bundle.js
- example.amd.js
- 1.chunk.js
- module1.js
- module2.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
8require.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')
module1.js
同上
命令行中运行webpack --config webpack.config.ensure.js
- main.bundle.js
- example.amd.js
- 1.chunk.js
- module1.js
- module2.js
浏览器中运行ensure/index.html,控制台输出:
aaa
module1
bbb
ccc
1module2 ddd
使用 vue-cli构建的项目,在 默认状况下 ,执行 npm run build 会将全部的js代码打包为一个总体,
打包位置是 dist/static/js/app.[contenthash].js
相似下面的路由代码
router/index.js 路由相关信息,该路由文件引入了多个 .vue组件
import Hello from '@/components/Hello'
import Province from '@/components/Province'
import Segment from '@/components/Segment'
import User from '@/components/User'
import Loading from '@/components/Loading'
执行 npm run build 会打包为一个总体 app.[contenthash].js ,这个文件是很是大,可能几兆或者几十兆,加载会很慢
因此咱们须要分模块打包,把咱们想要组合在一块儿的组件打包到一个 chunk块中去
分模块打包须要下面这样使用 webpack的 require.ensure,而且在最后加入一个 chunk名,
相同 chunk名字的模块将会打包到一块儿
router/index.js 修改成懒加载组件
const Province = r => require.ensure([], () => r(require('@/components/Province.vue')), 'chunkname1')
const Segment = r => require.ensure([], () => r(require('@/components/Segment.vue')), 'chunkname1')
const Loading = r => require.ensure([], () => r(require('@/components/Loading.vue')), 'chunkname3')
const User = r => require.ensure([], () => r(require('@/components/User.vue')), 'chunkname3')
根据 chunkame的不一样, 上面的四个组件, 将会被分红3个块打包,最终打包以后与组件相关的js文件会分为3个 (除了app.js,manifest.js, vendor.js)
分模块打包以后在 dist目录下是这样的, 这样就把一个大的 js文件分为一个个小的js文件了,按需去下载,其余的使用方法和import的效果同样
参考vue-router官方文档: https://router.vuejs.org/zh-cn/advanced/lazy-loading.html