8000 refactor webpack runner · Vue-Alex/vue-cli@6cd53e3 · GitHub
[go: up one dir, main page]

Skip to content

Commit 6cd53e3

Browse files
committed
refactor webpack runner
1 parent 1f0bf9c commit 6cd53e3

File tree

2 files changed

+67
-58
lines changed

2 files changed

+67
-58
lines changed

bin/vue-build

Lines changed: 2 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ var fs = require('fs')
44
var path = require('path')
55
var program = require('commander')
66
var chalk = require('chalk')
7-
var rm = require('rimraf').sync
87
var home = require('user-home')
98
var webpack = require('webpack')
109
var webpackMerge = require('webpack-merge')
@@ -18,7 +17,7 @@ var CopyPlugin = require('copy-webpack-plugin')
1817
var tildify = require('tildify')
1918
var loaders = require('../lib/loaders')
2019
var logger = require('../lib/logger')
21-
var createServer = require('../lib/server')
20+
var run = require('../lib/run')
2221

2322
/**
2423
* Usage.
@@ -301,37 +300,9 @@ if (!options.disableWebpackConfig) {
301300
}
302301
}
303302

304-
try {
305-
var compiler = webpack(webpackConfig)
306-
} catch (err) {
307-
if (err.name === 'WebpackOptionsValidationError') {
308-
logger.fatal(err.message)
309-
} else {
310-
throw err
311-
}
312-
}
313-
314303
checkEntryExists(options.entry)
315304

316-
if (typeof options.run === 'function') {
317-
options.run(webpackConfig, options)
318-
} else if (options.watch) {
319-
console.log('> Running in watch mode')
320-
rm(path.join(options.dist, '*'))
321-
compiler.watch({}, (err, stats) => handleBuild(err, stats, true))
322-
} else if (production) {
323-
console.log('> Creating an optimized production build:\n')
324-
// remove dist files but keep that folder in production mode
325-
rm(path.join(options.dist, '*'))
326-
compiler.run(handleBuild)
327-
} else {
328-
var server = createServer(compiler, options)
329-
330-
server.listen(options.port, options.host)
331-
if (options.open) {
332-
require('opn')(`http://${options.host}:${options.port}`)
333-
}
334-
}
305+
run(webpackConfig, options)
335306

336307
function checkEntryExists (entry) {
337308
if (!fs.existsSync(entry)) {
@@ -364,33 +335,6 @@ function getLibraryName (fileName) {
364335
return fileName.replace(/[-_.]([\w])/, (_, p1) => p1.toUpperCase())
365336
}
366337

367-
function handleBuild (err, stats, watch) {
368-
if (watch) {
369-
process.stdout.write('\x1Bc')
370-
}
371-
if (err) {
372-
process.exitCode = 1
373-
return console.error(err.stack)
374-
}
375-
if (stats.hasErrors() || stats.hasWarnings()) {
376-
process.exitCode = 1
377-
return console.error(stats.toString('errors-only'))
378-
}
379-
console.log(stats.toString({
380-
chunks: false,
381-
children: false,
382-
modules: false,
383-
colors: true
384-
}))
385-
console.log(`\n${chalk.bgGreen.black(' SUCCESS ')} Compiled successfully.\n`)
386-
if (!watch) {
387-
console.log(`The ${chalk.cyan(options.dist)} folder is ready to be de 6D47 ployed.`)
388-
console.log(`You may also serve it locally with a static server:\n`)
389-
console.log(` ${chalk.yellow('npm')} i -g serve`)
390-
console.log(` ${chalk.yellow('serve')} ${options.dist} --single\n`)
391-
}
392-
}
393-
394338
function getFilenames (options) {
395339
return Object.assign({
396340
js: options.production ? '[name].[chunkhash:8].js' : '[name].js',

lib/run.js

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
var path = require('path')
2+
var chalk = require('chalk')
3+
var rm = require('rimraf').sync
4+
var webpack = require('webpack')
5+
var logger = require('./logger')
6+
var createServer = require('../lib/server')
7+
8+
module.exports = function (webpackConfig, options) {
9+
try {
10+
var compiler = webpack(webpackConfig)
11+
} catch (err) {
12+
if (err.name === 'WebpackOptionsValidationError') {
13+
logger.fatal(err.message)
14+
} else {
15+
throw err
16+
}
17+
}
18+
19+
if (typeof options.run === 'function') {
20+
options.run(webpackConfig, options)
21+
} else if (options.watch) {
22+
console.log('> Running in watch mode')
23+
rm(path.join(options.dist, '*'))
24+
compiler.watch({}, (err, stats) => handleBuild(err, stats, true))
25+
} else if (options.production) {
26+
console.log('> Creating an optimized production build:\n')
27+
// remove dist files but keep that folder in production mode
28+
rm(path.join(options.dist, '*'))
29+
compiler.run(handleBuild)
30+
} else {
31+
var server = createServer(compiler, options)
32+
33+
server.listen(options.port, options.host)
34+
if (options.open) {
35+
require('opn')(`http://${options.host}:${options.port}`)
36+
}
37+
}
38+
39+
function handleBuild (err, stats, watch) {
40+
if (watch) {
41+
process.stdout.write('\x1Bc')
42+
}
43+
if (err) {
44+
process.exitCode = 1
45+
return console.error(err.stack)
46+
}
47+
if (stats.hasErrors() || stats.hasWarnings()) {
48+
process.exitCode = 1
49+
return console.error(stats.toString('errors-only'))
50+
}
51+
console.log(stats.toString({
52+
chunks: false,
53+
children: false,
54+
modules: false,
55+
colors: true
56+
}))
57+
console.log(`\n${chalk.bgGreen.black(' SUCCESS ')} Compiled successfully.\n`)
58+
if (!watch) {
59+
console.log(`The ${chalk.cyan(options.dist)} folder is ready to be deployed.`)
60+
console.log(`You may also serve it locally with a static server:\n`)
61+
console.log(` ${chalk.yellow('npm')} i -g serve`)
62+
console.log(` ${chalk.yellow('serve')} ${options.dist} --single\n`)
63+
}
64+
}
65+
}

0 commit comments

Comments
 (0)
0