2020-05-14

vue-cli3 配置多页面

多个页面时, 目录结构

  1. ├── src
  2. ├── pages
  3. ├── home
  4. | ├── home.html
  5. | └── home.js
  6. ├── about
  7. | ├── about.html
  8. | └── about.js

vue.config.js 添加

  1. pages: {
  2. home : {
  3. entry: 'src/pages/home/home.js',
  4. template: 'public/home.html',
  5. filename: 'home.html'
  6. },
  7. }

动态配置

  1. pages: getPages(),
  2. // 辅助函数
  3. var path = require("path");
  4. var glob = require("glob");
  5. function getPages() {
  6. var PAGE_PATH = path.resolve(__dirname, "./src/pages");
  7. var entryFiles = glob.sync(PAGE_PATH + "/*/*.js");
  8. var map = {};
  9. entryFiles.forEach((filePath) => {
  10. const filename = filePath.substring(
  11. filePath.lastIndexOf("/") + 1,
  12. filePath.lastIndexOf(".")
  13. );
  14. map[filename] = {
  15. entry: "src/pages/" + filename + "/" + filename + ".js",
  16. template: "public/" + filename + ".html",
  17. filename: filename + ".html",
  18. };
  19. });
  20. return map;
  21. }

vue-cli2配置多页面

借助glob第三方库, 使用shell模式匹配文件。(glob是webpack安装时依赖的一个第三方模块)
cli2中需要在build/webpack.base.conf.js中修改entry

  1. entry entries()
  2. // 辅助函数
  3. function entries () {
  4. const PAGE_PATH = path.resolve(__dirname, './src/pages')
  5. const entryFiles = glob.sync(PAGE_PATH + '/*/*.js')
  6. let map = {};
  7. entryFiles.forEach(filePath => {
  8. const filename = filePath.substring(
  9. filePath.lastIndexOf("/") +1,
  10. filePath.lastIndexOf(".")
  11. )
  12. map[filename] = filePath;
  13. })
  14. return map;
  15. }

分别在build/webpack.dev.conf.js和build/webpack.prod.conf.js中修改plugins

  1. plugins: [].concat(htmls())
  2. // 辅助函数
  3. function htmls () {
  4. const PAGE_PATH = path.resolve(__dirname, './src/pages')
  5. let entryHtml = glob.sync(PAGE_PATH + "/*/*.html");
  6. let arr = [];
  7. entryHtml.forEach(filePath => {
  8. let filename = filePath.substring(
  9. filePath.lastIndexOf("/") + 1,
  10. filePath.lastIndexOf(".")
  11. );
  12. let conf = {
  13. template: filePath, // 模板来源
  14. filename: filename + ".html", // 文件名称
  15. chunks: ["manifest", "vendor", filename], // 页面模板需要加对应的js脚本,如果不加这行则每个页面都会引入所有的js脚本
  16. inject: true
  17. };
  18. if (process.env.NODE_ENV === "production") {
  19. conf = merge(conf, {
  20. minify: {
  21. removeComments: true,
  22. collapseWhitespace: true,
  23. removeAttributeQuotes: true
  24. },
  25. chunksSortMode: "dependency"
  26. });
  27. }
  28. arr.push(new HtmlWebpackPlugin(conf));
  29. });
  30. return arr;
  31. };

webpack配置多页面

webpack项目, entry作为入口, output作为出口, 借助htmlwebpackplugin插件 把静态资源引入到html中。
普通单页面配置:

  1. var HtmlWebpackPlugin = require('html-webpack-plugin');
  2. var path = require('path');
  3. var webpackConfig = {
  4. entry: 'index.js',
  5. output: {
  6. path: path.resolve(__dirname, './dist'),
  7. filename: 'index_bundle.js'
  8. },
  9. plugins: [new HtmlWebpackPlugin({
  10. template: './src/index.html',
  11. filename: 'index.html',
  12. })]
  13. };

配置文件

  1. var webpackConfig = {
  2. entry: {
  3. home: "./pages/home/home.js",
  4. about: "./pages/about/about.js",
  5. },
  6. output: {
  7. path: path.resolve(__dirname, './dist'),
  8. filename: '[name].js'
  9. },
  10. plugins: [new HtmlWebpackPlugin({
  11. template: './src/pages/home/home.html',
  12. filename: 'home.html',
  13. }), new HtmlWebpackPlugin({
  14. template: './src/pages/about/about.html',
  15. filename: 'about.html',
  16. })]
  17. };

参考文献:
https://cli.vuejs.org/zh/config/#pages
https://www.webpackjs.com/configuration/