一直以來,對於webpack<=3來進行構建,仍是比較駕輕就熟的,然而隨時技術的更新,webpack4出來了,緊隨步伐,那就開始寫一個webpack4的構建腳手架吧, 因而...開始了基於vue的webpack4單頁構建腳手架的開發了。
Entry:入口,Webpack的入口文件,能夠爲String,Array,Object。
Output:出口,輸出的最終結果,裏面能夠配置path、publicPath、filename、chunkFilenamet等。
Module:模塊,在 Webpack 裏一切皆模塊,配置處理各類文件所須要的loader。
optimization:主要是作webpack3當中commonchunk所作的事件,可實現代碼分離、提取公共模塊等,配置更靈活也是最爲複雜的一部分。
Plugin:包括內置插件及第三方插件,直接對整個構建過程起做用。css
├── build
│ ├── config.js
│ ├── webpack.base.conf.js
│ ├── webpack.dev.conf.js
│ ├── webpack.prod.conf.js
│ ├── webpack.vendor.conf.js
├── src
│ ├── components
│ ├── router
│ ├── assets
│ ├── App.vue
│ ├── main.js
│ ├── index.html
│ ├── pages
│ │ ├── accRentIncome
│ │ │ ├── images
│ │ │ ├── App.vue
│ │ │ └── index.css
│ │ ├── login
│ │ │ ├── images
│ │ │ ├── App.vue
│ │ │ └── index.csshtml
entry: { main: [ path.resolve(__dirname, '../src/main.js'), ], }
output: { path: path.resolve("./dist"), filename: '[name].[hash:10].js', chunkFilename: '[name].chunk.js' }
module.exports = {
outputBase: './dist',
port: 8091,
vendor: {vue
path: './node_modules/__vue-vendor-bundle__', modules: [ 'babel-polyfill', 'axios', 'vue', 'vuex', 'vue-router', ]
},
mock: {node
contentBase: './mock', port: 8092
}
}webpack
開發模式入口文件加入webpack熱更新ios
entry: { main: [ 'webpack-dev-server/client?/', 'webpack/hot/only-dev-server' ] }, devtool: 'cheap-module-inline-source-map', devServer: { open: false, clientLogLevel: 'warning', publicPath: '/', contentBase: path.resolve(__dirname,'../src'), port: config.port, host: 'localhost', inline: true, historyApiFallback: { index: '/' }, hot: true, compress: true, proxy: config.proxy }
module.exports = { entry: { vendor: config.vendor.modules }, output: { filename: '[name]_dll_[chunkhash:10].js', path: path.resolve(config.vendor.path), library: '[name]_dll_[chunkhash:10]' }, mode: process.env.NODE_ENV, performance: { hints: false }, plugins: [ new webpack.DllPlugin({ context: process.cwd(), path: path.join(config.vendor.path, '[name].json'), name: '[name]_dll_[chunkhash:10]' }), new webpack.HashedModuleIdsPlugin() ] };
module: { rules: [ { test: /\.css$/, exclude: '/node_modules/', use: [ {loader: 'vue-style-loader'}, { loader: `css-loader`, options: { sourceMap: true, minimize: true } }, { loader: 'postcss-loader', options: { plugins: () => [ require('precss')(), require('postcss-cssnext')(), ] } } ] }, { test: /\.vue$/, loader: 'vue-loader', exclude: /node_modules/, options:{ loaders: { css: ['vue-style-loader','css-loader','postcss-loader'] } } }, { test: /\.(?:woff2?|eot|ttf|svg)$/, loader: 'file-loader', options: { name: 'icons/[name].[ext]' } }, { test: /\.(?:png|jpe?g|gif)$/, loaders: [ { loader: 'url-loader', options: { limit: 10000, name: '[name].[ext]' } } ] }, ]
}git
new CleanWebpackPlugin([config.outputBase], { root: path.resolve('./'), verbose: true }),
產出html
new HtmlWebpackPlugin({github
inject: true, title: 'test', template: '../src/index.html', filename: 'index.html', chunksSortMode: 'dependency', minify: { collapseWhitespace: true, removeComments: true, minifyJS: true }
}),web
new OptimizeCssAssetsPlugin(),vue-router
動態連接庫作關聯
new webpack.DllReferencePlugin({
context: process.cwd(), manifest: require(vendorManifestPath)
})
optimization:{
hashedModuleIds:true,
},