-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquickCommands.js
191 lines (168 loc) · 4.93 KB
/
quickCommands.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
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
// @flow
import path from 'path'
import runSeries from 'run-series'
import merge from 'webpack-merge'
import cleanApp from './commands/clean-app'
import {UserError} from './errors'
import {install} from './utils'
import webpackBuild from './webpackBuild'
import webpackServer from './webpackServer'
import type {ErrBack} from './types'
type QuickConfigOptions = {
// Extra command config to be merged in
commandConfig?: Object,
// Default <title>
defaultTitle: string,
// Path to a module which handles rendering if something suitable was
// exported from the entry module the user provided.
renderShim: ?string,
// Aliases which allow the render shim module to resolve modules it needs to
// import from the working directory.
renderShimAliases: ?Object,
};
type QuickAppConfig = {
getName: () => string,
getQuickDependencies: () => string[],
getQuickBuildConfig: () => Object,
getQuickServeConfig: () => Object,
};
/**
* Create a quick build, installing any required dependencies first if
* they're not resolvable.
*/
export function build(args: Object, appConfig: QuickAppConfig, cb: ErrBack) {
if (args._.length === 1) {
return cb(new UserError('An entry module must be specified.'))
}
let dist = args._[2] || 'dist'
runSeries([
(cb) => install(appConfig.getQuickDependencies(), {args, check: true}, cb),
(cb) => cleanApp({_: ['clean-app', dist]}, cb),
(cb) => webpackBuild(
`${appConfig.getName()} app`,
args,
() => createBuildConfig(args, appConfig.getQuickBuildConfig()),
cb
),
], cb)
}
/**
* Create default command config for a quick app build and merge any extra
* config provided into it.
*/
export function createBuildConfig(args: Object, options: QuickConfigOptions) {
let {
commandConfig: extraConfig = {},
defaultTitle,
renderShim,
renderShimAliases,
} = options
let entry = path.resolve(args._[1])
let dist = path.resolve(args._[2] || 'dist')
let mountId = args['mount-id'] || 'app'
let production = process.env.NODE_ENV === 'production'
let filenamePattern = production ? '[name].[chunkhash:8].js' : '[name].js'
let config: Object = {
babel: {
stage: 0,
},
devtool: 'source-map',
output: {
chunkFilename: filenamePattern,
filename: filenamePattern,
path: dist,
publicPath: '/',
},
plugins: {
html: {
mountId,
title: args.title || defaultTitle,
},
// A vendor bundle can be explicitly enabled with a --vendor flag
vendor: args.vendor,
},
}
if (renderShim == null || args.force === true) {
config.entry = {app: [entry]}
}
else {
// Use a render shim module which supports quick prototyping
config.entry = {app: [renderShim]}
config.plugins.define = {NWB_QUICK_MOUNT_ID: JSON.stringify(mountId)}
config.resolve = {
alias: {
// Allow the render shim module to import the provided entry module
'nwb-quick-entry': entry,
// Allow the render shim module to import modules from the cwd
...renderShimAliases,
}
}
}
if (args.polyfill === false || args.polyfills === false) {
config.polyfill = false
}
return merge(config, extraConfig)
}
/**
* Create default command config for quick serving and merge any extra config
* provided into it.
*/
export function createServeConfig(args: Object, options: QuickConfigOptions) {
let {
commandConfig: extraConfig = {},
defaultTitle,
renderShim,
renderShimAliases,
} = options
let entry = path.resolve(args._[1])
let dist = path.resolve(args._[2] || 'dist')
let mountId = args['mount-id'] || 'app'
let config: Object = {
babel: {
stage: 0
},
output: {
filename: 'app.js',
path: dist,
publicPath: '/',
},
plugins: {
html: {
mountId,
title: args.title || defaultTitle,
},
},
}
if (args.force === true || renderShim == null) {
config.entry = [entry]
}
else {
config.entry = [renderShim]
config.plugins.define = {NWB_QUICK_MOUNT_ID: JSON.stringify(mountId)}
config.resolve = {
alias: {
// Allow the render shim module to import the provided entry module
'nwb-quick-entry': entry,
// Allow the render shim module to import modules from the cwd
...renderShimAliases,
}
}
}
if (args.polyfill === false || args.polyfills === false) {
config.polyfill = false
}
return merge(config, extraConfig)
}
/**
* Run a quick development server, installing any required dependencies first if
* they're not resolvable.
*/
export function serve(args: Object, appConfig: QuickAppConfig, cb: ErrBack) {
if (args._.length === 1) {
return cb(new UserError('An entry module must be specified.'))
}
runSeries([
(cb) => install(appConfig.getQuickDependencies(), {args, check: true}, cb),
(cb) => webpackServer(args, createServeConfig(args, appConfig.getQuickServeConfig()), cb),
], cb)
}