path = require('path' config = require('../config' utils = require('./utils' webpack = require('webpack' merge = require('webpack-merge' baseWebpackConfig = require('./webpack.base.conf' ExtractTextPlugin = require('extract-text-webpack-plugin' HtmlWebpackPlugin = require('html-webpack-plugin' CleanPlugin = require('clean-webpack-plugin')
glob = require('glob' env =<span style="color: #0000ff">var webpackConfig =<span style="color: #000000"> merge(baseWebpackConfig,{
module: {
loaders: utils.styleLoaders({ sourceMap: config.build.productionSourceMap,extract: <span style="color: #0000ff">true<span style="color: #000000"> })
},devtool: config.build.productionSourceMap ? '#source-map' : <span style="color: #0000ff">false<span style="color: #000000">,output: {
path: config.build.assetsRoot,filename: utils.assetsPath('js/[name].[chunkhash].js'<span style="color: #000000">),chunkFilename: utils.assetsPath('js/[id].[chunkhash].js'<span style="color: #000000">)
},vue: {
loaders: utils.cssLoaders({
sourceMap: config.build.productionSourceMap,extract: <span style="color: #0000ff">true<span style="color: #000000">
})
},plugins: [
<span style="color: #008000">//<span style="color: #008000"> http://vuejs.github.io/vue-loader/workflow/production.html
<span style="color: #0000ff">new<span style="color: #000000"> webpack.DefinePlugin({
'process.env'<span style="color: #000000">: env
}),<span style="color: #0000ff">new<span style="color: #000000"> webpack.optimize.UglifyJsPlugin({
compress: {
warnings: <span style="color: #0000ff">false<span style="color: #000000">
}
}),<span style="color: #0000ff">new CleanPlugin(['../dist']),<span style="color: #008000">//<span style="color: #008000">清空生成目录
<span style="color: #0000ff">new<span style="color: #000000"> webpack.optimize.OccurenceOrderPlugin(),<span style="color: #008000">//<span style="color: #008000"> extract css into its own file
<span style="color: #0000ff">new ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css'<span style="color: #000000">)),<span style="color: #008000">//<span style="color: #008000"> generate dist index.html with correct asset hash for caching.
<span style="color: #008000">//<span style="color: #008000"> you can customize output by editing /index.html
<span style="color: #008000">//<span style="color: #008000"> see https://github.com/ampedandwired/html-webpack-plugin
<span style="color: #008000">//<span style="color: #008000"> split vendor js into its own file
<span style="color: #0000ff">new<span style="color: #000000"> webpack.optimize.CommonsChunkPlugin({
name: 'vendor'<span style="color: #000000">,minChunks: <span style="color: #0000ff">function<span style="color: #000000"> (module,count) {
<span style="color: #008000">//<span style="color: #008000"> any required modules inside node_modules are extracted to vendor
<span style="color: #0000ff">return<span style="color: #000000"> (
module.resource &&
/.js$/.test(module.resource) &&<span style="color: #000000">
module.resource.indexOf(
path.join(__dirname,'../node_modules'<span style="color: #000000">)
) === 0<span style="color: #000000">
)
}
}),<span style="color: #008000">//<span style="color: #008000"> extract webpack runtime and module manifest to its own file in order to
<span style="color: #008000">//<span style="color: #008000"> prevent vendor hash from being updated whenever app bundle is updated
<span style="color: #0000ff">new<span style="color: #000000"> webpack.optimize.CommonsChunkPlugin({
name: 'manifest'<span style="color: #000000">,chunks: ['vendor'<span style="color: #000000">]
})
]
})
<span style="color: #0000ff">if<span style="color: #000000"> (config.build.productionGzip) {
<span style="color: #0000ff">var CompressionWebpackPlugin = require('compression-webpack-plugin'<span style="color: #000000">)
webpackConfig.plugins.push(
<span style="color: #0000ff">new<span style="color: #000000"> CompressionWebpackPlugin({
asset: '[path].gz[query]'<span style="color: #000000">,algorithm: 'gzip'<span style="color: #000000">,test: <span style="color: #0000ff">new<span style="color: #000000"> RegExp(
'.(' +<span style="color: #000000">
config.build.productionGzipExtensions.join('|') +
')$'<span style="color: #000000">
),threshold: 10240<span style="color: #000000">,minRatio: 0.8<span style="color: #000000">
})
)
}
module.exports =<span style="color: #000000"> webpackConfig
<span style="color: #0000ff">function<span style="color: #000000"> getEntry(globPath) {
<span style="color: #0000ff">var entries =<span style="color: #000000"> {},basename,pathname;
<span style="color: #0000ff">if (<span style="color: #0000ff">typeof (globPath) != "object"<span style="color: #000000">) {
globPath =<span style="color: #000000"> [globPath]
}
globPath.forEach((itemPath) =><span style="color: #000000"> {
glob.sync(itemPath).forEach(<span style="color: #0000ff">function<span style="color: #000000"> (entry) {
basename =<span style="color: #000000"> path.basename(entry,path.extname(entry));
<span style="color: #0000ff">if (entry.split('/').length > 4<span style="color: #000000">) {
tmp = entry.split('/').splice(-3<span style="color: #000000">);
pathname = tmp.splice(0,1) + '/' + basename; <span style="color: #008000">//<span style="color: #008000"> 正确输出js和html的路径
entries[pathname] =<span style="color: #000000"> entry;
} <span style="color: #0000ff">else<span style="color: #000000"> {
entries[basename] =<span style="color: #000000"> entry;
}
});
});
<span style="color: #0000ff">return<span style="color: #000000"> entries;
}
<span style="color: #0000ff">var pages = getEntry(['./src/module/*.html','./src/module/*/.html'<span style="color: #000000">]);
<span style="color: #0000ff">for (<span style="color: #0000ff">var pathname <span style="color: #0000ff">in<span style="color: #000000"> pages) {
<span style="color: #008000">//<span style="color: #008000"> 配置生成的html文件,定义路径等
<span style="color: #0000ff">var conf =<span style="color: #000000"> {
filename: pathname + '.html'<span style="color: #000000">,template: pages[pathname],<span style="color: #008000">//<span style="color: #008000"> 模板路径
inject: <span style="color: #0000ff">true,<span style="color: #008000">//<span style="color: #008000"> js插入位置
<span style="color: #008000">//<span style="color: #008000"> necessary to consistently work with multiple chunks via CommonsChunkPlugin
chunksSortMode: 'dependency'<span style="color: #000000">
};
<span style="color: #0000ff">if (pathname <span style="color: #0000ff">in<span style="color: #000000"> module.exports.entry) {
conf.chunks = ['manifest','vendor'<span style="color: #000000">,pathname];
conf.hash = <span style="color: #0000ff">true<span style="color: #000000">;
}
module.exports.plugins.push(<span style="color: #0000ff">new<span style="color: #000000"> HtmlWebpackPlugin(conf));
}
修改index.js
path = require('path'module.exports =<span style="color: #000000"> {
build: {
env: require('./prod.env'<span style="color: #000000">),index: path.resolve(dirname,'../dist/index.html'<span style="color: #000000">),assetsRoot: path.resolve(dirname,'../dist'<span style="color: #000000">),assetsSubDirectory: 'static'<span style="color: #000000">,assetsPublicPath: '../'<span style="color: #000000">,productionSourceMap: <span style="color: #0000ff">true<span style="color: #000000">,<span style="color: #008000">//<span style="color: #008000"> Gzip off by default as many popular static hosts such as
<span style="color: #008000">//<span style="color: #008000"> Surge or Netlify already gzip all static assets for you.
<span style="color: #008000">//<span style="color: #008000"> Before setting to true
,make sure to:
<span style="color: #008000">//<span style="color: #008000"> npm install --save-dev compression-webpack-plugin
productionGzip: <span style="color: #0000ff">false<span style="color: #000000">,productionGzipExtensions: ['js','css'<span style="color: #000000">]
},dev: {
env: require('./dev.env'<span style="color: #000000">),port: 8080<span style="color: #000000">,assetsPublicPath: '/'<span style="color: #000000">,proxyTable: {},<span style="color: #008000">//<span style="color: #008000"> CSS Sourcemaps off by default because relative paths are "buggy"
<span style="color: #008000">//<span style="color: #008000"> with this option,according to the CSS-Loader README
<span style="color: #008000">//<span style="color: #008000"> (https://github.com/webpack/css-loader#sourcemaps)
<span style="color: #008000">//<span style="color: #008000"> In our experience,they generally work as expected,
<span style="color: #008000">//<span style="color: #008000"> just be aware of this issue when enabling this option.
cssSourceMap: <span style="color: #0000ff">false<span style="color: #000000">
}
}