<router-view/>
基于 glob 库,获得正确的 js 入口javascript
// 得到入口 js 文件 let entries = getEntry('./src/pages/**/*.js', './src/pages/'); // getEntry 方法 function getEntry(globPath, pathDir) { let files = glob.sync(globPath) let entries = {}, entry, dirname, basename, pathname, extname for (let i = 0; i < files.length; i++) { entry = files[i] dirname = path.dirname(entry) extname = path.extname(entry) basename = path.basename(entry, extname) pathname = path.normalize(path.join(dirname, basename)) pathDir = path.normalize(pathDir) if (pathname.startsWith(pathDir)) { pathname = pathname.substring(pathDir.length) } entries[pathname] = ['./' + entry] } return entries }
获取对应 html, 配置 htmlcss
// 获取对应 html let pages = Object.keys(getEntry('./src/pages/**/*.html', './src/pages/')) // 利用 HtmlWebpackPlugin 配置 html pages.forEach(function (pathname) { // 配置生成的 html 文件,定义路径等,可根据最终打包的文件要求进行修改 let page = pathname if (pathname.search('/') != -1) { page = pathname.split('/').pop() } // config 对象 let config = { // html 名字 The file to write the HTML to. Defaults to index.html filename: page + '.html', // 模板路径 // html-withimg-loader! 处理 html,以支持直接在html中使用img的src加载图片 template: 'html-withimg-loader!' + 'src/pages/' + pathname + '.html', // js 插入位置 When passing true or 'body' all javascript resources will be placed at the bottom of the body element inject: true, // html 压缩处理 minify: { // removeComments 移除页面注释,默认为true removeComments: true, //collapseWhitespace 移除空格、回车、换行符等符号,默认为true collapseWhitespace: false } // favicon: 'path/to/yourfile.ico' }; if (pathname in module.exports.entry) { // chunks 默认会在生成的 html 文件中引用全部的 js 文件,固然你也能够指定引入哪些特定的文件 // vendors 为第三方库,common 为公共的模块部分,pathname 和 entry 对应 config.chunks = ['common', pathname]; // 给生成的 js 文件一个独特的 hash 值,如 <script type=text/javascript src=bundle.js?22b9692e22e7be37b57e></script> config.hash = false; } // 在遍历中配置 (须要生成几个 html 文件,就配置几个 HtmlWebpackPlugin 对象) module.exports.plugins.push(new htmlWebpackPlugin(config)); });