Skip to content

Commit 7c839b4

Browse files
puxxustcluventa
authored andcommitted
merge updates of official vuejs webpack template
1 parent fcdd7e1 commit 7c839b4

File tree

11 files changed

+128
-49
lines changed

11 files changed

+128
-49
lines changed

build/build.js

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
// https://github.com/shelljs/shelljs
22
'use strict'
33

4+
require('./check-versions')()
45
require('shelljs/global')
6+
57
env.NODE_ENV = 'production'
68

7-
const path = require('path')
8-
const config = require('../config')
99
const ora = require('ora')
10+
const path = require('path')
11+
const chalk = require('chalk')
1012
const webpack = require('webpack')
13+
const config = require('../config')
1114
const webpackConfig = require('./webpack.prod.conf')
1215

1316
const spinner = ora('building for production...')
@@ -16,7 +19,7 @@ spinner.start()
1619
const assetsPath = path.join(config.build.assetsRoot, config.build.assetsSubDirectory)
1720
rm('-rf', assetsPath)
1821
mkdir('-p', assetsPath)
19-
cp('-R', 'assets/', assetsPath)
22+
cp('-R', 'assets/*', assetsPath)
2023

2124
const compiler = webpack(webpackConfig)
2225
const ProgressPlugin = require('webpack/lib/ProgressPlugin')
@@ -31,5 +34,11 @@ compiler.run((err, stats) => {
3134
children: false,
3235
chunks: false,
3336
chunkModules: false
34-
}) + '\n')
37+
}) + '\n\n')
38+
39+
console.log(chalk.cyan(' Build complete.\n'))
40+
console.log(chalk.yellow(
41+
' Tip: built files are meant to be served over an HTTP server.\n' +
42+
' Opening index.html over file:// won\'t work.\n'
43+
))
3544
})

build/check-versions.js

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
'use strict'
2+
3+
const chalk = require('chalk')
4+
const semver = require('semver')
5+
const packageConfig = require('../package.json')
6+
7+
const exec = (cmd) => {
8+
return require('child_process').execSync(cmd).toString().trim()
9+
}
10+
11+
const versionRequirements = [
12+
{
13+
name: 'node',
14+
currentVersion: semver.clean(process.version),
15+
versionRequirement: packageConfig.engines.node
16+
},
17+
{
18+
name: 'npm',
19+
currentVersion: exec('npm --version'),
20+
versionRequirement: packageConfig.engines.npm
21+
}
22+
]
23+
24+
module.exports = () => {
25+
const warnings = []
26+
for (let i = 0; i < versionRequirements.length; i++) {
27+
const mod = versionRequirements[i]
28+
if (!semver.satisfies(mod.currentVersion, mod.versionRequirement)) {
29+
warnings.push(mod.name + ': ' +
30+
chalk.red(mod.currentVersion) + ' should be ' +
31+
chalk.green(mod.versionRequirement)
32+
)
33+
}
34+
}
35+
36+
if (warnings.length) {
37+
console.log('')
38+
console.log(chalk.yellow('To use this template, you must update following to modules:'))
39+
console.log()
40+
for (let i = 0; i < warnings.length; i++) {
41+
const warning = warnings[i]
42+
console.log(' ' + warning)
43+
}
44+
console.log()
45+
process.exit(1)
46+
}
47+
}

build/dev-server.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
'use strict'
22

3+
require('./check-versions')()
4+
35
const path = require('path')
46
const express = require('express')
57
const webpack = require('webpack')
@@ -12,6 +14,8 @@ const webpackConfig = process.env.NODE_ENV === 'testing'
1214

1315
// default port where dev server listens for incoming traffic
1416
const port = process.env.PORT || config.dev.port
17+
// automatically open browser, if not set will be false
18+
const autoOpenBrowser = Boolean(config.dev.autoOpenBrowser)
1519
// Define HTTP proxies to your custom API backend
1620
// https://github.com/chimurai/http-proxy-middleware
1721
const proxyTable = config.dev.proxyTable
@@ -42,7 +46,7 @@ Object.keys(proxyTable).forEach(context => {
4246
if (typeof options === 'string') {
4347
options = { target: options }
4448
}
45-
app.use(proxyMiddleware(context, options))
49+
app.use(proxyMiddleware(options.filter || context, options))
4650
})
4751

4852
// handle fallback for HTML5 history API
@@ -59,12 +63,20 @@ app.use(hotMiddleware)
5963
const staticPath = path.posix.join(config.dev.assetsPublicPath, config.dev.assetsSubDirectory)
6064
app.use(staticPath, express.static('./assets'))
6165

66+
const uri = 'http://localhost:' + port
67+
68+
devMiddleware.waitUntilValid(() => {
69+
console.log('> Listening at ' + uri + '\n')
70+
})
71+
6272
module.exports = app.listen(port, err => {
6373
if (err) {
6474
console.log(err)
6575
return
6676
}
67-
const uri = 'http://localhost:' + port
68-
console.log('Listening at ' + uri + '\n')
69-
opn(uri)
77+
78+
// when env is testing, don't need open it
79+
if (autoOpenBrowser && process.env.NODE_ENV !== 'testing') {
80+
opn(uri)
81+
}
7082
})

build/utils.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ exports.assetsPath = _path => {
1414
exports.cssLoaders = options => {
1515
options = options || {}
1616
// generate loader string to be used with extract text plugin
17-
function generateLoaders (loaders) {
18-
const sourceLoader = loaders.map(function (loader) {
17+
const generateLoaders = loaders => {
18+
const sourceLoader = loaders.map(loader => {
1919
let extraParamChar
2020
if (/\?/.test(loader)) {
2121
loader = loader.replace(/\?/, '-loader?')
@@ -27,10 +27,12 @@ exports.cssLoaders = options => {
2727
return loader + (options.sourceMap ? extraParamChar + 'sourceMap' : '')
2828
}).join('!')
2929

30+
// Extract CSS when that option is specified
31+
// (which is the case during production build)
3032
if (options.extract) {
3133
return ExtractTextPlugin.extract({
32-
fallbackLoader: 'style-loader',
33-
loader: sourceLoader
34+
use: sourceLoader,
35+
fallback: 'vue-style-loader'
3436
})
3537
} else {
3638
return ['vue-style-loader', sourceLoader].join('!')
@@ -53,7 +55,7 @@ exports.cssLoaders = options => {
5355
exports.styleLoaders = options => {
5456
const output = []
5557
const loaders = exports.cssLoaders(options)
56-
for (const extension in loaders) {
58+
for (const extension of Object.keys(loaders)) {
5759
const loader = loaders[extension]
5860
output.push({
5961
test: new RegExp('\\.' + extension + '$'),

build/vue-loader.conf.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
'use strict'
2+
3+
const utils = require('./utils')
4+
const config = require('../config')
5+
6+
const isProduction = process.env.NODE_ENV === 'production'
7+
8+
module.exports = {
9+
loaders: utils.cssLoaders({
10+
sourceMap: isProduction ? config.build.productionSourceMap : config.dev.cssSourceMap,
11+
extract: isProduction
12+
}),
13+
postcss: [
14+
require('autoprefixer')({
15+
browsers: ['last 3 versions']
16+
})
17+
]
18+
}

build/webpack.base.conf.js

Lines changed: 10 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
11
'use strict'
22

33
const path = require('path')
4-
const webpack = require('webpack')
54
const config = require('../config')
65
const utils = require('./utils')
76
const projectRoot = path.resolve(__dirname, '../')
87

9-
const isProduction = process.env.NODE_ENV === 'production'
10-
118
module.exports = {
129
entry: {
1310
app: ['./client/index.js'],
@@ -17,7 +14,9 @@ module.exports = {
1714
},
1815
output: {
1916
path: config.build.assetsRoot,
20-
publicPath: isProduction ? config.build.assetsPublicPath : config.dev.assetsPublicPath,
17+
publicPath: process.env.NODE_ENV === 'production'
18+
? config.build.assetsPublicPath
19+
: config.dev.assetsPublicPath,
2120
filename: '[name].js'
2221
},
2322
resolve: {
@@ -39,22 +38,19 @@ module.exports = {
3938
module: {
4039
loaders: [
4140
{
42-
test: /\.vue$/,
43-
loader: 'eslint-loader',
44-
include: projectRoot,
45-
exclude: /node_modules/,
46-
enforce: 'pre'
47-
},
48-
{
49-
test: /\.js$/,
41+
test: /\.(js|vue)$/,
5042
loader: 'eslint-loader',
5143
include: projectRoot,
5244
exclude: /node_modules/,
53-
enforce: 'pre'
45+
enforce: 'pre',
46+
options: {
47+
formatter: require('eslint-friendly-formatter')
48+
}
5449
},
5550
{
5651
test: /\.vue$/,
57-
loader: 'vue-loader'
52+
loader: 'vue-loader',
53+
options: require('./vue-loader.conf')
5854
},
5955
{
6056
test: /\.js$/,
@@ -81,21 +77,6 @@ module.exports = {
8177
}
8278
]
8379
},
84-
plugins: [
85-
new webpack.LoaderOptionsPlugin({
86-
vue: {
87-
loaders: utils.cssLoaders({
88-
sourceMap: isProduction,
89-
extract: isProduction
90-
}),
91-
postcss: [
92-
require('autoprefixer')({
93-
browsers: ['last 3 versions']
94-
})
95-
]
96-
}
97-
})
98-
],
9980
// See https://github.com/webpack/webpack/issues/3486
10081
performance: {
10182
hints: false

build/webpack.dev.conf.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ module.exports = merge(baseWebpackConfig, {
2323
'process.env': config.dev.env
2424
}),
2525
// https://github.com/glenjamin/webpack-hot-middleware#installation--usage
26-
new webpack.optimize.OccurrenceOrderPlugin(),
2726
new webpack.HotModuleReplacementPlugin(),
2827
new webpack.NoEmitOnErrorsPlugin(),
2928
// extract vendor chunks for better caching

build/webpack.prod.conf.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ const webpackConfig = merge(baseWebpackConfig, {
4545
comments: false
4646
}
4747
}),
48-
new webpack.optimize.OccurrenceOrderPlugin(),
4948
// extract css into its own file
5049
new ExtractTextPlugin(utils.assetsPath('css/[name].[contenthash].css')),
5150
// generate dist index.html with correct asset hash for caching.

config/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module.exports = {
2121
dev: {
2222
env: require('./dev.env'),
2323
port: process.env.DEV_PORT || 8080,
24+
autoOpenBrowser: true,
2425
assetsSubDirectory: 'assets',
2526
assetsPublicPath: '/',
2627
proxyTable: {},

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,6 +86,7 @@
8686
"electron-devtools-installer": "^2.0.1",
8787
"eslint": "^3.12.2",
8888
"eslint-config-standard": "^6.1.0",
89+
"eslint-friendly-formatter": "^2.0.7",
8990
"eslint-loader": "^1.6.1",
9091
"eslint-plugin-html": "^1.7.0",
9192
"eslint-plugin-promise": "^3.4.0",
@@ -109,7 +110,7 @@
109110
"stylus-loader": "^2.4.0",
110111
"url-loader": "^0.5.7",
111112
"vue-html-loader": "^1.2.3",
112-
"vue-loader": "^10.0.2",
113+
"vue-loader": "^11.0.0",
113114
"vue-template-compiler": "2.1.10",
114115
"webpack": "2.2.0",
115116
"webpack-dev-middleware": "^1.9.0",

0 commit comments

Comments
 (0)