# Vue-Multipe-Page-Webpack **Repository Path**: dataup/Vue-Multipe-Page-Webpack ## Basic Information - **Project Name**: Vue-Multipe-Page-Webpack - **Description**: No description available - **Primary Language**: JavaScript - **License**: MIT - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2021-05-10 - **Last Updated**: 2021-05-10 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # multipepage vue init webpack project ## 目录结构 `src` | - `pages` 全局html目录 | - `index` 子html目录 | - `assets` 静态文件 | - `components` 静态文件 | - `router` 子路由 | - `app.vue` 子模板 | - `index.js` 子入口文件 | - `index.html` 子html | - `components` 全局组件 | - `utils` 全局插件 `static` 全局文件 ### 多页面配置 #### `utils` - 增加两组函数 glob是webpack安装时依赖的一个第三方模块,还模块允许你使用 *等符号, 例如lib/*.js就是获取lib文件夹下的所有js后缀名的文件 const glob = require('glob') 页面模板 const HtmlWebpackPlugin = require('html-webpack-plugin') 取得相应的页面路径,因为之前的配置,所以是src文件夹下的pages文件夹 const PAGE_PATH = path.resolve(__dirname, '../src/pages') 用于做相应的merge处理 const merge = require('webpack-merge') ##### 函数1: 多入口配置 通过glob模块读取pages文件夹下的所有对应文件夹下的js后缀文件,如果该文件存在 那么就作为入口处理 exports.entries = function () { var entryFiles = glob.sync(PAGE_PATH + '/*/*.js') var map = {} entryFiles.forEach((filePath) => { var filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.')) map[filename] = filePath }) return map } ##### 函数2: 多页面输出配置 与上面的多页面入口配置相同,读取pages文件夹下的对应的html后缀文件,然后放入数组中 exports.htmlPlugin = function () { let entryHtml = glob.sync(PAGE_PATH + '/*/*.html') let arr = [] entryHtml.forEach((filePath) => { let filename = filePath.substring(filePath.lastIndexOf('\/') + 1, filePath.lastIndexOf('.')) let conf = { // 模板来源 template: filePath, // 文件名称 filename: filename + '.html', // 页面模板需要加对应的js脚本,如果不加这行则每个页面都会引入所有的js脚本 chunks: ['manifest', 'vendor', filename], inject: true } if (process.env.NODE_ENV === 'production') { conf = merge(conf, { minify: { removeComments: true, collapseWhitespace: true, removeAttributeQuotes: true }, chunksSortMode: 'dependency' }) } arr.push(new HtmlWebpackPlugin(conf)) }) return arr } #### `webpack.base.conf` - 修改入口配置 entry: utils.entries() #### `webpack.dev.conf` - 修改开发环境页面输出 去掉plugins -> new HtmlWebpackPlugin() 结尾增加 .concat(utils.htmlPlugin()) 合并输出的配置 #### `webpack.prod.conf` - 修改生产环境页面输出 去掉plugins -> new HtmlWebpackPlugin() 结尾增加 .concat(utils.htmlPlugin()) 合并输出的配置 ### TODO:多页面路由模式不能使用history ### TODO:静态文件ex:img 请使用require引用 ### TODO:页面入口html不能重复