-
-
Notifications
You must be signed in to change notification settings - Fork 1.7k
Expand file tree
/
Copy pathindex.ts
More file actions
244 lines (206 loc) · 5.54 KB
/
index.ts
File metadata and controls
244 lines (206 loc) · 5.54 KB
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
// Make sure the Acorn Parser (used by Webpack) can parse ES-Stage3 code
// This must be at the top BEFORE webpack is loaded so that we can extend
// and replace the parser before webpack uses it
// Based on the issue: https://github.com/webpack/webpack/issues/10216
import stage3 from 'acorn-stage3';
// we use require to be able to override the exports
const acorn = require('acorn');
acorn.Parser = acorn.Parser.extend(stage3);
import { highlight } from 'cli-highlight';
import { merge } from 'webpack-merge';
import Config from 'webpack-chain';
import webpack from 'webpack';
import { applyExternalConfigs } from './helpers/externalConfigs';
import { determineProjectFlavor } from './helpers/flavor';
import { error, info } from './helpers/log';
import { configs } from './configuration';
import helpers from './helpers';
export interface IWebpackEnv {
[name: string]: any;
env?: string;
appPath?: string;
appResourcesPath?: string;
buildPath?: string;
appComponents?: string[];
nativescriptLibPath?: string | boolean;
android?: boolean;
ios?: boolean;
// for custom platforms
platform?: string;
sourceMap?: string | boolean;
production?: boolean;
report?: boolean;
hmr?: boolean;
// enable verbose output
verbose?: boolean;
// enable webpack profiling
profile?: boolean;
// print webpack stats (default: true)
stats?: boolean;
// enable commonjs modules (default: ES modules, esm)
commonjs?: boolean;
// misc
replace?: string[] | string;
watchNodeModules?: boolean;
}
interface IChainEntry {
chainFn: any;
order?: number;
plugin?: string;
}
let webpackChains: IChainEntry[] = [];
let webpackMerges: any[] = [];
let explicitUseConfig = false;
let hasInitialized = false;
let currentPlugin: string | undefined;
/**
* @internal
*/
export let env: IWebpackEnv = {};
/**
* @internal
*/
export function setCurrentPlugin(plugin: string) {
currentPlugin = plugin;
}
/**
* @internal
*/
export function clearCurrentPlugin() {
currentPlugin = undefined;
}
////// PUBLIC API
/**
* The default flavor specific configs
*/
export const defaultConfigs = configs;
/**
* Utilities to simplify various tasks
*/
export const Utils = helpers;
/**
* webpack-merge exported for convenience. Useful for merging configuration objects
*/
export { merge };
/**
* Initialize @nativescript/webpack with the webpack env.
* Must be called first.
*
* @param _env The webpack env
*/
export function init(_env: IWebpackEnv) {
hasInitialized = true;
if (_env) {
env = _env;
}
}
/**
* Explicitly specify the base config to use.
* Calling this will opt-out from automatic flavor detection.
*
* Useful when the flavor cannot be detected due to the project structure
* for example in a custom monorepo.
*
* @param config Name of the base config to use.
*/
export function useConfig(config: keyof typeof defaultConfigs | false) {
explicitUseConfig = true;
if (config) {
webpackChains.push({
order: -1,
chainFn: configs[config],
});
}
}
/**
* Add a new function to be called when building the internal config using webpack-chain.
*
* @param chainFn A function that accepts the internal chain config, and the current environment
* @param options Optional options to control the order in which the chain function should be applied.
*/
export function chainWebpack(
chainFn: (config: Config, env: IWebpackEnv) => any,
options?: { order?: number },
) {
webpackChains.push({
order: options?.order || 0,
chainFn,
plugin: currentPlugin,
});
}
/**
* Merge an object into the resolved chain config.
*
* @param mergeFn An object or a function that optionally returns an object (can mutate the object directly and return nothing)
*/
export function mergeWebpack(
mergeFn:
| ((config: Partial<webpack.Configuration>, env: IWebpackEnv) => any)
| Partial<webpack.Configuration>,
) {
webpackMerges.push(mergeFn);
}
/**
* Resolve a new instance of the internal chain config with all chain functions applied.
*/
export function resolveChainableConfig(): Config {
const config = new Config();
if (!explicitUseConfig) {
useConfig(determineProjectFlavor());
}
// apply configs from dependencies
// todo: allow opt-out
applyExternalConfigs();
webpackChains
.splice(0)
.sort((a, b) => {
return a.order - b.order;
})
.forEach(({ chainFn, plugin }) => {
try {
chainFn(config, env);
} catch (err) {
if (plugin) {
// catch and print errors from plugins
return error(`
Unable to apply chain function from: ${plugin}.
Error is: ${err}
`);
}
// otherwise throw - as the error is likely from the user config
// or missing env flags (eg. missing platform)
throw err;
}
});
if (env.verbose) {
info('Resolved chainable config (before merges):');
info(highlight(config.toString(), { language: 'js' }));
}
return config;
}
/**
* Resolve a "final" configuration that has all chain functions and merges applied.
*
* @param chainableConfig Optional chain config to use.
*/
export function resolveConfig(
chainableConfig = resolveChainableConfig(),
): webpack.Configuration {
if (!hasInitialized) {
throw error('resolveConfig() must be called after init()');
}
let config = chainableConfig.toConfig();
// this applies webpack merges
webpackMerges.forEach((mergeFn) => {
if (typeof mergeFn === 'function') {
// mergeFn is a function with optional return value
const res = mergeFn(config, env);
if (res) config = merge(config, res);
} else if (mergeFn) {
// mergeFn is a literal value (object)
config = merge(config, mergeFn);
}
});
// return a config usable by webpack
return config;
}