forked from electrode-io/electrode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
328 lines (311 loc) · 9.16 KB
/
index.ts
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
/* eslint-disable @typescript-eslint/no-var-requires */
export { initWebpackConfigComposer, generateConfig as compose } from "./util/generate-config";
export { JsonpScriptSrcPlugin } from "./plugins/jsonp-script-src-plugin";
export { SubAppWebpackPlugin } from "./plugins/subapp-plugin";
//
// When xrun execute a build task that involves invoking webpack it
// will check if user wants webpack to start with their webpack.config.js
// If yes, then the task will set env ELECTRODE_WEBPACK_PROFILE to
// the desired profile, so when webpack runs, it's passed to the
// archetype, where we can check and load the correct internal
// webpack config accordingly.
//
export const env = process.env.ELECTRODE_WEBPACK_PROFILE || "production";
export const options = require(`./options/${env}`);
/**
* The copy of webpack module that's installed for @xarc/webpack's use with its configs.
*
* If you need reference to webpack, say to get a plugin, you should use this copy instead
* of getting it with `require("webpack")`.
*/
export const webpack = require("webpack");
const genPartials = require("./partials");
const ConfigPartial = require("webpack-config-composer/lib/partial");
/**
* The webpack config partials this module provides for building a webapp
*/
export const partials = {
/**
* Some base webpack configuration
*/
baseOptions: genPartials["_base-options"],
/**
* configuration to setup the app's entry code
*/
entry: genPartials._entry,
/**
* setup for processing subapp chunks
*/
subappChunks: genPartials["_subapp-chunks"],
/**
* setup whether to auto bundle source to simulate node.js APIs
*/
node: genPartials._node,
/**
* setup the bundle code output
*/
output: genPartials._output,
/**
* setup module resolution
*/
resolve: genPartials._resolve,
/**
* setup resolveLoader option
*/
resolveLoader: genPartials["_resolve-loader"],
/**
* base setup for running Karma tests
*/
karmaBase: genPartials["_test-base"],
/**
* setup for karma test entry
*/
karmaEntry: genPartials["_test-entry"],
/**
* setup for karma test output
*/
karmaOutput: genPartials["_test-output"],
/**
* setup for karma test module resolve
*/
karmaResolve: genPartials["_test-resolve"],
/**
* setup to use babel and babel-loader to transpile code
*/
babel: genPartials._babel,
/**
* setup CSS/styling support
*/
extractStyle: genPartials["_extract-style"],
/**
* setup @loadable/webpack-plugin for dynamic import loadable components
*/
loadable: genPartials._loadable,
/**
* setup loaders for font files like woff/woff2/eot/ttf
*/
fonts: genPartials._fonts,
/**
* setup loaders for handling images like jpeg/png/gif/svg
*/
images: genPartials._images,
/**
* setup loading non-js assets when running in SSR mode
*/
isomorphic: genPartials._isomorphic,
/**
* setup for PWA functionalities
*/
pwa: genPartials._pwa,
/**
* setup a plugin to capture stats and save as stats.json
*/
statsWriter: genPartials._stats,
/**
* setup for optimizing code for production
*
* With webpack 4 this is not really needed given that webpack4 automatically
* handles minification with the mode option.
*/
minify: genPartials._uglify,
/**
* setup for locale support
*/
locales: genPartials._locales,
/**
* setup a plugin that properly terminates webpack on failures
*/
fail: genPartials._fail,
/**
* setup development tools and server
*/
dev: genPartials._dev,
dllEntry: genPartials["_dll-entry"],
dllOutput: genPartials["_dll-output"],
dllReference: genPartials["_dll-reference"],
dllLoad: genPartials["_dll-load"],
dll: genPartials._dll,
/**
* setup a plugin to do simple text base compile progress reporting
*/
progressSimple: genPartials["_simple-progress"],
/**
* setup source maps to be inline
*/
sourceMapsInline: genPartials["_sourcemaps-inline"],
/**
* setup source maps to be remote
*/
sourceMapsRemote: genPartials["_sourcemaps-remote"],
/**
* set webpack to development mode
*/
devMode: genPartials["_dev-mode"],
/**
* set webpack to production mode
*/
prodMode: genPartials["_prod-mode"]
};
// support legacy custom webpack config from user
Object.assign(partials, genPartials);
/**
* Some predefined profiles that specified a list of partials in arrays.
*
* These profiles are available:
* - base - the base for everything
* - production - partials that are for production build only
* - development - partials that are for development only
* - karma - partials that are for running karma tests only
*/
export const profiles = {
/**
* The base feature that include all the partials for a webapp.
* These partials are included: baseOptions, entry, subappChunks,
* output, resolve, resolveLoader, babel, extractStyle, fonts,
* images, statsWriter, isomorphic, node
*/
base: [
partials.baseOptions,
partials.entry,
partials.subappChunks,
partials.output,
partials.resolve,
partials.resolveLoader,
partials.babel,
partials.extractStyle,
partials.fonts,
partials.images,
partials.statsWriter,
partials.isomorphic,
partials.node
],
/**
* Additional partials that are used for a production build
*/
production: [
partials.prodMode,
partials.dllReference,
partials.minify,
partials.locales,
partials.sourceMapsRemote,
partials.progressSimple
],
/**
* Additional partials that are specific for a development build
*/
development: [partials.devMode, partials.dev],
/**
* Additional partials that are specific for a build to run Karma tests
*/
karma: [
partials.devMode,
partials.sourceMapsInline,
partials.progressSimple,
partials.karmaBase,
partials.karmaEntry,
partials.karmaOutput,
partials.karmaResolve
]
};
/**
* Ordinary plain object that holds a webpack config
*/
export type PlainConfig = Record<string, any>;
import * as WebpackComposer from "webpack-config-composer";
/**
*
* Apply an array of partial webpack configs into `config`
*
* The partials in the array is applied from left to right so the right ones override left ones.
*
* You can get predefined partials from this module. For example, to add your own webpack config:
*
* In your `webpack.config.ts`:
*
* ```js
* import { profiles, applyPartials } from "@xarc/webpack"
*
* const myConfig = applyPartials({
* // your base webpack configs that are OK to get override
* },
* [
* ...profiles.base,
* ...profiles.development,
* {
* // your own webpack config that will override
* // everything else goes here
* }
* ]
* );
*
* export default myConfig;
* ```
*
*
* @param config - the base config (will not be mutated)
* @param parts - array of partials to apply
*
* @returns a new config with all partials merged into `config`
*/
export function applyPartials(
config: PlainConfig = {},
parts: (PlainConfig | typeof ConfigPartial)[]
) {
const composer = new WebpackComposer({ partials: parts });
composer.addProfile("apply", {});
let id = 1;
parts.forEach(p => {
if (p instanceof ConfigPartial) {
composer.addPartialToProfile(p._name, "apply", p.config, p.options);
} else {
composer.addPartialToProfile(`object-${Date.now()}-${id++}`, "apply", p, {});
}
});
return composer.compose(config, "apply");
}
/**
* Provide out of the box default webpack configs for various modes:
*
* Available configs:
* - development() - for development
* - production() - for building production
* - karma() - for running karma tests
*/
export const defaultConfigs = {
/**
* generate webpack config for development
*
* Basically: `applyPartials({}, [...profiles.base, ...profiles.development])`
* @param baseConfig - base config to merge into (not mutated)
* @param moreParts - more partials to apply (will override)
* @returns a new webpack config ready for use
*/
development(
baseConfig: PlainConfig = {},
moreParts: (PlainConfig | typeof ConfigPartial)[] = []
) {
return applyPartials(baseConfig, [...profiles.base, ...profiles.development, ...moreParts]);
},
/**
* generate webpack config for production
*
* Basically: `applyPartials({}, [...profiles.base, ...profiles.production])`
* @param baseConfig - base config to merge into (not mutated)
* @param moreParts - more partials to apply (will override)
* @returns a new webpack config ready for use
*/
production(baseConfig: PlainConfig = {}, moreParts: (PlainConfig | typeof ConfigPartial)[] = []) {
return applyPartials(baseConfig, [...profiles.base, ...profiles.production, ...moreParts]);
},
/**
* generate webpack config for running karma tests
*
* Basically: `applyPartials({}, [...profiles.base, ...profiles.karma])`
* @param baseConfig - base config to merge into (not mutated)
* @param moreParts - more partials to apply (will override)
* @returns a new webpack config ready for use
*/
karma(baseConfig: PlainConfig = {}, moreParts: (PlainConfig | typeof ConfigPartial)[] = []) {
return applyPartials(baseConfig, [...profiles.base, ...profiles.karma, moreParts]);
}
};