-
Notifications
You must be signed in to change notification settings - Fork 110
/
webpack.ts
786 lines (718 loc) · 24.1 KB
/
webpack.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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
import type {
Configuration,
Compiler,
RuleSetRule,
Stats,
RuleSetUseItem,
WebpackPluginInstance,
Module,
} from 'webpack';
import { join, dirname, resolve, relative, posix, isAbsolute } from 'path';
import { createHash } from 'crypto';
import { mergeWith, flatten, zip } from 'lodash';
import { writeFileSync, realpathSync, readFileSync } from 'fs';
import { compile, registerHelper } from 'handlebars';
import jsStringEscape from 'js-string-escape';
import { BundleDependencies, ResolvedTemplateImport } from './splitter';
import { BuildResult, Bundler, BundlerOptions } from './bundler';
import type { InputNode } from 'broccoli-node-api';
import Plugin from 'broccoli-plugin';
import { babelFilter, packageName, Package } from '@embroider/shared-internals';
import { Options } from './package';
import { PackageCache } from '@embroider/shared-internals';
import { Memoize } from 'typescript-memoize';
import makeDebug from 'debug';
import { ensureDirSync, symlinkSync, existsSync } from 'fs-extra';
import MiniCssExtractPlugin from 'mini-css-extract-plugin';
import minimatch from 'minimatch';
import { TransformOptions } from '@babel/core';
import { stripQuery } from './util';
const EXTENSIONS = ['.js', '.ts', '.json'];
const debug = makeDebug('ember-auto-import:webpack');
registerHelper('js-string-escape', jsStringEscape);
registerHelper('join', function (list, connector) {
return list.join(connector);
});
const moduleIdMap = new Map<string, string>();
function moduleToId(moduleSpecifier: string) {
let id = moduleSpecifier;
// if the module contains characters that need to be escaped, then map this to a hash instead, so we can easily replace this later
if (moduleSpecifier.includes('"') || moduleSpecifier.includes("'")) {
id = createHash('md5').update('some_string').digest('hex');
moduleIdMap.set(id, moduleSpecifier);
}
return id;
}
function idToModule(id: string) {
return moduleIdMap.get(id) ?? id;
}
registerHelper('module-to-id', moduleToId);
const entryTemplate = compile(
`
module.exports = (function(){
var d = _eai_d;
var r = _eai_r;
window.emberAutoImportDynamic = function(specifier) {
if (arguments.length === 1) {
return r('_eai_dyn_' + specifier);
} else {
return r('_eai_dynt_' + specifier)(Array.prototype.slice.call(arguments, 1))
}
};
window.emberAutoImportSync = function(specifier) {
{{! this is only used for synchronous importSync() using a template string }}
return r('_eai_sync_' + specifier)(Array.prototype.slice.call(arguments, 1))
};
{{!- es compatibility. Where we're using "require", webpack doesn't necessarily know to apply its own ES compatibility stuff -}}
function esc(m) {
return m && m.__esModule ? m : Object.assign({ default: m }, m);
}
{{#each staticImports as |module|}}
d('{{js-string-escape module.specifier}}', EAI_DISCOVERED_EXTERNALS('{{module-to-id module.specifier}}'), function() { return esc(require('{{js-string-escape module.specifier}}')); });
{{/each}}
{{#each dynamicImports as |module|}}
d('_eai_dyn_{{js-string-escape module.specifier}}', [], function() { return import('{{js-string-escape module.specifier}}'); });
{{/each}}
{{#each staticTemplateImports as |module|}}
d('_eai_sync_{{js-string-escape module.key}}', [], function() {
return function({{module.args}}) {
return esc(require({{{module.template}}}));
}
});
{{/each}}
{{#each dynamicTemplateImports as |module|}}
d('_eai_dynt_{{js-string-escape module.key}}', [], function() {
return function({{module.args}}) {
return import({{{module.template}}});
}
});
{{/each}}
{{#if needsApp}}
require('./app.cjs');
{{/if}}
})();
`,
{ noEscape: true }
) as (args: {
staticImports: { specifier: string }[];
dynamicImports: { specifier: string }[];
staticTemplateImports: { key: string; args: string; template: string }[];
dynamicTemplateImports: { key: string; args: string; template: string }[];
publicAssetURL: string | undefined;
needsApp: boolean;
}) => string;
// this goes in a file by itself so we can tell webpack not to parse it. That
// allows us to grab the "require" and "define" from our enclosing scope without
// webpack messing with them.
//
// It's important that we're using our enclosing scope and not jumping directly
// to window.require (which would be easier), because the entire Ember app may be
// inside a closure with a "require" that isn't the same as "window.require".
const loader = `
window._eai_r = require;
window._eai_d = define;
`;
export default class WebpackBundler extends Plugin implements Bundler {
private state:
| {
webpack: Compiler;
stagingDir: string;
}
| undefined;
private lastBuildResult: BuildResult | undefined;
constructor(priorTrees: InputNode[], private opts: BundlerOptions) {
super(priorTrees, {
persistentOutput: true,
needsCache: true,
annotation: 'ember-auto-import-webpack',
});
}
get buildResult() {
if (!this.lastBuildResult) {
throw new Error(`bug: no buildResult available yet`);
}
return this.lastBuildResult;
}
private get webpack() {
return this.setup().webpack;
}
private get stagingDir() {
return this.setup().stagingDir;
}
private setup() {
if (this.state) {
return this.state;
}
// resolve the real path, because we're going to do path comparisons later
// that could fail if this is not canonical.
//
// cast is ok because we passed needsCache to super
let stagingDir = realpathSync(this.cachePath!);
let entry: { [name: string]: string[] } = {};
this.opts.bundles.names.forEach((bundle) => {
entry[bundle] = [
join(stagingDir, 'l.cjs'),
join(stagingDir, `${bundle}.cjs`),
];
});
let { plugin: stylePlugin, loader: styleLoader } = this.setupStyleLoader();
let config: Configuration = {
mode:
this.opts.environment === 'production' ? 'production' : 'development',
entry,
performance: {
hints: false,
},
// this controls webpack's own runtime code generation. You still need
// preset-env to preprocess the libraries themselves (which is already
// part of this.opts.babelConfig)
target: `browserslist:${this.opts.rootPackage.browserslist()}`,
output: {
path: join(this.outputPath, 'assets'),
publicPath: this.opts.rootPackage.publicAssetURL(),
filename: `chunk.[id].[chunkhash].js`,
chunkFilename: `chunk.[id].[chunkhash].js`,
libraryTarget: 'var',
library: '__ember_auto_import__',
},
optimization: {
splitChunks: {
chunks: 'all',
},
},
resolveLoader: {
alias: {
// these loaders are our dependencies, not the app's dependencies. I'm
// not overriding the default loader resolution rules in case the app also
// wants to control those.
'babel-loader-8': require.resolve('babel-loader'),
'eai-style-loader': require.resolve('style-loader'),
'eai-css-loader': require.resolve('css-loader'),
},
},
resolve: {
extensions: EXTENSIONS,
mainFields: ['browser', 'module', 'main'],
alias: Object.assign(
{
// this is because of the allowAppImports feature needs to be able to import things
// like app-name/lib/something from within webpack handled code but that needs to be
// able to resolve to app-root/app/lib/something.
[this.opts.rootPackage.name]: `${this.opts.rootPackage.root}/app`,
},
...removeUndefined([...this.opts.packages].map((pkg) => pkg.aliases))
),
},
plugins: removeUndefined([stylePlugin]),
module: {
noParse: (file: string) => file === join(stagingDir, 'l.cjs'),
rules: [
this.babelRule(
stagingDir,
(filename) => !this.fileIsInApp(filename),
this.opts.rootPackage.cleanBabelConfig()
),
this.babelRule(
stagingDir,
(filename) => this.fileIsInApp(filename),
this.opts.rootPackage.babelOptions
),
{
test: /\.css$/i,
use: [
styleLoader,
{
loader: 'eai-css-loader',
options: [...this.opts.packages].find(
(pkg) => pkg.cssLoaderOptions
)?.cssLoaderOptions,
},
],
},
],
},
node: false,
externals: this.externalsHandler,
};
if ([...this.opts.packages].find((pkg) => pkg.forbidsEval)) {
config.devtool = 'source-map';
}
mergeConfig(
config,
...[...this.opts.packages].map((pkg) => pkg.webpackConfig)
);
debug('webpackConfig %j', config);
this.state = { webpack: this.opts.webpack(config), stagingDir };
return this.state;
}
private setupStyleLoader(): {
loader: RuleSetUseItem;
plugin: WebpackPluginInstance | undefined;
} {
if (
this.opts.environment === 'production' ||
this.opts.rootPackage.isFastBootEnabled
) {
return {
loader: MiniCssExtractPlugin.loader,
plugin: new MiniCssExtractPlugin({
filename: `chunk.[id].[chunkhash].css`,
chunkFilename: `chunk.[id].[chunkhash].css`,
...[...this.opts.packages].find(
(pkg) => pkg.miniCssExtractPluginOptions
)?.miniCssExtractPluginOptions,
}),
};
} else
return {
loader: {
loader: 'eai-style-loader',
options: [...this.opts.packages].find((pkg) => pkg.styleLoaderOptions)
?.styleLoaderOptions,
},
plugin: undefined,
};
}
private skipBabel(): Required<Options>['skipBabel'] {
let output: Required<Options>['skipBabel'] = [];
for (let pkg of this.opts.packages) {
let skip = pkg.skipBabel;
if (skip) {
output = output.concat(skip);
}
}
return output;
}
private fileIsInApp(filename: string) {
let packageCache = PackageCache.shared(
'ember-auto-import',
this.opts.rootPackage.root
);
const pkg = packageCache.ownerOfFile(filename);
return pkg?.root === this.opts.rootPackage.root;
}
private babelRule(
stagingDir: string,
filter: (filename: string) => boolean,
babelConfig: TransformOptions
): RuleSetRule {
let shouldTranspile = babelFilter(
this.skipBabel(),
this.opts.rootPackage.root
);
return {
test: (filename: string) => {
// We don't apply babel to our own stagingDir (it contains only our own
// entrypoints that we wrote, and it can use `import()`, which we want
// to leave directly for webpack).
//
// And we otherwise defer to the `skipBabel` setting as implemented by
// `@embroider/shared-internals`.
return (
dirname(filename) !== stagingDir &&
shouldTranspile(filename) &&
filter(filename)
);
},
use: {
loader: 'babel-loader-8',
options: babelConfig,
},
};
}
private externalizedByUs = new Set<string>();
@Memoize()
private get externalsHandler(): Configuration['externals'] {
let packageCache = PackageCache.shared(
'ember-auto-import',
this.opts.rootPackage.root
);
return (params, callback) => {
let { context, request, contextInfo } = params;
if (!context || !request) {
return callback();
}
if (request.startsWith('!')) {
return callback();
}
let pkg = packageCache.ownerOfFile(context);
if (!pkg) {
// we're not inside any identifiable NPM package
return callback();
}
let name = packageName(request);
if (!name) {
if (!isAbsolute(request) && pkg.root === this.opts.rootPackage.root) {
let appRelativeContext = relative(
resolve(this.opts.rootPackage.root, 'app'),
context
);
name = this.opts.rootPackage.name;
let candidateName = posix.join(name, appRelativeContext, request);
if (candidateName.startsWith(name)) {
request = candidateName;
} else {
// the relative request does not target the traditional "app" dir
return callback();
}
} else {
// we're only interested in handling inter-package resolutions
return callback();
}
}
// Handling full-name imports that point at the app itself e.g. app-name/lib/thingy
if (name === this.opts.rootPackage.name) {
if (
this.importMatchesAppImports(
stripQuery(request.slice(name.length + 1))
)
) {
// webpack should handle this because it's another file in the app that matches allowAppImports
return callback();
} else {
// use ember's module because this is part of the app that doesn't match allowAppImports
this.externalizedByUs.add(request);
return callback(undefined, 'commonjs ' + request);
}
}
// if we're not in a v2 addon and the file that is doing the import doesn't match one of the allowAppImports patterns
// then we don't implement the "fallback behaviour" below i.e. this won't be handled by ember-auto-import
if (
!pkg.isV2Addon() &&
!this.matchesAppImports(pkg, contextInfo?.issuer)
) {
return callback();
}
if (pkg.isV2Addon() && pkg.meta.externals?.includes(name)) {
this.externalizedByUs.add(request);
return callback(undefined, 'commonjs ' + request);
}
try {
let found = packageCache.resolve(name, pkg);
if (!found.isEmberPackage() || found.isV2Addon()) {
// if we're importing a non-ember package or a v2 addon, we don't
// externalize. Those are all "normal" looking packages that should be
// resolvable statically.
return callback();
} else {
// the package exists but it is a v1 ember addon, so it's not
// resolvable at build time, so we externalize it.
this.externalizedByUs.add(request);
return callback(undefined, 'commonjs ' + request);
}
} catch (err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err;
}
// real package doesn't exist, so externalize it
this.externalizedByUs.add(request);
return callback(undefined, 'commonjs ' + request);
}
};
}
*withResolvableExtensions(
importSpecifier: string
): Generator<string, void, void> {
if (importSpecifier.match(/\.\w+$/)) {
yield importSpecifier;
} else {
for (let ext of EXTENSIONS) {
yield `${importSpecifier}${ext}`;
}
}
}
importMatchesAppImports(relativeImportSpecifier: string): boolean {
for (let candidate of this.withResolvableExtensions(
relativeImportSpecifier
)) {
if (
this.opts.rootPackage.allowAppImports.some((pattern) =>
minimatch(candidate, pattern)
)
) {
return true;
}
}
return false;
}
matchesAppImports(pkg: Package, requestingFile: string | undefined): boolean {
if (!requestingFile) {
return false;
}
if (pkg.root !== this.opts.rootPackage.root) {
return false;
}
return this.opts.rootPackage.allowAppImports.some((pattern) =>
minimatch(relative(join(pkg.root, 'app'), requestingFile), pattern)
);
}
async build(): Promise<void> {
let bundleDeps = await this.opts.splitter.deps();
for (let [bundle, deps] of bundleDeps.entries()) {
this.writeEntryFile(bundle, deps);
}
this.writeLoaderFile();
this.linkDeps(bundleDeps);
let stats = await this.runWebpack();
this.lastBuildResult = this.summarizeStats(stats, bundleDeps);
this.addDiscoveredExternals(this.lastBuildResult);
}
private addDiscoveredExternals(build: BuildResult) {
for (let assetFiles of build.entrypoints.values()) {
for (let assetFile of assetFiles) {
let inputSrc = readFileSync(
resolve(this.outputPath, assetFile),
'utf8'
);
let outputSrc = inputSrc.replace(
/EAI_DISCOVERED_EXTERNALS\(['"]([^'"]+)['"]\)/g,
(_substr: string, matched: string) => {
let deps = build
.externalDepsFor(idToModule(matched))
.filter((dep) => this.externalizedByUs.has(dep));
return '[' + deps.map((d) => `'${d}'`).join(',') + ']';
}
);
writeFileSync(resolve(this.outputPath, assetFile), outputSrc, 'utf8');
}
}
}
private externalDepsSearcher(
stats: Required<Stats>
): (request: string) => string[] {
let externals = new Map<Module, Set<string>>();
function gatherExternals(
module: Module,
output = new Set<string>()
): Set<string> {
if (externals.has(module)) {
for (let ext of externals.get(module)!) {
output.add(ext);
}
} else {
let ownExternals = new Set<string>();
externals.set(module, ownExternals);
for (let dep of module.dependencies) {
let nextModule = stats.compilation.moduleGraph.getModule(dep);
if (nextModule) {
if ((nextModule as any).externalType) {
ownExternals.add((nextModule as any).request);
} else {
gatherExternals(nextModule, ownExternals);
}
}
}
for (let o of ownExternals) {
output.add(o);
}
}
return output;
}
return (request: string): string[] => {
for (let module of stats.compilation.modules) {
for (let dep of module.dependencies) {
if ((dep as any).request === request) {
return [
...gatherExternals(stats.compilation.moduleGraph.getModule(dep)!),
];
}
}
}
return [];
};
}
private summarizeStats(
_stats: Required<Stats>,
bundleDeps: Map<string, BundleDependencies>
): BuildResult {
let { entrypoints, assets } = _stats.toJson();
// webpack's types are written rather loosely, implying that these two
// properties may not be present. They really always are, as far as I can
// tell, but we need to check here anyway to satisfy the type checker.
if (!entrypoints) {
throw new Error(`unexpected webpack output: no entrypoints`);
}
if (!assets) {
throw new Error(`unexpected webpack output: no assets`);
}
let output: BuildResult = {
entrypoints: new Map(),
lazyAssets: [] as string[],
externalDepsFor: this.externalDepsSearcher(_stats),
};
let nonLazyAssets: Set<string> = new Set();
for (let id of Object.keys(entrypoints!)) {
let { assets: entrypointAssets } = entrypoints![id];
if (!entrypointAssets) {
throw new Error(`unexpected webpack output: no entrypoint.assets`);
}
// our built-in bundles can be "empty" while still existing because we put
// setup code in them, so they get a special check for non-emptiness.
// Whereas any other bundle that was manually configured by the user
// should always be emitted.
if (
!this.opts.bundles.isBuiltInBundleName(id) ||
nonEmptyBundle(id, bundleDeps)
) {
output.entrypoints.set(
id,
entrypointAssets.map((a) => 'assets/' + a.name)
);
}
entrypointAssets.forEach((asset) => nonLazyAssets.add(asset.name));
}
for (let asset of assets!) {
if (!nonLazyAssets.has(asset.name)) {
output.lazyAssets.push('assets/' + asset.name);
}
}
return output;
}
private writeEntryFile(name: string, deps: BundleDependencies) {
writeFileSync(
join(this.stagingDir, `${name}.cjs`),
entryTemplate({
staticImports: deps.staticImports,
dynamicImports: deps.dynamicImports,
dynamicTemplateImports:
deps.dynamicTemplateImports.map(mapTemplateImports),
staticTemplateImports:
deps.staticTemplateImports.map(mapTemplateImports),
publicAssetURL: this.opts.rootPackage.publicAssetURL(),
needsApp: name === 'tests',
})
);
}
private writeLoaderFile() {
writeFileSync(join(this.stagingDir, `l.cjs`), loader);
}
private linkDeps(bundleDeps: Map<string, BundleDependencies>) {
for (let deps of bundleDeps.values()) {
for (let resolved of deps.staticImports) {
this.ensureLinked(resolved);
}
for (let resolved of deps.dynamicImports) {
this.ensureLinked(resolved);
}
for (let resolved of deps.staticTemplateImports) {
this.ensureLinked(resolved);
}
for (let resolved of deps.dynamicTemplateImports) {
this.ensureLinked(resolved);
}
}
}
private ensureLinked({
packageName,
packageRoot,
}: {
packageName: string;
packageRoot: string;
}): void {
ensureDirSync(dirname(join(this.stagingDir, 'node_modules', packageName)));
if (!existsSync(join(this.stagingDir, 'node_modules', packageName))) {
symlinkSync(
packageRoot,
join(this.stagingDir, 'node_modules', packageName),
'junction'
);
}
}
private async runWebpack(): Promise<Required<Stats>> {
return new Promise((resolve, reject) => {
this.webpack.run((err, stats) => {
const statsString = stats ? stats.toString() : '';
if (err) {
this.opts.consoleWrite(statsString);
reject(err);
return;
}
if (stats?.hasErrors()) {
this.opts.consoleWrite(statsString);
reject(new Error('webpack returned errors to ember-auto-import'));
return;
}
if (stats?.hasWarnings() || process.env.AUTO_IMPORT_VERBOSE) {
this.opts.consoleWrite(statsString);
}
// this cast is justified because we already checked hasErrors above
resolve(stats as Required<Stats>);
});
}) as Promise<Required<Stats>>;
}
}
export function mergeConfig(dest: Configuration, ...srcs: Configuration[]) {
return mergeWith(dest, ...srcs, combine);
}
function combine(objValue: any, srcValue: any, key: string) {
if (key === 'noParse') {
return eitherPattern(objValue, srcValue);
}
if (key === 'externals') {
return [srcValue, objValue].flat();
}
// arrays concat
if (Array.isArray(objValue)) {
return objValue.concat(srcValue);
}
}
// webpack configs have several places where they accept:
// - RegExp
// - [RegExp]
// - (resource: string) => boolean
// - string
// - [string]
// This function combines any of these with a logical OR.
function eitherPattern(...patterns: any[]): (resource: string) => boolean {
let flatPatterns = flatten(patterns);
return function (resource) {
for (let pattern of flatPatterns) {
if (pattern instanceof RegExp) {
if (pattern.test(resource)) {
return true;
}
} else if (typeof pattern === 'string') {
if (pattern === resource) {
return true;
}
} else if (typeof pattern === 'function') {
if (pattern(resource)) {
return true;
}
}
}
return false;
};
}
function mapTemplateImports(imp: ResolvedTemplateImport) {
return {
key: imp.importedBy[0].cookedQuasis.join('${e}'),
args: imp.expressionNameHints.join(','),
template:
'`' +
zip(imp.cookedQuasis, imp.expressionNameHints)
.map(([q, e]) => q + (e ? '${' + e + '}' : ''))
.join('') +
'`',
};
}
function nonEmptyBundle(
name: string,
bundleDeps: Map<string, BundleDependencies>
): boolean {
let deps = bundleDeps.get(name);
if (!deps) {
return false;
}
return (
deps.staticImports.length > 0 ||
deps.staticTemplateImports.length > 0 ||
deps.dynamicImports.length > 0 ||
deps.dynamicTemplateImports.length > 0
);
}
// this little helper is needed because typescript can't see through normal
// usage of Array.prototype.filter.
function removeUndefined<T>(list: (T | undefined)[]): T[] {
return list.filter((item) => typeof item !== 'undefined') as T[];
}