forked from vuejs/vue-cli
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvue-build
executable file
·384 lines (349 loc) · 10.9 KB
/
vue-build
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
#!/usr/bin/env node
var fs = require('fs')
var path = require('path')
var program = require('commander')
var chalk = require('chalk')
var home = require('user-home')
var webpack = require('webpack')
var webpackMerge = require('webpack-merge')
var HtmlWebpackPlugin = require('html-webpack-plugin')
var FriendlyErrorsPlugin = require('friendly-errors-webpack-plugin')
var PostCompilePlugin = require('post-compile-webpack-plugin')
var ProgressPlugin = require('webpack/lib/ProgressPlugin')
var ExtractTextPlugin = require('extract-text-webpack-plugin')
var isYarn = require('installed-by-yarn-globally')
var CopyPlugin = require('copy-webpack-plugin')
var tildify = require('tildify')
var loaders = require('../lib/loaders')
var logger = require('../lib/logger')
var run = require('../lib/run')
/**
* Usage.
*/
program
.usage('[entry]')
.option('--dist [directory]', 'Output directory for bundled files')
.option('--port [port]', 'Server port')
.option('--host [host]', 'Server host', 'localhost')
.option('--prod, --production', 'Build for production')
.option('-w, --watch', 'Run in watch mode')
.option('-m, --mount', 'Auto-create app instance from given component, true for `.vue` file')
.option('-c, --config [file]', 'Use custom config file')
.option('--wp, --webpack [file]', 'Use custom webpack config file')
.option('--disable-config', 'You do not want to use config file')
.option('--disable-webpack-config', 'You do not want to use webpack config file')
.option('-o, --open', 'Open browser')
.option('--proxy [url]', 'Proxy API request')
.option('--lib [libraryName]', 'Distribute component in UMD format')
.option('--disable-compress', 'Disable compress when building in production mode')
.parse(process.argv)
var args = program.args
var production = program.production
process.env.NODE_ENV = production ? 'production' : 'development'
var localConfig
// config option in only available in CLI option
if (!program.disableConfig) {
var localConfigPath = typeof program.config === 'string'
? cwd(program.config)
: path.join(home, '.vue', 'config.js')
var hasLocalConfig = fs.existsSync(localConfigPath)
if (hasLocalConfig) {
console.log(`> Using config file at ${chalk.yellow(tildify(localConfigPath))}`)
localConfig = require(localConfigPath)
}
}
var options = merge({
port: 4000,
dist: 'dist',
host: 'localhost'
}, localConfig, {
entry: args[0],
config: program.config,
port: program.port,
host: program.host,
open: program.open,
dist: program.dist,
webpack: program.webpack,
disableWebpackConfig: program.disableWebpackConfig,
mount: program.mount,
proxy: program.proxy,
production: program.production,
lib: program.lib,
watch: program.watch,
disableCompress: program.disableCompress
})
function help () {
if (!options.config && !options.entry) {
return program.help()
}
}
help()
var cssOptions = {
extract: production,
sourceMap: true
}
var postcssOptions = {
plugins: [
require('autoprefixer')(Object.assign({
browsers: ['ie > 8', 'last 5 versions']
}, options.autoprefixer))
]
}
if (options.postcss) {
if (Object.prototype.toString.call(options.postcss) === '[object Object]') {
var plugins = options.postcss.plugins
if (plugins) {
postcssOptions.plugins = postcssOptions.plugins.concat(plugins)
delete options.postcss.plugins
}
Object.assign(postcssOptions, options.postcss)
} else {
postcssOptions = options.postcss
}
}
var babelOptions = Object.assign({
babelrc: true,
cacheDirectory: true,
sourceMaps: production ? 'both' : false,
presets: []
}, options.babel)
var useBabelRc = babelOptions.babelrc && fs.existsSync('.babelrc')
if (useBabelRc) {
console.log('> Using .babelrc in current working directory')
} else {
babelOptions.babelrc = false
babelOptions.presets.push(require.resolve('babel-preset-vue-app'))
}
var filenames = getFilenames(options)
var webpackConfig = {
entry: {
client: []
},
output: {
path: cwd(options.dist),
filename: filenames.js,
publicPath: '/'
},
performance: {
hints: false
},
resolve: {
extensions: ['.js', '.vue', '.css'],
modules: [
cwd(),
cwd('node_modules'), // modules in cwd's node_modules
ownDir('node_modules') // modules in package's node_modules
],
alias: {}
},
resolveLoader: {
modules: [
cwd('node_modules'), // loaders in cwd's node_modules
ownDir('node_modules') // loaders in package's node_modules
]
},
module: {
rules: [
{
test: /\.js$/,
loader: 'babel-loader',
exclude: [/node_modules/]
},
{
test: /\.es6$/,
loader: 'babel-loader'
},
{
test: /\.vue$/,
loader: 'vue-loader',
options: {
postcss: postcssOptions,
loaders: loaders.cssLoaders(cssOptions)
}
},
{
test: /\.(ico|jpg|png|gif|svg|eot|otf|webp|ttf|woff|woff2)(\?.*)?$/,
loader: 'file-loader',
query: {
name: filenames.static
}
}
].concat(loaders.styleLoaders(cssOptions))
},
plugins: [
new webpack.DefinePlugin({
'process.env.NODE_ENV': JSON.stringify(process.env.NODE_ENV)
}),
new webpack.LoaderOptionsPlugin({
options: {
context: process.cwd(),
postcss: postcssOptions,
babel: babelOptions
}
})
]
}
// copy ./static/** to dist folder
if (fs.existsSync('static')) {
webpackConfig.plugins.push(new CopyPlugin([{
from: 'static',
to: './'
}]))
}
// if entry ends with `.vue` and no `mount` option was specified
// we implicitly set `mount` to true unless `lib` is set
// for `.js` component you can set `mount` to true manually
if (options.mount === undefined && !options.lib && /\.vue$/.test(options.entry)) {
options.mount = true
}
// create a Vue instance to load given component
// set an alias to the path of the component
// otherwise use it directly as webpack entry
if (options.mount) {
webpackConfig.entry.client.push(ownDir('lib/default-entry.es6'))
webpackConfig.resolve.alias['your-tasteful-component'] = cwd(options.entry)
} else if (Array.isArray(options.entry)) {
webpackConfig.entry.client = options.client
} else if (typeof options.entry === 'object') {
webpackConfig.entry = options.entry
} else {
webpackConfig.entry.client.push(options.entry)
}
// the `lib` mode
// distribute the entry file as a component (umd format)
if (options.lib) {
webpackConfig.output.filename = replaceExtension(options.entry, '.js')
webpackConfig.output.library = typeof options.lib === 'string'
? options.lib
: getLibraryName(replaceExtension(options.entry, ''))
webpackConfig.output.libraryTarget = 'umd'
} else if (options.html !== false) {
// only output index.html in non-lib mode
var html = Array.isArray(options.html) ? options.html : [options.html || {}]
html.forEach(item => {
webpackConfig.plugins.unshift(
new HtmlWebpackPlugin(Object.assign({
title: 'Vue App',
template: ownDir('lib/template.html')
}, item))
)
})
}
// installed by `yarn global add`
if (isYarn(__dirname)) {
// modules in yarn global node_modules
// because of yarn's flat node_modules structure
webpackConfig.resolve.modules.push(ownDir('..'))
// loaders in yarn global node_modules
webpackConfig.resolveLoader.modules.push(ownDir('..'))
}
if (production) {
webpackConfig.plugins.push(
new ProgressPlugin(),
new webpack.LoaderOptionsPlugin({
minimize: !options.disableCompress
}),
new ExtractTextPlugin(filenames.css)
)
if (options.disableCompress) {
webpackConfig.devtool = false
} else {
webpackConfig.devtool = 'source-map'
webpackConfig.plugins.push(new webpack.optimize.UglifyJsPlugin({
sourceMap: true,
compressor: {
warnings: false,
conditionals: true,
unused: true,
comparisons: true,
sequences: true,
dead_code: true,
evaluate: true,
if_return: true,
join_vars: true,
negate_iife: false
},
output: {
comments: false
}
}))
}
} else {
webpackConfig.devtool = 'eval-source-map'
webpackConfig.plugins.push(
new FriendlyErrorsPlugin(),
new PostCompilePlugin(() => {
console.log(`> Open http://${options.host}:${options.port}`)
})
)
}
// webpack
// object: merge with webpack config
// function: mutate webpack config
// string: load webpack config file then merge with webpack config
if (!options.disableWebpackConfig) {
if (typeof options.webpack === 'object') {
webpackConfig = webpackMerge.smart(webpackConfig, options.webpack)
} else if (typeof options.webpack === 'function') {
webpackConfig = options.webpack(webpackConfig, options, webpack) || webpackConfig
} else {
var localWebpackConfigPath = typeof options.webpack === 'string'
? cwd(options.webpack)
: path.join(home, '.vue', 'webpack.config.js')
var hasLocalWebpackConfig = fs.existsSync(localWebpackConfigPath)
if (hasLocalWebpackConfig) {
console.log(`> Using webpack config file at ${chalk.yellow(tildify(localWebpackConfigPath))}`)
webpackConfig = webpackMerge.smart(webpackConfig, require(localWebpackConfigPath))
}
}
}
if (!options.watch && !options.production) {
webpackConfig.plugins.push(new webpack.HotModuleReplacementPlugin())
var hmrEntries = options.hmrEntries || ['client']
var hmrClient = require.resolve('webpack-hot-middleware/client') + `?reload=true&path=http://${options.host}:${options.port}/__webpack_hmr`
hmrEntries.forEach(name => {
if (Array.isArray(webpackConfig.entry[name])) {
webpackConfig.entry[name].unshift(hmrClient)
} else {
webpackConfig.entry[name] = [hmrClient, webpackConfig.entry[name]]
}
})
}
// only check entry when there's no custom config
if (!options.config && !fs.existsSync(options.entry)) {
logger.fatal(`${chalk.yellow(options.entry)} does not exist, did you forget to create one?`)
}
run(webpackConfig, options)
function merge (obj) {
var i = 1
var target
var key
for (; i < arguments.length; i++) {
target = arguments[i]
for (key in target) {
if (Object.prototype.hasOwnProperty.call(target, key) && target[key] !== undefined) {
obj[key] = target[key]
}
}
}
return obj
}
function replaceExtension (entry, ext) {
return path.basename(entry).replace(/\.(vue|js)$/, ext)
}
function getLibraryName (fileName) {
return fileName.replace(/[-_.]([\w])/, (_, p1) => p1.toUpperCase())
}
function getFilenames (options) {
return Object.assign({
js: options.production ? '[name].[chunkhash:8].js' : '[name].js',
css: options.lib ? `${replaceExtension(options.entry, '.css')}` : '[name].[contenthash:8].css',
static: options.lib ? 'static/[name].[ext]' : 'static/[name].[hash:8].[ext]'
}, options.filename)
}
function cwd (file) {
return path.resolve(file || '')
}
function ownDir (file) {
return path.join(__dirname, '..', file || '')
}