1. 程式人生 > >vue-cli+webpack實現多頁面應用的配置

vue-cli+webpack實現多頁面應用的配置

直接上~~

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; // 正確輸出jshtml的路徑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; // 正確輸出jshtml的路徑
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; // 正確輸出jshtml的路徑
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));
}