-
-
Notifications
You must be signed in to change notification settings - Fork 6
/
build.js
79 lines (66 loc) 路 2.07 KB
/
build.js
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
const commonCommandOptions = require('../util/commonCommandOptions')
const defaultArgs = {
clean: true,
}
/** @type {import('../../types/ServicePlugin').ServicePlugin} */
module.exports = (api, options) => {
api.registerCommand('build', {
description: 'Build the app for production',
usage: 'nodepack-service build [entry]',
options: {
'--no-clean': 'do not delete the dist folder before building',
'--function': 'apply default config for serverless function',
...commonCommandOptions,
},
}, async (args) => {
// Default args
for (const key in defaultArgs) {
if (args[key] == null) {
args[key] = defaultArgs[key]
}
}
const { info, error, done, log, chalk } = require('@nodepack/utils')
info(chalk.blue('Preparing production pack...'))
const { getDefaultEntry } = require('../util/defaultEntry.js')
options.entry = getDefaultEntry(api, options, args)
const webpack = require('webpack')
const path = require('path')
const fs = require('fs-extra')
const formatStats = require('../util/formatStats')
// Sererless function
if (args.function) {
options.externals = false
options.productionSourceMap = false
}
const webpackConfig = await api.resolveWebpackConfig()
const targetDir = webpackConfig.output.path
if (args.clean) {
await fs.remove(targetDir)
}
return new Promise((resolve, reject) => {
const compiler = webpack(webpackConfig)
compiler.run(
(err, stats) => {
if (err) {
error(err)
} else {
if (stats.hasErrors()) {
return reject(`Build failed with errors.`)
}
const targetDirShort = path.relative(
api.service.cwd,
targetDir
)
log(formatStats(stats, targetDirShort, api))
done(chalk.green('Build complete! Your app is ready for production.'))
resolve()
}
}
)
})
})
}
// @ts-ignore
module.exports.defaultEnvs = {
build: 'production',
}