入门客AI创业平台(我带你入门,你带我飞行)
博文笔记

vue-cli+webpack实现多页面应用的配置

创建时间:2017-04-21 投稿人: 浏览次数:2842

直接上~~

webpack.base.conf.js

/**
 * 多页面实现--1引入工具
* */
var glob = require("glob")
/**
* 多页面实现 --2 入口文件配置
* */
var entries = getEntry("./src/module/**/*.js"); // 获得入口js文件function getEntry(globPath) { var entries = {}, basename, tmp, pathname; glob.sync(globPath).forEach(function (entry) { basename = path.basename(entry, path.extname(entry)); tmp = entry.split("/").splice(-3); pathname = tmp.splice(0, 1) + "/" + basename; // 正确输出js和html的路径 entries[pathname] = entry; }); return entries;}
module.exports = {
/**
 * 多页面实现--3   入口文件设置为entries
* */
entry: entries, output: { path: config.build.assetsRoot, filename: "[name].js", publicPath: process.env.NODE_ENV === "production" ? config.build.assetsPublicPath : config.dev.assetsPublicPath },

webpack.dev.conf.js

/**
 * 多页面实现---1 引入工具
* */
var path = require("path");
var glob = require("glob")

module.exports = merge(baseWebpackConfig, {
  module: {
    rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap })
  },
  // cheap-module-eval-source-map is faster for development
  devtool: "#cheap-module-eval-source-map",
  plugins: [
    new webpack.DefinePlugin({
      "process.env": config.dev.env
    }),
    // https://github.com/glenjamin/webpack-hot-middleware#installation--usage
    new webpack.HotModuleReplacementPlugin(),
    new webpack.NoEmitOnErrorsPlugin(),
    // https://github.com/ampedandwired/html-webpack-plugin
    
    /**
     * 多页面实现--2 注释掉HtmlWebpackPlugin
    * */
    /*new HtmlWebpackPlugin({//
      filename: "index.html",
      template: "index.html",
      inject: true
    }),*/

    new FriendlyErrorsPlugin()
  ]
})

/**
 * 多页面实现--3 设置文件
* */

function getEntry(globPath) {
  var entries = {},
    basename, tmp, pathname;

  glob.sync(globPath).forEach(function(entry) {
    basename = path.basename(entry, path.extname(entry));
    tmp = entry.split("/").splice(-3);
    pathname = tmp.splice(0, 1) + "/" + basename; // 正确输出js和html的路径
    entries[pathname] = entry;
  });
  console.log("dev-entrys:");
  console.log(entries);
  return entries;
}
var pages = getEntry("./src/module/**/*.html");
console.log("dev pages----------------------");
for (var pathname in pages) {
  console.log("filename:" + pathname + ".html");
  console.log("template:" + pages[pathname]);
  // 配置生成的html文件,定义路径等
  var conf = {
    filename: pathname + ".html",
    template: pages[pathname], // 模板路径
    minify: { //传递 html-minifier 选项给 minify 输出
      removeComments: true
    },
    inject: "body", // js插入位置
    chunks: [pathname, "vendor", "manifest"] // 每个html引用的js模块,也可以在这里加上vendor等公用模块
  };
  // 需要生成几个html文件,就配置几个HtmlWebpackPlugin对象
  module.exports.plugins.push(new HtmlWebpackPlugin(conf));
}

webpack.prod.conf.js

/**
 * 实现多页面应用的配置
* */
function getEntry(globPath) {
  var entries = {},
    basename, tmp, pathname;
  if (typeof (globPath) != "object") {
    globPath = [globPath]
  }
  globPath.forEach((itemPath) => {
    glob.sync(itemPath).forEach(function (entry) {
      basename = path.basename(entry, path.extname(entry));
      if (entry.split("/").length > 4) {
        tmp = entry.split("/").splice(-3);
        pathname = tmp.splice(0, 1) + "/" + basename; // 正确输出js和html的路径
        entries[pathname] = entry;
      } else {
        entries[basename] = entry;
      }
    });
  });
  return entries;
}

var pages = getEntry(["./src/module/*.html","./src/module/**/*.html"]);

for (var pathname in pages) {
  // 配置生成的html文件,定义路径等
  var conf = {
    filename: pathname + ".html",
    template: pages[pathname],   // 模板路径
    inject: true,              // js插入位置
    // necessary to consistently work with multiple chunks via CommonsChunkPlugin
    chunksSortMode: "dependency"
  };

  if (pathname in module.exports.entry) {
    conf.chunks = ["manifest", "vendor", pathname];
    conf.hash = true;
  }

  module.exports.plugins.push(new HtmlWebpackPlugin(conf));
}



声明:该文观点仅代表作者本人,入门客AI创业平台信息发布平台仅提供信息存储空间服务,如有疑问请联系rumenke@qq.com。
  • 上一篇:没有了
  • 下一篇:没有了
未上传头像