//webpack.config.js const path = require('path'), webpack = require('webpack'); const HtmlWebpackPlugin = require('html-webpack-plugin'), ExtractTextPlugin = require("extract-text-webpack-plugin"), CopyWebpackPlugin = require('copy-webpack-plugin'), CleanWebpackPlugin = require('clean-webpack-plugin'); const extractCSS = new ExtractTextPlugin('css/[name]_[contenthash:8].css'); var distPath = path.join(__dirname,'dist'), srcPath = path.join(__dirname,'src'); module.exports = { //入口文件 entry: { common : srcPath + "/pages/common/common.js", index : srcPath + "/pages/index/index.js", ui : srcPath + "/pages/ui/ui.js" }, //輸出目錄 output: { publicPath: '/ptah-ui/dist/', path: distPath, filename: 'js/[name]_[chunkhash:8].js' }, module: { rules: [ { test: /\.css$/, loader: extractCSS.extract([ 'css-loader']) }, { test: /\.less/, loader: extractCSS.extract([ 'css-loader','less-loader']) }, { test: /.art$/, use: [ 'art-template-loader' ] }, { test: /\.(png|jpg|gif)$/, loader : 'file-loader?name=images/[name]_[hash:8].[ext]' }, { test: /\.(htm|html)$/i, loader: 'html-withimg-loader' } ] }, plugins: [ new CleanWebpackPlugin(['dist']), extractCSS, new CopyWebpackPlugin([{ from : srcPath +'/lib', to : distPath + '/lib' }]), new HtmlWebpackPlugin({ template: srcPath + '/pages/index/index.html', chunks: ['index','common'], filename: 'pages/index.html' }), new HtmlWebpackPlugin({ template: srcPath + '/pages/ui/ui.html', chunks: ['ui','common'], filename: 'pages/ui.html' }) ] }
前提條件:已安裝nodejscss
//入口文件 entry: { common : srcPath + "/pages/common/common.js", index : srcPath + "/pages/index/index.js", ui : srcPath + "/pages/ui/ui.js" }, //輸出目錄 output: { publicPath: '/ptah-ui/dist/', path: distPath, filename: 'js/[name]_[chunkhash:8].js' },
注意hash與chunkhash的區別。這樣打包後的文件就自動帶入了md5文件簽名了,能夠解決緩存和增量更新的問題。html
因爲是多頁面,頭部導航是公共的,想經過include簽入進去。node
html-webpack-plugin
html-withimg-loader
extract-text-webpack-plugin
copy-webpack-plugin
(思路:單獨創建一個common.js,裏面引入公共文件,做爲chuck) webpack