@srejs/vue-webpack
Version:
srejs vue技术栈webpack配置工具包
77 lines (76 loc) • 2.81 kB
JavaScript
var _path = require("path");
var _fs = require("fs");
var _common = require("@srejs/common");
var _htmlWebpackPlugin = require("html-webpack-plugin");
var _miniCssExtractPlugin = require("mini-css-extract-plugin");
var _webpackBundleAnalyzer = require("webpack-bundle-analyzer");
var _vueLoader = require("vue-loader");
var entryDir = (0, _common.getEntryDir)();
var rootDir = (0, _common.getRootDir)();
var global_local = "".concat(rootDir, "/index.html");
var favicon_local = "".concat(rootDir, "/favicon.ico");
function loadPluginHtml(page) {
var htmlList = ['index.html', "".concat(page, ".html")];
var template_local;
var exists = htmlList.some(function (file) {
var htmlPath = _path.join(entryDir, "".concat(page, "/").concat(file));
if (_fs.existsSync(htmlPath)) {
template_local = htmlPath;
return true;
}
});
if (exists) {
return template_local;
} else if (_fs.existsSync(global_local)) {
return global_local;
} else {
return _path.join(__dirname, './index.html');
}
}
function getPlugin(entryObj, isServer) {
var pages = Object.keys(entryObj);
var webpackPlugin = [];
!isServer && pages.forEach(function (pathname) {
var entryName = pathname.split('/')[0];
var template_local = loadPluginHtml(entryName);
var conf = {
filename: entryName + '/' + entryName + '.html',
//生成的html存放路径,相对于path
template: template_local,
//html模板路径
favicon: _fs.existsSync(favicon_local) ? favicon_local : '',
title: entryName,
inject: 'body',
//js插入的位置,true/'head'/'body'/false
scriptLoading: 'defer',
hash: false,
//为静态资源生成hash值
chunks: [pathname],
//需要引入的chunk,不配置就会引入所有页面的资源
minify: {
minifyCSS: (0, _common.isDev)() ? false : true,
minifyJS: (0, _common.isDev)() ? false : true,
collapseWhitespace: (0, _common.isDev)() ? false : true,
keepClosingSlash: (0, _common.isDev)() ? false : true,
removeComments: false,
removeRedundantAttributes: true,
removeScriptTypeAttributes: true,
removeStyleLinkTypeAttributes: true,
useShortDoctype: true
}
};
webpackPlugin.push(new _htmlWebpackPlugin(conf));
});
webpackPlugin.push(new _vueLoader.VueLoaderPlugin());
!(0, _common.isDev)() && webpackPlugin.push(new _miniCssExtractPlugin({
filename: (0, _common.isDev)() || isServer ? '[name].css' : '[name]_[contenthash:8].css'
}));
if (process.argv.indexOf('--analyzer') > -1 && !isServer) {
!(0, _common.isDev)() && webpackPlugin.push(new _webpackBundleAnalyzer.BundleAnalyzerPlugin());
}
return webpackPlugin;
}
module.exports = {
getPlugin: getPlugin
};
;