Skip to content

Commit 6185597

Browse files
emil14LinusBorg
authored andcommitted
Minor refactoring of build scripts (vuejs-templates#1082)
* build: Replace regular function with the arrow one * check-verstions: Add some line breaks to improve code readability * utils: Minor refactoring Replace 'var' with 'const' operator for 'postcssLoader' defenition; Add some spaces to improve code readability; Rename variable with package.json from 'pkg' to 'packageConfig'; Replace regular function stored in 'createNotifierCallback' with arrow. * vue-loader: Remove extra line break and Add a helpful one * base.conf: Add variable for linting rule to imprive code readability * dev.conf: Update formatting * prod.conf: Update formatting and Use ES6 short syntax for method
1 parent 7ad3590 commit 6185597

7 files changed

+35
-27
lines changed

template/build/build.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ spinner.start()
1616

1717
rm(path.join(config.build.assetsRoot, config.build.assetsSubDirectory), err => {
1818
if (err) throw err
19-
webpack(webpackConfig, function (err, stats) {
19+
webpack(webpackConfig, (err, stats) => {
2020
spinner.stop()
2121
if (err) throw err
2222
process.stdout.write(stats.toString({

template/build/check-versions.js

+5
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ const chalk = require('chalk')
33
const semver = require('semver')
44
const packageConfig = require('../package.json')
55
const shell = require('shelljs')
6+
67
function exec (cmd) {
78
return require('child_process').execSync(cmd).toString().trim()
89
}
@@ -25,8 +26,10 @@ if (shell.which('npm')) {
2526

2627
module.exports = function () {
2728
const warnings = []
29+
2830
for (let i = 0; i < versionRequirements.length; i++) {
2931
const mod = versionRequirements[i]
32+
3033
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
3134
warnings.push(mod.name + ': ' +
3235
chalk.red(mod.currentVersion) + ' should be ' +
@@ -39,10 +42,12 @@ module.exports = function () {
3942
console.log('')
4043
console.log(chalk.yellow('To use this template, you must update following to modules:'))
4144
console.log()
45+
4246
for (let i = 0; i < warnings.length; i++) {
4347
const warning = warnings[i]
4448
console.log(' ' + warning)
4549
}
50+
4651
console.log()
4752
process.exit(1)
4853
}

template/build/utils.js

+11-8
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
const path = require('path')
33
const config = require('../config')
44
const ExtractTextPlugin = require('extract-text-webpack-plugin')
5-
const pkg = require('../package.json')
5+
const packageConfig = require('../package.json')
66

77
exports.assetsPath = function (_path) {
88
const assetsSubDirectory = process.env.NODE_ENV === 'production'
99
? config.build.assetsSubDirectory
1010
: config.dev.assetsSubDirectory
11+
1112
return path.posix.join(assetsSubDirectory, _path)
1213
}
1314

@@ -21,7 +22,7 @@ exports.cssLoaders = function (options) {
2122
}
2223
}
2324

24-
var postcssLoader = {
25+
const postcssLoader = {
2526
loader: 'postcss-loader',
2627
options: {
2728
sourceMap: options.sourceMap
@@ -31,6 +32,7 @@ exports.cssLoaders = function (options) {
3132
// generate loader string to be used with extract text plugin
3233
function generateLoaders (loader, loaderOptions) {
3334
const loaders = options.usePostCSS ? [cssLoader, postcssLoader] : [cssLoader]
35+
3436
if (loader) {
3537
loaders.push({
3638
loader: loader + '-loader',
@@ -68,28 +70,29 @@ exports.cssLoaders = function (options) {
6870
exports.styleLoaders = function (options) {
6971
const output = []
7072
const loaders = exports.cssLoaders(options)
73+
7174
for (const extension in loaders) {
7275
const loader = loaders[extension]
7376
output.push({
7477
test: new RegExp('\\.' + extension + '$'),
7578
use: loader
7679
})
7780
}
81+
7882
return output
7983
}
8084

81-
exports.createNotifierCallback = function () {
85+
exports.createNotifierCallback = () => {
8286
const notifier = require('node-notifier')
8387

8488
return (severity, errors) => {
85-
if (severity !== 'error') {
86-
return
87-
}
88-
const error = errors[0]
89+
if (severity !== 'error') return
8990

91+
const error = errors[0]
9092
const filename = error.file && error.file.split('!').pop()
93+
9194
notifier.notify({
92-
title: pkg.name,
95+
title: packageConfig.name,
9396
message: severity + ': ' + error.name,
9497
subtitle: filename || '',
9598
icon: path.join(__dirname, 'logo.png')

template/build/vue-loader.conf.js

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ const sourceMapEnabled = isProduction
66
? config.build.productionSourceMap
77
: config.dev.cssSourceMap
88

9-
109
module.exports = {
1110
loaders: utils.cssLoaders({
1211
sourceMap: sourceMapEnabled,

template/build/webpack.base.conf.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,17 @@ function resolve (dir) {
88
return path.join(__dirname, '..', dir)
99
}
1010

11+
const lintingRule = {
12+
test: /\.(js|vue)$/,
13+
loader: 'eslint-loader',
14+
enforce: 'pre',
15+
include: [resolve('src'), resolve('test')],
16+
options: {
17+
formatter: require('eslint-friendly-formatter'),
18+
emitWarning: !config.dev.showEslintErrorsInOverlay
19+
}
20+
}
21+
1122
module.exports = {
1223
context: path.resolve(__dirname, '../'),
1324
entry: {
@@ -32,16 +43,7 @@ module.exports = {
3243
module: {
3344
rules: [
3445
{{#lint}}
35-
...(config.dev.useEslint? [{
36-
test: /\.(js|vue)$/,
37-
loader: 'eslint-loader',
38-
enforce: 'pre',
39-
include: [resolve('src'), resolve('test')],
40-
options: {
41-
formatter: require('eslint-friendly-formatter'),
42-
emitWarning: !config.dev.showEslintErrorsInOverlay
43-
}
44-
}] : []),
46+
...(config.dev.useEslint ? [lintingRule] : []),
4547
{{/lint}}
4648
{
4749
test: /\.vue$/,

template/build/webpack.dev.conf.js

+3-4
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@ const devWebpackConfig = merge(baseWebpackConfig, {
2424
host: process.env.HOST || config.dev.host,
2525
port: process.env.PORT || config.dev.port,
2626
open: config.dev.autoOpenBrowser,
27-
overlay: config.dev.errorOverlay ? {
28-
warnings: false,
29-
errors: true,
30-
} : false,
27+
overlay: config.dev.errorOverlay
28+
? { warnings: false, errors: true }
29+
: false,
3130
publicPath: config.dev.assetsPublicPath,
3231
proxy: config.dev.proxyTable,
3332
quiet: true, // necessary for FriendlyErrorsPlugin

template/build/webpack.prod.conf.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ const webpackConfig = merge(baseWebpackConfig, {
5555
// duplicated CSS from different components can be deduped.
5656
new OptimizeCSSPlugin({
5757
cssProcessorOptions: config.build.productionSourceMap
58-
? { safe: true, map: { inline: false } }
59-
: { safe: true }
58+
? { safe: true, map: { inline: false } }
59+
: { safe: true }
6060
}),
6161
// generate dist index.html with correct asset hash for caching.
6262
// you can customize output by editing /index.html
@@ -84,7 +84,7 @@ const webpackConfig = merge(baseWebpackConfig, {
8484
// split vendor js into its own file
8585
new webpack.optimize.CommonsChunkPlugin({
8686
name: 'vendor',
87-
minChunks: function (module) {
87+
minChunks (module) {
8888
// any required modules inside node_modules are extracted to vendor
8989
return (
9090
module.resource &&

0 commit comments

Comments
 (0)