You can create your own context with the require.context() function.javascript
It allows you to pass in a directory to search, a flag indicating whether subdirectories should be searched too, and a regular expression to match files against.html
webpack parses for require.context() in the code while building.vue
webpack文档 - require.contextjava
require.context是webpack中,用来建立本身的(模块)上下文;
webpack会在代码构建的时候去解析该函数
require.context(directory, useSubdirectories = false, regExp = /^\.\//);
该方法有3个参数:react
// 示例 const test = require.context('./string', false, /\.js$/);
个人目录结构以下:webpack
Stringweb
test正则表达式
这时候若是console.log(test)
,就会发现调用require.context以后返回的是一个函数express
webpackContext(req) { var id = webpackContextResolve(req); return __webpack_require__(id); }
此次若是还须要深刻就须要去webpack打包以后的文件中寻找了:react-router
var map = { "./test/test1.js": "./src/string/test/test1.js", "./trim.js": "./src/string/trim.js", "./trimLeft.js": "./src/string/trimLeft.js", "./trimRight.js": "./src/string/trimRight.js" }; function webpackContext(req) { var id = webpackContextResolve(req); return __webpack_require__(id); } function webpackContextResolve(req) { var id = map[req]; if(!(id + 1)) { // check for number or string var e = new Error("Cannot find module '" + req + "'"); e.code = 'MODULE_NOT_FOUND'; throw e; } return id; } webpackContext.keys = function webpackContextKeys() { return Object.keys(map); }; webpackContext.resolve = webpackContextResolve; module.exports = webpackContext; webpackContext.id = "./src/string sync recursive \\.js$";
由上面的代码能够看出,在webpackContext
定义了多个方法和属性
console.log(webpackContext.id) // "./src/string sync recursive \.js$" console.log(webpackContext('./trim.js')) // "./src/string/trim.js" console.log(webpackContext.keys()) // ["./test/test1.js", "./trim.js", "./trimLeft.js", "./trimRight.js"]
具体就很少说了,直接看文档
vue官方文档 - 基础组件的自动化全局注册
当你的单页应用变成了大型应用后,路由也在慢慢的增多
// rootRoute.js const rootRoute = { childRoutes: [ { path: "/", component: AppLayout, childRoutes: [ { path: "shop", // 购买详情页 component: ShopLayout, childRoutes: [ { path: "foodDetail", component: FoodDetail }, { path: "shoesDetail", component: ShoesDetail } // 其余 ] }, { path: "order", // 订单页 component: Order, childRoutes: [ { path: "remark", //订单备注 component: Remark }, { path: "invoice", //发票抬头 component: Invoice }, { path: "payment", //付款页面 component: Payment }, { path: "userValidation", //用户验证 component: UserValidation }, { path: "chooseAddress", //选择地址 component: ChooseAddress, childRoutes: [ { path: "addAddress", //添加地址 component: AddAddress, childRoutes: [ { path: "searchAddress", //搜索地址 component: SearchAddress } ] } ] } ] } // ... // 大量新增路由 // ... ] } ] };
当路由变的愈来愈大,大到已经难以维护时。咱们按照react-router
提供的思路,对路由按业务模块进行拆分。
// rootRoute.js const rootRoute = { childRoutes: [ { path: '/', component: AppLayout, childRoutes: [ require('./modules/shop/route'), //购买详情页 require('./modules/order/route'), // 订单页 require('./modules/login/route'), // 登陆注册页 require('./modules/service/route'), // 服务中心 // ... // 其余大量新增路由 // ... ] } ] };
再进一步优化的话,就可使用require.context
const rootRoute = { childRoutes: [ { path: '/', component: AppLayout, childRoutes: (r => { return r.keys().map(key => r(key)); })(require.context('./', true, /^\.\/modules\/((?!\/)[\s\S])+\/route\.js$/)) } ] };
好比我如今想要造一个本身的工具库utils,那么随着工具函数数量的增长,势必须要将代码分割得更小,甚至细化到一个工具函数对应一个js文件。
这时若是还须要在入口js文件中一个个手动引用,那么每增长一个js文件,就须要从新去修改入口js一次,工程量是很是大的。
这时就可使用到require.context
了~
/** * @desc webpack打包入口文件 * @example 自动引入子目录下全部js文件 */ let moduleExports = {}; const r = require.context('./', true, /^\.\/.+\/.+\.js$/); r.keys().forEach(key => { let attr = key.substring(key.lastIndexOf('/') + 1, key.lastIndexOf('.')); moduleExports[attr] = r(key); }); module.exports = moduleExports;