Skip to content

Commit 378ca2c

Browse files
authored
update to webpack4 (#889)
1 parent 1df59cc commit 378ca2c

12 files changed

+284
-248
lines changed

Diff for: README.md

+3
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,9 @@ npm run build:prod
151151
# --report to build with bundle size analytics
152152
npm run build:prod --report
153153

154+
# --generate a bundle size analytics. default: bundle-report.html
155+
npm run build:prod --generate_report
156+
154157
# --preview to start a server in local to preview
155158
npm run build:prod --preview
156159

Diff for: README.zh-CN.md

+4-1
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,10 @@ npm run build:prod
162162
## 其它
163163
```bash
164164
# --report to build with bundle size analytics
165-
npm run build:prod --report
165+
npm run build:prod
166+
167+
# --generate a bundle size analytics. default: bundle-report.html
168+
npm run build:prod --generate_report
166169

167170
# --preview to start a server in local to preview
168171
npm run build:prod --preview

Diff for: build/build.js

+31-20
Original file line numberDiff line numberDiff line change
@@ -8,49 +8,60 @@ const chalk = require('chalk')
88
const webpack = require('webpack')
99
const config = require('../config')
1010
const webpackConfig = require('./webpack.prod.conf')
11-
var connect = require('connect');
11+
var connect = require('connect')
1212
var serveStatic = require('serve-static')
1313

14-
const spinner = ora('building for ' + process.env.env_config + ' environment...')
14+
const spinner = ora(
15+
'building for ' + process.env.env_config + ' environment...'
16+
)
1517
spinner.start()
1618

1719
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
1820
if (err) throw err
1921
webpack(webpackConfig, (err, stats) => {
2022
spinner.stop()
2123
if (err) throw err
22-
process.stdout.write(stats.toString({
23-
colors: true,
24-
modules: false,
25-
children: false,
26-
chunks: false,
27-
chunkModules: false
28-
}) + '\n\n')
24+
process.stdout.write(
25+
stats.toString({
26+
colors: true,
27+
modules: false,
28+
children: false,
29+
chunks: false,
30+
chunkModules: false
31+
}) + '\n\n'
32+
)
2933

3034
if (stats.hasErrors()) {
3135
console.log(chalk.red(' Build failed with errors.\n'))
3236
process.exit(1)
3337
}
3438

3539
console.log(chalk.cyan(' Build complete.\n'))
36-
console.log(chalk.yellow(
37-
' Tip: built files are meant to be served over an HTTP server.\n' +
38-
' Opening index.html over file:// won\'t work.\n'
39-
))
40+
console.log(
41+
chalk.yellow(
42+
' Tip: built files are meant to be served over an HTTP server.\n' +
43+
" Opening index.html over file:// won't work.\n"
44+
)
45+
)
4046

4147
if (process.env.npm_config_preview) {
4248
const port = 9526
43-
const host = "http://localhost:" + port
49+
const host = 'http://localhost:' + port
4450
const basePath = config.build.assetsPublicPath
4551
const app = connect()
4652

47-
app.use(basePath, serveStatic('./dist', {
48-
'index': ['index.html', '/']
49-
}))
53+
app.use(
54+
basePath,
55+
serveStatic('./dist', {
56+
index: ['index.html', '/']
57+
})
58+
)
5059

51-
app.listen(port, function () {
52-
console.log(chalk.green(`> Listening at http://localhost:${port}${basePath}`))
53-
});
60+
app.listen(port, function() {
61+
console.log(
62+
chalk.green(`> Listening at http://localhost:${port}${basePath}`)
63+
)
64+
})
5465
}
5566
})
5667
})

Diff for: build/check-versions.js

+17-7
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,11 @@ const semver = require('semver')
44
const packageConfig = require('../package.json')
55
const shell = require('shelljs')
66

7-
function exec (cmd) {
8-
return require('child_process').execSync(cmd).toString().trim()
7+
function exec(cmd) {
8+
return require('child_process')
9+
.execSync(cmd)
10+
.toString()
11+
.trim()
912
}
1013

1114
const versionRequirements = [
@@ -24,23 +27,30 @@ if (shell.which('npm')) {
2427
})
2528
}
2629

27-
module.exports = function () {
30+
module.exports = function() {
2831
const warnings = []
2932

3033
for (let i = 0; i < versionRequirements.length; i++) {
3134
const mod = versionRequirements[i]
3235

3336
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
34-
warnings.push(mod.name + ': ' +
35-
chalk.red(mod.currentVersion) + ' should be ' +
36-
chalk.green(mod.versionRequirement)
37+
warnings.push(
38+
mod.name +
39+
': ' +
40+
chalk.red(mod.currentVersion) +
41+
' should be ' +
42+
chalk.green(mod.versionRequirement)
3743
)
3844
}
3945
}
4046

4147
if (warnings.length) {
4248
console.log('')
43-
console.log(chalk.yellow('To use this template, you must update following to modules:'))
49+
console.log(
50+
chalk.yellow(
51+
'To use this template, you must update following to modules:'
52+
)
53+
)
4454
console.log()
4555

4656
for (let i = 0; i < warnings.length; i++) {

Diff for: build/utils.js

+28-21
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
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

7-
exports.assetsPath = function (_path) {
8-
const assetsSubDirectory = process.env.NODE_ENV === 'production'
9-
? config.build.assetsSubDirectory
10-
: config.dev.assetsSubDirectory
7+
exports.assetsPath = function(_path) {
8+
const assetsSubDirectory =
9+
process.env.NODE_ENV === 'production'
10+
? config.build.assetsSubDirectory
11+
: config.dev.assetsSubDirectory
1112

1213
return path.posix.join(assetsSubDirectory, _path)
1314
}
1415

15-
exports.cssLoaders = function (options) {
16+
exports.cssLoaders = function(options) {
1617
options = options || {}
1718

1819
const cssLoader = {
@@ -30,8 +31,22 @@ exports.cssLoaders = function (options) {
3031
}
3132

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

3651
if (loader) {
3752
loaders.push({
@@ -42,32 +57,24 @@ exports.cssLoaders = function (options) {
4257
})
4358
}
4459

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-
}
60+
return loaders
5561
}
56-
5762
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
5863
return {
5964
css: generateLoaders(),
6065
postcss: generateLoaders(),
6166
less: generateLoaders('less'),
62-
sass: generateLoaders('sass', { indentedSyntax: true }),
67+
sass: generateLoaders('sass', {
68+
indentedSyntax: true
69+
}),
6370
scss: generateLoaders('sass'),
6471
stylus: generateLoaders('stylus'),
6572
styl: generateLoaders('stylus')
6673
}
6774
}
6875

6976
// Generate loaders for standalone style files (outside of .vue)
70-
exports.styleLoaders = function (options) {
77+
exports.styleLoaders = function(options) {
7178
const output = []
7279
const loaders = exports.cssLoaders(options)
7380

Diff for: build/vue-loader.conf.js

+1-18
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,5 @@
11
'use strict'
2-
const utils = require('./utils')
3-
const config = require('../config')
4-
const isProduction = process.env.NODE_ENV === 'production'
5-
const sourceMapEnabled = isProduction
6-
? config.build.productionSourceMap
7-
: config.dev.cssSourceMap
82

93
module.exports = {
10-
loaders: utils.cssLoaders({
11-
sourceMap: sourceMapEnabled,
12-
extract: isProduction
13-
}),
14-
cssSourceMap: sourceMapEnabled,
15-
cacheBusting: config.dev.cacheBusting,
16-
transformToRequire: {
17-
video: ['src', 'poster'],
18-
source: 'src',
19-
img: 'src',
20-
image: 'xlink:href'
21-
}
4+
//You can set the vue-loader configuration by yourself.
225
}

Diff for: build/webpack.base.conf.js

+13-6
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
const path = require('path')
33
const utils = require('./utils')
44
const config = require('../config')
5+
const { VueLoaderPlugin } = require('vue-loader')
56
const vueLoaderConfig = require('./vue-loader.conf')
67

7-
function resolve (dir) {
8+
function resolve(dir) {
89
return path.join(__dirname, '..', dir)
910
}
1011

@@ -27,14 +28,15 @@ module.exports = {
2728
output: {
2829
path: config.build.assetsRoot,
2930
filename: '[name].js',
30-
publicPath: process.env.NODE_ENV === 'production'
31-
? config.build.assetsPublicPath
32-
: config.dev.assetsPublicPath
31+
publicPath:
32+
process.env.NODE_ENV === 'production'
33+
? config.build.assetsPublicPath
34+
: config.dev.assetsPublicPath
3335
},
3436
resolve: {
3537
extensions: ['.js', '.vue', '.json'],
3638
alias: {
37-
'@': resolve('src'),
39+
'@': resolve('src')
3840
}
3941
},
4042
module: {
@@ -48,7 +50,11 @@ module.exports = {
4850
{
4951
test: /\.js$/,
5052
loader: 'babel-loader?cacheDirectory',
51-
include: [resolve('src'), resolve('test'), resolve('node_modules/webpack-dev-server/client')]
53+
include: [
54+
resolve('src'),
55+
resolve('test'),
56+
resolve('node_modules/webpack-dev-server/client')
57+
]
5258
},
5359
{
5460
test: /\.svg$/,
@@ -85,6 +91,7 @@ module.exports = {
8591
}
8692
]
8793
},
94+
plugins: [new VueLoaderPlugin()],
8895
node: {
8996
// prevent webpack from injecting useless setImmediate polyfill because Vue
9097
// source contains it (although only uses it if it's native).

Diff for: build/webpack.dev.conf.js

+22-14
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,20 @@ const HtmlWebpackPlugin = require('html-webpack-plugin')
99
const FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
1010
const portfinder = require('portfinder')
1111

12-
function resolve (dir) {
12+
function resolve(dir) {
1313
return path.join(__dirname, '..', dir)
1414
}
1515

1616
const HOST = process.env.HOST
1717
const PORT = process.env.PORT && Number(process.env.PORT)
1818

1919
const devWebpackConfig = merge(baseWebpackConfig, {
20+
mode: 'development',
2021
module: {
21-
rules: utils.styleLoaders({ sourceMap: config.dev.cssSourceMap, usePostCSS: true })
22+
rules: utils.styleLoaders({
23+
sourceMap: config.dev.cssSourceMap,
24+
usePostCSS: true
25+
})
2226
},
2327
// cheap-module-eval-source-map is faster for development
2428
devtool: config.dev.devtool,
@@ -39,16 +43,14 @@ const devWebpackConfig = merge(baseWebpackConfig, {
3943
proxy: config.dev.proxyTable,
4044
quiet: true, // necessary for FriendlyErrorsPlugin
4145
watchOptions: {
42-
poll: config.dev.poll,
46+
poll: config.dev.poll
4347
}
4448
},
4549
plugins: [
4650
new webpack.DefinePlugin({
4751
'process.env': require('../config/dev.env')
4852
}),
4953
new webpack.HotModuleReplacementPlugin(),
50-
new webpack.NamedModulesPlugin(), // HMR shows correct file names in console on update.
51-
new webpack.NoEmitOnErrorsPlugin(),
5254
// https://github.com/ampedandwired/html-webpack-plugin
5355
new HtmlWebpackPlugin({
5456
filename: 'index.html',
@@ -57,7 +59,7 @@ const devWebpackConfig = merge(baseWebpackConfig, {
5759
favicon: resolve('favicon.ico'),
5860
title: 'vue-element-admin',
5961
path: config.dev.assetsPublicPath + config.dev.assetsSubDirectory
60-
}),
62+
})
6163
]
6264
})
6365

@@ -73,14 +75,20 @@ module.exports = new Promise((resolve, reject) => {
7375
devWebpackConfig.devServer.port = port
7476

7577
// Add FriendlyErrorsPlugin
76-
devWebpackConfig.plugins.push(new FriendlyErrorsPlugin({
77-
compilationSuccessInfo: {
78-
messages: [`Your application is running here: http://${devWebpackConfig.devServer.host}:${port}`],
79-
},
80-
onErrors: config.dev.notifyOnErrors
81-
? utils.createNotifierCallback()
82-
: undefined
83-
}))
78+
devWebpackConfig.plugins.push(
79+
new FriendlyErrorsPlugin({
80+
compilationSuccessInfo: {
81+
messages: [
82+
`Your application is running here: http://${
83+
devWebpackConfig.devServer.host
84+
}:${port}`
85+
]
86+
},
87+
onErrors: config.dev.notifyOnErrors
88+
? utils.createNotifierCallback()
89+
: undefined
90+
})
91+
)
8492

8593
resolve(devWebpackConfig)
8694
}

0 commit comments

Comments
 (0)