Skip to content
This repository was archived by the owner on Apr 12, 2022. It is now read-only.

Commit 0fe6f3a

Browse files
tec: Upgrade to Webpack 4
I spent a bit more time to update to Webpack 4. I hope it's finished now... References: - vuejs-templates/webpack#1280 - vuejs-templates/webpack#1369
1 parent 8164a2b commit 0fe6f3a

10 files changed

+1642
-1033
lines changed

client/build/utils.js

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
'use strict'
22
const path = require('path')
33
const config = require('../config')
4-
const ExtractTextPlugin = require('extract-text-webpack-plugin')
4+
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
55
const packageConfig = require('../package.json')
66

77
exports.assetsPath = function (_path) {
@@ -25,13 +25,32 @@ exports.cssLoaders = function (options) {
2525
const postcssLoader = {
2626
loader: 'postcss-loader',
2727
options: {
28-
sourceMap: options.sourceMap
28+
sourceMap: options.sourceMap,
29+
plugins: (loader) => [
30+
require('autoprefixer')({
31+
browsers: ['last 2 versions'],
32+
}),
33+
],
2934
}
3035
}
3136

3237
// generate loader string to be used with extract text plugin
3338
function generateLoaders (loader, loaderOptions) {
34-
const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]
39+
const loaders = []
40+
41+
// Extract CSS when that option is specified
42+
// (which is the case during production build)
43+
if (options.extract) {
44+
loaders.push(MiniCssExtractPlugin.loader)
45+
} else {
46+
loaders.push('vue-style-loader')
47+
}
48+
49+
loaders.push(cssLoader)
50+
51+
if (options.usePostCSS) {
52+
loaders.push(postcssLoader)
53+
}
3554

3655
if (loader) {
3756
loaders.push({
@@ -42,29 +61,16 @@ exports.cssLoaders = function (options) {
4261
})
4362
}
4463

45-
// Extract CSS when that option is specified
46-
// (which is the case during production build)
47-
if (options.extract) {
48-
return ExtractTextPlugin.extract({
49-
use: loaders,
50-
fallback: 'vue-style-loader'
51-
})
52-
} else {
53-
return ['vue-style-loader'].concat(loaders)
54-
}
64+
return loaders
5565
}
5666

5767
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
5868
return {
5969
css: generateLoaders(),
6070
postcss: generateLoaders(),
61-
less: generateLoaders('less'),
62-
sass: generateLoaders('sass', { indentedSyntax: true }),
6371
scss: generateLoaders('sass', {
6472
data: "@import 'src/styles/variables/index';",
6573
}),
66-
stylus: generateLoaders('stylus'),
67-
styl: generateLoaders('stylus')
6874
}
6975
}
7076

client/build/vue-loader.conf.js

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,9 @@
11
'use strict'
2-
const utils = require('./utils')
32
const config = require('../config')
4-
const isProduction = process.env.NODE_ENV === 'production'
5-
const sourceMapEnabled = isProduction
6-
? config.build.productionSourceMap
7-
: config.dev.cssSourceMap
83

94
module.exports = {
10-
loaders: utils.cssLoaders({
11-
sourceMap: sourceMapEnabled,
12-
extract: isProduction
13-
}),
14-
cssSourceMap: sourceMapEnabled,
155
cacheBusting: config.dev.cacheBusting,
16-
transformToRequire: {
6+
transformAssetUrls: {
177
video: ['src', 'poster'],
188
source: 'src',
199
img: 'src',

client/build/webpack.base.conf.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const path = require('path')
33
const utils = require('./utils')
44
const config = require('../config')
55
const vueLoaderConfig = require('./vue-loader.conf')
6+
const { VueLoaderPlugin } = require('vue-loader')
67

78
function resolve (dir) {
89
return path.join(__dirname, '..', dir)
@@ -31,6 +32,9 @@ module.exports = {
3132
? config.build.assetsPublicPath
3233
: config.dev.assetsPublicPath
3334
},
35+
plugins: [
36+
new VueLoaderPlugin(),
37+
],
3438
resolve: {
3539
extensions: ['.js', '.vue', '.json'],
3640
alias: {
@@ -49,7 +53,10 @@ module.exports = {
4953
{
5054
test: /\.js$/,
5155
loader: 'babel-loader',
52-
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
56+
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')],
57+
exclude: file => (
58+
/node_modules/.test(file) && !/\.vue\.js/.test(file)
59+
),
5360
},
5461
{
5562
test: /\.(png|jpe?g|gif|svg)(\?.*)?$/,

client/build/webpack.dev.conf.js

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,12 @@ const HOST = process.env.HOST
1414
const PORT = process.env.PORT && Number(process.env.PORT)
1515

1616
const devWebpackConfig = merge(baseWebpackConfig, {
17+
mode: 'development',
1718
module: {
18-
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
19+
rules: utils.styleLoaders({
20+
sourceMap: config.dev.cssSourceMap,
21+
usePostCSS: true,
22+
})
1923
},
2024
// cheap-module-eval-source-map is faster for development
2125
devtool: config.dev.devtool,
@@ -49,8 +53,6 @@ const devWebpackConfig = merge(baseWebpackConfig, {
4953
'process.env': require('../config/dev.env')
5054
}),
5155
new webpack.HotModuleReplacementPlugin(),
52-
new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
53-
new webpack.NoEmitOnErrorsPlugin(),
5456
// https://github.com/ampedandwired/html-webpack-plugin
5557
new HtmlWebpackPlugin({
5658
filename: 'index.html',
@@ -65,7 +67,10 @@ const devWebpackConfig = merge(baseWebpackConfig, {
6567
ignore: ['.*']
6668
}
6769
])
68-
]
70+
],
71+
optimization: {
72+
noEmitOnErrors: true,
73+
},
6974
})
7075

7176
module.exports = new Promise((resolve, reject) => {

client/build/webpack.prod.conf.js

Lines changed: 33 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const merge = require('webpack-merge')
77
const baseWebpackConfig = require('./webpack.base.conf')
88
const CopyWebpackPlugin = require('copy-webpack-plugin')
99
const HtmlWebpackPlugin = require('html-webpack-plugin')
10-
const ExtractTextPlugin = require('extract-text-webpack-plugin')
10+
const MiniCssExtractPlugin = require('mini-css-extract-plugin')
1111
const OptimizeCSSPlugin = require('optimize-css-assets-webpack-plugin')
1212
const UglifyJsPlugin = require('uglifyjs-webpack-plugin')
1313

@@ -16,6 +16,7 @@ const env = process.env.NODE_ENV === 'testing'
1616
: require('../config/prod.env')
1717

1818
const webpackConfig = merge(baseWebpackConfig, {
19+
mode: 'production',
1920
module: {
2021
rules: utils.styleLoaders({
2122
sourceMap: config.build.productionSourceMap,
@@ -27,30 +28,15 @@ const webpackConfig = merge(baseWebpackConfig, {
2728
output: {
2829
path: config.build.assetsRoot,
2930
filename: utils.assetsPath('js/[name].[chunkhash].js'),
30-
chunkFilename: utils.assetsPath('js/[id].[chunkhash].js')
3131
},
3232
plugins: [
3333
// http://vuejs.github.io/vue-loader/en/workflow/production.html
3434
new webpack.DefinePlugin({
3535
'process.env': env
3636
}),
37-
new UglifyJsPlugin({
38-
uglifyOptions: {
39-
compress: {
40-
warnings: false
41-
}
42-
},
43-
sourceMap: config.build.productionSourceMap,
44-
parallel: true
45-
}),
4637
// extract css into its own file
47-
new ExtractTextPlugin({
48-
filename: utils.assetsPath('css/[name].[contenthash].css'),
49-
// Setting the following option to `false` will not extract CSS from codesplit chunks.
50-
// Their CSS will instead be inserted dynamically with style-loader when the codesplit chunk has been loaded by webpack.
51-
// It's currently set to `true` because we are seeing that sourcemaps are included in the codesplit bundle as well when it's `false`,
52-
// increasing file size: https://github.com/vuejs-templates/webpack/issues/1110
53-
allChunks: true,
38+
new MiniCssExtractPlugin({
39+
filename: utils.assetsPath('css/[name].[chunkhash].css'),
5440
}),
5541
// Compress extracted CSS. We are using this plugin so that possible
5642
// duplicated CSS from different components can be deduped.
@@ -75,42 +61,14 @@ const webpackConfig = merge(baseWebpackConfig, {
7561
// more options:
7662
// https://github.com/kangax/html-minifier#options-quick-reference
7763
},
78-
// necessary to consistently work with multiple chunks via CommonsChunkPlugin
64+
// necessary to consistently work with multiple chunks
7965
chunksSortMode: 'dependency'
8066
}),
67+
68+
new webpack.NamedChunksPlugin(),
69+
8170
// keep module.id stable when vendor modules does not change
8271
new webpack.HashedModuleIdsPlugin(),
83-
// enable scope hoisting
84-
new webpack.optimize.ModuleConcatenationPlugin(),
85-
// split vendor js into its own file
86-
new webpack.optimize.CommonsChunkPlugin({
87-
name: 'vendor',
88-
minChunks (module) {
89-
// any required modules inside node_modules are extracted to vendor
90-
return (
91-
module.resource &&
92-
/\.js$/.test(module.resource) &&
93-
module.resource.indexOf(
94-
path.join(__dirname, '../node_modules')
95-
) === 0
96-
)
97-
}
98-
}),
99-
// extract webpack runtime and module manifest to its own file in order to
100-
// prevent vendor hash from being updated whenever app bundle is updated
101-
new webpack.optimize.CommonsChunkPlugin({
102-
name: 'manifest',
103-
minChunks: Infinity
104-
}),
105-
// This instance extracts shared chunks from code splitted chunks and bundles them
106-
// in a separate chunk, similar to the vendor chunk
107-
// see: https://webpack.js.org/plugins/commons-chunk-plugin/#extra-async-commons-chunk
108-
new webpack.optimize.CommonsChunkPlugin({
109-
name: 'app',
110-
async: 'vendor-async',
111-
children: true,
112-
minChunks: 3
113-
}),
11472

11573
// copy custom static assets
11674
new CopyWebpackPlugin([
@@ -120,7 +78,31 @@ const webpackConfig = merge(baseWebpackConfig, {
12078
ignore: ['.*']
12179
}
12280
])
123-
]
81+
],
82+
optimization: {
83+
splitChunks: {
84+
chunks: 'all',
85+
cacheGroups: {
86+
vendor: {
87+
name: 'vendor',
88+
test: /[\\/]node_modules[\\/]/,
89+
enforce: true,
90+
},
91+
},
92+
},
93+
runtimeChunk: 'single',
94+
minimizer: [
95+
new UglifyJsPlugin({
96+
uglifyOptions: {
97+
compress: {
98+
warnings: false
99+
}
100+
},
101+
sourceMap: config.build.productionSourceMap,
102+
parallel: true
103+
}),
104+
],
105+
},
124106
})
125107

126108
if (config.build.productionGzip) {

0 commit comments

Comments
 (0)