@embroider/core 2.0.2 → 2.1.1-unstable.00ec2e7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,448 +0,0 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- Object.defineProperty(exports, "__esModule", { value: true });
9
- exports.isDefineExpression = exports.isDynamicImportExpression = exports.isImportSyncExpression = void 0;
10
- const shared_internals_1 = require("@embroider/shared-internals");
11
- const path_1 = require("path");
12
- const shared_internals_2 = require("@embroider/shared-internals");
13
- const typescript_memoize_1 = require("typescript-memoize");
14
- const js_handlebars_1 = require("./js-handlebars");
15
- const babel_import_util_1 = require("babel-import-util");
16
- const crypto_1 = require("crypto");
17
- const fs_extra_1 = require("fs-extra");
18
- function isImportSyncExpression(t, path) {
19
- if (!path ||
20
- !path.isCallExpression() ||
21
- path.node.callee.type !== 'Identifier' ||
22
- !path.get('callee').referencesImport('@embroider/macros', 'importSync')) {
23
- return false;
24
- }
25
- const args = path.node.arguments;
26
- return Array.isArray(args) && args.length === 1 && t.isStringLiteral(args[0]);
27
- }
28
- exports.isImportSyncExpression = isImportSyncExpression;
29
- function isDynamicImportExpression(t, path) {
30
- if (!path || !path.isCallExpression() || path.node.callee.type !== 'Import') {
31
- return false;
32
- }
33
- const args = path.node.arguments;
34
- return Array.isArray(args) && args.length === 1 && t.isStringLiteral(args[0]);
35
- }
36
- exports.isDynamicImportExpression = isDynamicImportExpression;
37
- function isDefineExpression(t, path) {
38
- // should we allow nested defines, or stop at the top level?
39
- if (!path.isCallExpression() || path.node.callee.type !== 'Identifier' || path.node.callee.name !== 'define') {
40
- return false;
41
- }
42
- const args = path.node.arguments;
43
- // only match define with 3 arguments define(name: string, deps: string[], cb: Function);
44
- return (Array.isArray(args) &&
45
- args.length === 3 &&
46
- t.isStringLiteral(args[0]) &&
47
- t.isArrayExpression(args[1]) &&
48
- t.isFunction(args[2]));
49
- }
50
- exports.isDefineExpression = isDefineExpression;
51
- function adjustSpecifier(specifier, file, opts, isDynamic) {
52
- if (specifier === '@embroider/macros') {
53
- // the macros package is always handled directly within babel (not
54
- // necessarily as a real resolvable package), so we should not mess with it.
55
- // It might not get compiled away until *after* our plugin has run, which is
56
- // why we need to know about it.
57
- return specifier;
58
- }
59
- specifier = handleRenaming(specifier, file, opts);
60
- specifier = handleExternal(specifier, file, opts, isDynamic);
61
- return specifier;
62
- }
63
- function handleRenaming(specifier, sourceFile, opts) {
64
- let packageName = (0, shared_internals_1.packageName)(specifier);
65
- if (!packageName) {
66
- return specifier;
67
- }
68
- for (let [candidate, replacement] of Object.entries(opts.renameModules)) {
69
- if (candidate === specifier) {
70
- return replacement;
71
- }
72
- for (let extension of opts.resolvableExtensions) {
73
- if (candidate === specifier + '/index' + extension) {
74
- return replacement;
75
- }
76
- if (candidate === specifier + extension) {
77
- return replacement;
78
- }
79
- }
80
- }
81
- if (opts.renamePackages[packageName]) {
82
- return specifier.replace(packageName, opts.renamePackages[packageName]);
83
- }
84
- let pkg = sourceFile.owningPackage();
85
- if (!pkg || !pkg.isV2Ember()) {
86
- return specifier;
87
- }
88
- if (pkg.meta['auto-upgraded'] && pkg.name === packageName) {
89
- // we found a self-import, make it relative. Only auto-upgraded packages get
90
- // this help, v2 packages are natively supposed to use relative imports for
91
- // their own modules, and we want to push them all to do that correctly.
92
- let fullPath = specifier.replace(packageName, pkg.root);
93
- return (0, shared_internals_2.explicitRelative)((0, path_1.dirname)(sourceFile.name), fullPath);
94
- }
95
- let relocatedIntoPkg = sourceFile.relocatedIntoPackage();
96
- if (relocatedIntoPkg && pkg.meta['auto-upgraded'] && relocatedIntoPkg.name === packageName) {
97
- // a file that was relocated into a package does a self-import of that
98
- // package's name. This can happen when an addon (like ember-cli-mirage)
99
- // emits files from its own treeForApp that contain imports of the app's own
100
- // fully qualified name.
101
- let fullPath = specifier.replace(packageName, relocatedIntoPkg.root);
102
- return (0, shared_internals_2.explicitRelative)((0, path_1.dirname)(sourceFile.name), fullPath);
103
- }
104
- return specifier;
105
- }
106
- function isExplicitlyExternal(specifier, fromPkg) {
107
- return Boolean(fromPkg.isV2Addon() && fromPkg.meta['externals'] && fromPkg.meta['externals'].includes(specifier));
108
- }
109
- function isResolvable(packageName, fromPkg, appRoot) {
110
- try {
111
- let dep = shared_internals_2.PackageCache.shared('embroider-stage3', appRoot).resolve(packageName, fromPkg);
112
- if (!dep.isEmberPackage() && fromPkg.meta['auto-upgraded'] && !fromPkg.hasDependency('ember-auto-import')) {
113
- // classic ember addons can only import non-ember dependencies if they
114
- // have ember-auto-import.
115
- //
116
- // whereas native v2 packages can always import any dependency
117
- return false;
118
- }
119
- return dep;
120
- }
121
- catch (err) {
122
- if (err.code !== 'MODULE_NOT_FOUND') {
123
- throw err;
124
- }
125
- return false;
126
- }
127
- }
128
- const dynamicMissingModule = (0, js_handlebars_1.compile)(`
129
- throw new Error('Could not find module \`{{{js-string-escape moduleName}}}\`');
130
- `);
131
- const externalTemplate = (0, js_handlebars_1.compile)(`
132
- {{#if (eq runtimeName "require")}}
133
- const m = window.requirejs;
134
- {{else}}
135
- const m = window.require("{{{js-string-escape runtimeName}}}");
136
- {{/if}}
137
- {{!-
138
- There are plenty of hand-written AMD defines floating around
139
- that lack this, and they will break when other build systems
140
- encounter them.
141
-
142
- As far as I can tell, Ember's loader was already treating this
143
- case as a module, so in theory we aren't breaking anything by
144
- marking it as such when other packagers come looking.
145
-
146
- todo: get review on this part.
147
- -}}
148
- if (m.default && !m.__esModule) {
149
- m.__esModule = true;
150
- }
151
- module.exports = m;
152
- `);
153
- function handleExternal(specifier, sourceFile, opts, isDynamic) {
154
- let pkg = sourceFile.owningPackage();
155
- if (!pkg || !pkg.isV2Ember()) {
156
- return specifier;
157
- }
158
- let packageName = (0, shared_internals_1.packageName)(specifier);
159
- if (!packageName) {
160
- // This is a relative import. We don't automatically externalize those
161
- // because it's rare, and by keeping them static we give better errors. But
162
- // we do allow them to be explicitly externalized by the package author (or
163
- // a compat adapter). In the metadata, they would be listed in
164
- // package-relative form, so we need to convert this specifier to that.
165
- let absoluteSpecifier = (0, path_1.resolve)((0, path_1.dirname)(sourceFile.name), specifier);
166
- let packageRelativeSpecifier = (0, shared_internals_2.explicitRelative)(pkg.root, absoluteSpecifier);
167
- if (isExplicitlyExternal(packageRelativeSpecifier, pkg)) {
168
- let publicSpecifier = absoluteSpecifier.replace(pkg.root, pkg.name);
169
- return makeExternal(publicSpecifier, sourceFile, opts);
170
- }
171
- else {
172
- return specifier;
173
- }
174
- }
175
- // absolute package imports can also be explicitly external based on their
176
- // full specifier name
177
- if (isExplicitlyExternal(specifier, pkg)) {
178
- return makeExternal(specifier, sourceFile, opts);
179
- }
180
- if (!pkg.meta['auto-upgraded'] && shared_internals_1.emberVirtualPeerDeps.has(packageName)) {
181
- // Native v2 addons are allowed to use the emberVirtualPeerDeps like
182
- // `@glimmer/component`. And like all v2 addons, it's important that they
183
- // see those dependencies after those dependencies have been converted to
184
- // v2.
185
- //
186
- // But unlike auto-upgraded addons, native v2 addons are not necessarily
187
- // copied out of their original place in node_modules. And from that
188
- // original place they might accidentally resolve the emberVirtualPeerDeps
189
- // that are present there in v1 format.
190
- //
191
- // So before we even check isResolvable, we adjust these imports to point at
192
- // the app's copies instead.
193
- if (shared_internals_1.emberVirtualPeerDeps.has(packageName)) {
194
- if (!opts.activeAddons[packageName]) {
195
- throw new Error(`${pkg.name} is trying to import the app's ${packageName} package, but it seems to be missing`);
196
- }
197
- return (0, shared_internals_2.explicitRelative)((0, path_1.dirname)(sourceFile.name), specifier.replace(packageName, opts.activeAddons[packageName]));
198
- }
199
- }
200
- let relocatedPkg = sourceFile.relocatedIntoPackage();
201
- if (relocatedPkg) {
202
- // this file has been moved into another package (presumably the app).
203
- // first try to resolve from the destination package
204
- if (isResolvable(packageName, relocatedPkg, opts.appRoot)) {
205
- // self-imports are legal in the app tree, even for v2 packages.
206
- if (!pkg.meta['auto-upgraded'] && packageName !== pkg.name) {
207
- throw new Error(`${pkg.name} is trying to import ${packageName} from within its app tree. This is unsafe, because ${pkg.name} can't control which dependencies are resolvable from the app`);
208
- }
209
- return specifier;
210
- }
211
- else {
212
- // second try to resolve from the source package
213
- let targetPkg = isResolvable(packageName, pkg, opts.appRoot);
214
- if (targetPkg) {
215
- // self-imports are legal in the app tree, even for v2 packages.
216
- if (!pkg.meta['auto-upgraded'] && packageName !== pkg.name) {
217
- throw new Error(`${pkg.name} is trying to import ${packageName} from within its app tree. This is unsafe, because ${pkg.name} can't control which dependencies are resolvable from the app`);
218
- }
219
- // we found it, but we need to rewrite it because it's not really going to
220
- // resolve from where its sitting
221
- return (0, shared_internals_2.explicitRelative)((0, path_1.dirname)(sourceFile.name), specifier.replace(packageName, targetPkg.root));
222
- }
223
- }
224
- }
225
- else {
226
- if (isResolvable(packageName, pkg, opts.appRoot)) {
227
- if (!pkg.meta['auto-upgraded'] && !reliablyResolvable(pkg, packageName)) {
228
- throw new Error(`${pkg.name} is trying to import from ${packageName} but that is not one of its explicit dependencies`);
229
- }
230
- return specifier;
231
- }
232
- }
233
- // auto-upgraded packages can fall back to the set of known active addons
234
- //
235
- // v2 packages can fall back to the set of known active addons only to find
236
- // themselves (which is needed due to app tree merging)
237
- if ((pkg.meta['auto-upgraded'] || packageName === pkg.name) && opts.activeAddons[packageName]) {
238
- return (0, shared_internals_2.explicitRelative)((0, path_1.dirname)(sourceFile.name), specifier.replace(packageName, opts.activeAddons[packageName]));
239
- }
240
- if (pkg.meta['auto-upgraded']) {
241
- // auto-upgraded packages can fall back to attempting to find dependencies at
242
- // runtime. Native v2 packages can only get this behavior in the
243
- // isExplicitlyExternal case above because they need to explicitly ask for
244
- // externals.
245
- return makeExternal(specifier, sourceFile, opts);
246
- }
247
- else {
248
- // native v2 packages don't automatically externalize *everything* the way
249
- // auto-upgraded packages do, but they still externalize known and approved
250
- // ember virtual packages (like @ember/component)
251
- if (shared_internals_1.emberVirtualPackages.has(packageName)) {
252
- return makeExternal(specifier, sourceFile, opts);
253
- }
254
- }
255
- // non-resolvable imports in dynamic positions become runtime errors, not
256
- // build-time errors, so we emit the runtime error module here before the
257
- // stage3 packager has a chance to see the missing module. (Maybe some stage3
258
- // packagers will have this behavior by default, because it would make sense,
259
- // but webpack at least does not.)
260
- if (isDynamic) {
261
- return makeMissingModule(specifier, sourceFile, opts);
262
- }
263
- // this is falling through with the original specifier which was
264
- // non-resolvable, which will presumably cause a static build error in stage3.
265
- return specifier;
266
- }
267
- function makeMissingModule(specifier, sourceFile, opts) {
268
- let target = (0, path_1.join)(opts.externalsDir, 'missing', specifier + '.js');
269
- atomicWrite(target, dynamicMissingModule({
270
- moduleName: specifier,
271
- }));
272
- return (0, shared_internals_2.explicitRelative)((0, path_1.dirname)(sourceFile.name), target.slice(0, -3));
273
- }
274
- function makeExternal(specifier, sourceFile, opts) {
275
- let target = (0, path_1.join)(opts.externalsDir, specifier + '.js');
276
- atomicWrite(target, externalTemplate({
277
- runtimeName: specifier,
278
- }));
279
- return (0, shared_internals_2.explicitRelative)((0, path_1.dirname)(sourceFile.name), target.slice(0, -3));
280
- }
281
- function atomicWrite(path, content) {
282
- if ((0, fs_extra_1.pathExistsSync)(path)) {
283
- return;
284
- }
285
- let suffix = (0, crypto_1.randomBytes)(8).toString('hex');
286
- (0, fs_extra_1.outputFileSync)(path + suffix, content);
287
- try {
288
- (0, fs_extra_1.renameSync)(path + suffix, path);
289
- }
290
- catch (err) {
291
- // windows throws EPERM for concurrent access. For us it's not an error
292
- // condition because the other thread is writing the exact same value we
293
- // would have.
294
- if (err.code !== 'EPERM') {
295
- throw err;
296
- }
297
- }
298
- }
299
- function main(babel) {
300
- let t = babel.types;
301
- return {
302
- visitor: {
303
- Program: {
304
- enter(path, state) {
305
- let opts = ensureOpts(state);
306
- state.adjustFile = new AdjustFile(path.hub.file.opts.filename, opts.relocatedFiles, opts.appRoot);
307
- let adder = new babel_import_util_1.ImportUtil(t, path);
308
- addExtraImports(adder, t, path, opts.extraImports);
309
- },
310
- exit(path, state) {
311
- for (let child of path.get('body')) {
312
- if (child.isImportDeclaration() || child.isExportNamedDeclaration() || child.isExportAllDeclaration()) {
313
- rewriteTopLevelImport(child, state);
314
- }
315
- }
316
- },
317
- },
318
- CallExpression(path, state) {
319
- if (isImportSyncExpression(t, path) || isDynamicImportExpression(t, path)) {
320
- const [source] = path.get('arguments');
321
- let opts = ensureOpts(state);
322
- let specifier = adjustSpecifier(source.node.value, state.adjustFile, opts, true);
323
- source.replaceWith(t.stringLiteral(specifier));
324
- return;
325
- }
326
- // Should/can we make this early exit when the first define was found?
327
- if (!isDefineExpression(t, path)) {
328
- return;
329
- }
330
- let pkg = state.adjustFile.owningPackage();
331
- if (pkg && pkg.isV2Ember() && !pkg.meta['auto-upgraded']) {
332
- throw new Error(`The file ${state.adjustFile.originalFile} in package ${pkg.name} tried to use AMD define. Native V2 Ember addons are forbidden from using AMD define, they must use ECMA export only.`);
333
- }
334
- let opts = ensureOpts(state);
335
- const dependencies = path.node.arguments[1];
336
- const specifiers = dependencies.elements.slice();
337
- specifiers.push(path.node.arguments[0]);
338
- for (let source of specifiers) {
339
- if (!source) {
340
- continue;
341
- }
342
- if (source.type !== 'StringLiteral') {
343
- throw path.buildCodeFrameError(`expected only string literal arguments`);
344
- }
345
- if (source.value === 'exports' || source.value === 'require') {
346
- // skip "special" AMD dependencies
347
- continue;
348
- }
349
- let specifier = adjustSpecifier(source.value, state.adjustFile, opts, false);
350
- if (specifier !== source.value) {
351
- source.value = specifier;
352
- }
353
- }
354
- },
355
- },
356
- };
357
- }
358
- exports.default = main;
359
- function rewriteTopLevelImport(path, state) {
360
- let opts = ensureOpts(state);
361
- const { source } = path.node;
362
- if (source === null || source === undefined) {
363
- return;
364
- }
365
- let specifier = adjustSpecifier(source.value, state.adjustFile, opts, false);
366
- if (specifier !== source.value) {
367
- source.value = specifier;
368
- }
369
- }
370
- main.baseDir = function () {
371
- return (0, path_1.join)(__dirname, '..');
372
- };
373
- function addExtraImports(adder, t, path, extraImports) {
374
- for (let { absPath, target, runtimeName } of extraImports) {
375
- if (absPath === path.hub.file.opts.filename) {
376
- if (runtimeName) {
377
- path.node.body.unshift(amdDefine(t, adder, path, target, runtimeName));
378
- }
379
- else {
380
- adder.importForSideEffect(target);
381
- }
382
- }
383
- }
384
- }
385
- function amdDefine(t, adder, path, target, runtimeName) {
386
- let value = t.callExpression(adder.import(path, '@embroider/macros', 'importSync'), [t.stringLiteral(target)]);
387
- return t.expressionStatement(t.callExpression(t.memberExpression(t.identifier('window'), t.identifier('define')), [
388
- t.stringLiteral(runtimeName),
389
- t.functionExpression(null, [], t.blockStatement([t.returnStatement(value)])),
390
- ]));
391
- }
392
- class AdjustFile {
393
- constructor(name, relocatedFiles, appRoot) {
394
- this.name = name;
395
- this.packageCache = shared_internals_2.PackageCache.shared('embroider-stage3', appRoot);
396
- if (!name) {
397
- throw new Error(`bug: adjust-imports plugin was run without a filename`);
398
- }
399
- this.originalFile = relocatedFiles[name] || name;
400
- }
401
- get isRelocated() {
402
- return this.originalFile !== this.name;
403
- }
404
- owningPackage() {
405
- return this.packageCache.ownerOfFile(this.originalFile);
406
- }
407
- relocatedIntoPackage() {
408
- if (this.isRelocated) {
409
- let owning = this.packageCache.ownerOfFile(this.name);
410
- if (owning && !owning.isV2Ember()) {
411
- throw new Error(`bug: it should only be possible to get relocated into a v2 ember package here`);
412
- }
413
- return owning;
414
- }
415
- }
416
- }
417
- __decorate([
418
- (0, typescript_memoize_1.Memoize)()
419
- ], AdjustFile.prototype, "owningPackage", null);
420
- __decorate([
421
- (0, typescript_memoize_1.Memoize)()
422
- ], AdjustFile.prototype, "relocatedIntoPackage", null);
423
- function ensureOpts(state) {
424
- let { opts } = state;
425
- if ('adjustImportsOptionsPath' in opts) {
426
- // eslint-disable-next-line @typescript-eslint/no-require-imports
427
- return (state.opts = { ...require(opts.adjustImportsOptionsPath), ...require(opts.relocatedFilesPath) });
428
- }
429
- return opts;
430
- }
431
- // we don't want to allow things that resolve only by accident that are likely
432
- // to break in other setups. For example: import your dependencies'
433
- // dependencies, or importing your own name from within a monorepo (which will
434
- // work because of the symlinking) without setting up "exports" (which makes
435
- // your own name reliably resolvable)
436
- function reliablyResolvable(pkg, packageName) {
437
- if (pkg.hasDependency(packageName)) {
438
- return true;
439
- }
440
- if (pkg.name === packageName && pkg.packageJSON.exports) {
441
- return true;
442
- }
443
- if (shared_internals_1.emberVirtualPeerDeps.has(packageName)) {
444
- return true;
445
- }
446
- return false;
447
- }
448
- //# sourceMappingURL=babel-plugin-adjust-imports.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"babel-plugin-adjust-imports.js","sourceRoot":"","sources":["babel-plugin-adjust-imports.ts"],"names":[],"mappings":";;;;;;;;;AAAA,kEAAwH;AACxH,+BAA8C;AAI9C,kEAAiG;AACjG,2DAA6C;AAC7C,mDAA0C;AAC1C,yDAA+C;AAC/C,mCAAqC;AACrC,uCAAsE;AAyCtE,SAAgB,sBAAsB,CAAC,CAAa,EAAE,IAAmB;IACvE,IACE,CAAC,IAAI;QACL,CAAC,IAAI,CAAC,gBAAgB,EAAE;QACxB,IAAI,CAAC,IAAI,CAAC,MAAM,CAAC,IAAI,KAAK,YAAY;QACtC,CAAC,IAAI,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC,gBAAgB,CAAC,mBAAmB,EAAE,YAAY,CAAC,EACvE;QACA,OAAO,KAAK,CAAC;KACd;IAED,MAAM,IAAI,GAAG,IAAI,CAAC,IAAI,CAAC,SAAS,CAAC;IACjC,OAAO,KAAK,CAAC,OAAO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AC,+EAA+E,CAAC,CAAC;aAClG;YACD,OAAO,MAAM,CAAC;SACf;IACH,CAAC;CACF;AAdC;IADC,IAAA,4BAAO,GAAE;+CAGT;AAGD;IADC,IAAA,4BAAO,GAAE;sDAST;AAGH,SAAS,UAAU,CAAC,KAAY;IAC9B,IAAI,EAAE,IAAI,EAAE,GAAG,KAAK,CAAC;IACrB,IAAI,0BAA0B,IAAI,IAAI,EAAE;QACtC,iEAAiE;QACjE,OAAO,CAAC,KAAK,CAAC,IAAI,GAAG,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,wBAAwB,CAAC,EAAE,GAAG,OAAO,CAAC,IAAI,CAAC,kBAAkB,CAAC,EAAE,CAAC,CAAC;KAC1G;IACD,OAAO,IAAI,CAAC;AACd,CAAC;AAED,8EAA8E;AAC9E,mEAAmE;AACnE,8EAA8E;AAC9E,4EAA4E;AAC5E,qCAAqC;AACrC,SAAS,kBAAkB,CAAC,GAAc,EAAE,WAAmB;IAC7D,IAAI,GAAG,CAAC,aAAa,CAAC,WAAW,CAAC,EAAE;QAClC,OAAO,IAAI,CAAC;KACb;IAED,IAAI,GAAG,CAAC,IAAI,KAAK,WAAW,IAAI,GAAG,CAAC,WAAW,CAAC,OAAO,EAAE;QACvD,OAAO,IAAI,CAAC;KACb;IAED,IAAI,uCAAoB,CAAC,GAAG,CAAC,WAAW,CAAC,EAAE;QACzC,OAAO,IAAI,CAAC;KACb;IAED,OAAO,KAAK,CAAC;AACf,CAAC","sourcesContent":["import { emberVirtualPackages, emberVirtualPeerDeps, packageName as getPackageName } from '@embroider/shared-internals';\nimport { join, dirname, resolve } from 'path';\nimport type { NodePath } from '@babel/traverse';\nimport type * as Babel from '@babel/core';\nimport type { types as t } from '@babel/core';\nimport { PackageCache, Package, V2Package, explicitRelative } from '@embroider/shared-internals';\nimport { Memoize } from 'typescript-memoize';\nimport { compile } from './js-handlebars';\nimport { ImportUtil } from 'babel-import-util';\nimport { randomBytes } from 'crypto';\nimport { outputFileSync, pathExistsSync, renameSync } from 'fs-extra';\n\ninterface State {\n adjustFile: AdjustFile;\n opts: Options | DeflatedOptions;\n}\n\nexport interface DeflatedOptions {\n adjustImportsOptionsPath: string;\n relocatedFilesPath: string;\n}\n\ntype BabelTypes = typeof t;\n\nexport interface Options {\n renamePackages: {\n [fromName: string]: string;\n };\n renameModules: {\n [fromName: string]: string;\n };\n extraImports: {\n absPath: string;\n target: string;\n runtimeName?: string;\n }[];\n externalsDir: string;\n activeAddons: {\n [packageName: string]: string;\n };\n relocatedFiles: { [relativePath: string]: string };\n resolvableExtensions: string[];\n appRoot: string;\n}\n\ntype DefineExpressionPath = NodePath<t.CallExpression> & {\n node: t.CallExpression & {\n arguments: [t.StringLiteral, t.ArrayExpression, Function];\n };\n};\n\nexport function isImportSyncExpression(t: BabelTypes, path: NodePath<any>) {\n if (\n !path ||\n !path.isCallExpression() ||\n path.node.callee.type !== 'Identifier' ||\n !path.get('callee').referencesImport('@embroider/macros', 'importSync')\n ) {\n return false;\n }\n\n const args = path.node.arguments;\n return Array.isArray(args) && args.length === 1 && t.isStringLiteral(args[0]);\n}\n\nexport function isDynamicImportExpression(t: BabelTypes, path: NodePath<any>) {\n if (!path || !path.isCallExpression() || path.node.callee.type !== 'Import') {\n return false;\n }\n\n const args = path.node.arguments;\n return Array.isArray(args) && args.length === 1 && t.isStringLiteral(args[0]);\n}\n\nexport function isDefineExpression(t: BabelTypes, path: NodePath<any>): path is DefineExpressionPath {\n // should we allow nested defines, or stop at the top level?\n if (!path.isCallExpression() || path.node.callee.type !== 'Identifier' || path.node.callee.name !== 'define') {\n return false;\n }\n\n const args = path.node.arguments;\n\n // only match define with 3 arguments define(name: string, deps: string[], cb: Function);\n return (\n Array.isArray(args) &&\n args.length === 3 &&\n t.isStringLiteral(args[0]) &&\n t.isArrayExpression(args[1]) &&\n t.isFunction(args[2])\n );\n}\n\nfunction adjustSpecifier(specifier: string, file: AdjustFile, opts: Options, isDynamic: boolean) {\n if (specifier === '@embroider/macros') {\n // the macros package is always handled directly within babel (not\n // necessarily as a real resolvable package), so we should not mess with it.\n // It might not get compiled away until *after* our plugin has run, which is\n // why we need to know about it.\n return specifier;\n }\n\n specifier = handleRenaming(specifier, file, opts);\n specifier = handleExternal(specifier, file, opts, isDynamic);\n return specifier;\n}\n\nfunction handleRenaming(specifier: string, sourceFile: AdjustFile, opts: Options) {\n let packageName = getPackageName(specifier);\n if (!packageName) {\n return specifier;\n }\n\n for (let [candidate, replacement] of Object.entries(opts.renameModules)) {\n if (candidate === specifier) {\n return replacement;\n }\n for (let extension of opts.resolvableExtensions) {\n if (candidate === specifier + '/index' + extension) {\n return replacement;\n }\n if (candidate === specifier + extension) {\n return replacement;\n }\n }\n }\n\n if (opts.renamePackages[packageName]) {\n return specifier.replace(packageName, opts.renamePackages[packageName]);\n }\n\n let pkg = sourceFile.owningPackage();\n if (!pkg || !pkg.isV2Ember()) {\n return specifier;\n }\n\n if (pkg.meta['auto-upgraded'] && pkg.name === packageName) {\n // we found a self-import, make it relative. Only auto-upgraded packages get\n // this help, v2 packages are natively supposed to use relative imports for\n // their own modules, and we want to push them all to do that correctly.\n let fullPath = specifier.replace(packageName, pkg.root);\n return explicitRelative(dirname(sourceFile.name), fullPath);\n }\n\n let relocatedIntoPkg = sourceFile.relocatedIntoPackage();\n if (relocatedIntoPkg && pkg.meta['auto-upgraded'] && relocatedIntoPkg.name === packageName) {\n // a file that was relocated into a package does a self-import of that\n // package's name. This can happen when an addon (like ember-cli-mirage)\n // emits files from its own treeForApp that contain imports of the app's own\n // fully qualified name.\n let fullPath = specifier.replace(packageName, relocatedIntoPkg.root);\n return explicitRelative(dirname(sourceFile.name), fullPath);\n }\n\n return specifier;\n}\n\nfunction isExplicitlyExternal(specifier: string, fromPkg: V2Package): boolean {\n return Boolean(fromPkg.isV2Addon() && fromPkg.meta['externals'] && fromPkg.meta['externals'].includes(specifier));\n}\n\nfunction isResolvable(packageName: string, fromPkg: V2Package, appRoot: string): false | Package {\n try {\n let dep = PackageCache.shared('embroider-stage3', appRoot).resolve(packageName, fromPkg);\n if (!dep.isEmberPackage() && fromPkg.meta['auto-upgraded'] && !fromPkg.hasDependency('ember-auto-import')) {\n // classic ember addons can only import non-ember dependencies if they\n // have ember-auto-import.\n //\n // whereas native v2 packages can always import any dependency\n return false;\n }\n return dep;\n } catch (err) {\n if (err.code !== 'MODULE_NOT_FOUND') {\n throw err;\n }\n return false;\n }\n}\n\nconst dynamicMissingModule = compile(`\n throw new Error('Could not find module \\`{{{js-string-escape moduleName}}}\\`');\n`) as (params: { moduleName: string }) => string;\n\nconst externalTemplate = compile(`\n{{#if (eq runtimeName \"require\")}}\nconst m = window.requirejs;\n{{else}}\nconst m = window.require(\"{{{js-string-escape runtimeName}}}\");\n{{/if}}\n{{!-\n There are plenty of hand-written AMD defines floating around\n that lack this, and they will break when other build systems\n encounter them.\n\n As far as I can tell, Ember's loader was already treating this\n case as a module, so in theory we aren't breaking anything by\n marking it as such when other packagers come looking.\n\n todo: get review on this part.\n-}}\nif (m.default && !m.__esModule) {\n m.__esModule = true;\n}\nmodule.exports = m;\n`) as (params: { runtimeName: string }) => string;\n\nfunction handleExternal(specifier: string, sourceFile: AdjustFile, opts: Options, isDynamic: boolean): string {\n let pkg = sourceFile.owningPackage();\n if (!pkg || !pkg.isV2Ember()) {\n return specifier;\n }\n\n let packageName = getPackageName(specifier);\n if (!packageName) {\n // This is a relative import. We don't automatically externalize those\n // because it's rare, and by keeping them static we give better errors. But\n // we do allow them to be explicitly externalized by the package author (or\n // a compat adapter). In the metadata, they would be listed in\n // package-relative form, so we need to convert this specifier to that.\n let absoluteSpecifier = resolve(dirname(sourceFile.name), specifier);\n let packageRelativeSpecifier = explicitRelative(pkg.root, absoluteSpecifier);\n if (isExplicitlyExternal(packageRelativeSpecifier, pkg)) {\n let publicSpecifier = absoluteSpecifier.replace(pkg.root, pkg.name);\n return makeExternal(publicSpecifier, sourceFile, opts);\n } else {\n return specifier;\n }\n }\n\n // absolute package imports can also be explicitly external based on their\n // full specifier name\n if (isExplicitlyExternal(specifier, pkg)) {\n return makeExternal(specifier, sourceFile, opts);\n }\n\n if (!pkg.meta['auto-upgraded'] && emberVirtualPeerDeps.has(packageName)) {\n // Native v2 addons are allowed to use the emberVirtualPeerDeps like\n // `@glimmer/component`. And like all v2 addons, it's important that they\n // see those dependencies after those dependencies have been converted to\n // v2.\n //\n // But unlike auto-upgraded addons, native v2 addons are not necessarily\n // copied out of their original place in node_modules. And from that\n // original place they might accidentally resolve the emberVirtualPeerDeps\n // that are present there in v1 format.\n //\n // So before we even check isResolvable, we adjust these imports to point at\n // the app's copies instead.\n if (emberVirtualPeerDeps.has(packageName)) {\n if (!opts.activeAddons[packageName]) {\n throw new Error(`${pkg.name} is trying to import the app's ${packageName} package, but it seems to be missing`);\n }\n return explicitRelative(dirname(sourceFile.name), specifier.replace(packageName, opts.activeAddons[packageName]));\n }\n }\n\n let relocatedPkg = sourceFile.relocatedIntoPackage();\n if (relocatedPkg) {\n // this file has been moved into another package (presumably the app).\n\n // first try to resolve from the destination package\n if (isResolvable(packageName, relocatedPkg, opts.appRoot)) {\n // self-imports are legal in the app tree, even for v2 packages.\n if (!pkg.meta['auto-upgraded'] && packageName !== pkg.name) {\n throw new Error(\n `${pkg.name} is trying to import ${packageName} from within its app tree. This is unsafe, because ${pkg.name} can't control which dependencies are resolvable from the app`\n );\n }\n return specifier;\n } else {\n // second try to resolve from the source package\n let targetPkg = isResolvable(packageName, pkg, opts.appRoot);\n if (targetPkg) {\n // self-imports are legal in the app tree, even for v2 packages.\n if (!pkg.meta['auto-upgraded'] && packageName !== pkg.name) {\n throw new Error(\n `${pkg.name} is trying to import ${packageName} from within its app tree. This is unsafe, because ${pkg.name} can't control which dependencies are resolvable from the app`\n );\n }\n // we found it, but we need to rewrite it because it's not really going to\n // resolve from where its sitting\n return explicitRelative(dirname(sourceFile.name), specifier.replace(packageName, targetPkg.root));\n }\n }\n } else {\n if (isResolvable(packageName, pkg, opts.appRoot)) {\n if (!pkg.meta['auto-upgraded'] && !reliablyResolvable(pkg, packageName)) {\n throw new Error(\n `${pkg.name} is trying to import from ${packageName} but that is not one of its explicit dependencies`\n );\n }\n return specifier;\n }\n }\n\n // auto-upgraded packages can fall back to the set of known active addons\n //\n // v2 packages can fall back to the set of known active addons only to find\n // themselves (which is needed due to app tree merging)\n if ((pkg.meta['auto-upgraded'] || packageName === pkg.name) && opts.activeAddons[packageName]) {\n return explicitRelative(dirname(sourceFile.name), specifier.replace(packageName, opts.activeAddons[packageName]));\n }\n\n if (pkg.meta['auto-upgraded']) {\n // auto-upgraded packages can fall back to attempting to find dependencies at\n // runtime. Native v2 packages can only get this behavior in the\n // isExplicitlyExternal case above because they need to explicitly ask for\n // externals.\n return makeExternal(specifier, sourceFile, opts);\n } else {\n // native v2 packages don't automatically externalize *everything* the way\n // auto-upgraded packages do, but they still externalize known and approved\n // ember virtual packages (like @ember/component)\n if (emberVirtualPackages.has(packageName)) {\n return makeExternal(specifier, sourceFile, opts);\n }\n }\n\n // non-resolvable imports in dynamic positions become runtime errors, not\n // build-time errors, so we emit the runtime error module here before the\n // stage3 packager has a chance to see the missing module. (Maybe some stage3\n // packagers will have this behavior by default, because it would make sense,\n // but webpack at least does not.)\n if (isDynamic) {\n return makeMissingModule(specifier, sourceFile, opts);\n }\n\n // this is falling through with the original specifier which was\n // non-resolvable, which will presumably cause a static build error in stage3.\n return specifier;\n}\n\nfunction makeMissingModule(specifier: string, sourceFile: AdjustFile, opts: Options): string {\n let target = join(opts.externalsDir, 'missing', specifier + '.js');\n atomicWrite(\n target,\n dynamicMissingModule({\n moduleName: specifier,\n })\n );\n return explicitRelative(dirname(sourceFile.name), target.slice(0, -3));\n}\n\nfunction makeExternal(specifier: string, sourceFile: AdjustFile, opts: Options): string {\n let target = join(opts.externalsDir, specifier + '.js');\n atomicWrite(\n target,\n externalTemplate({\n runtimeName: specifier,\n })\n );\n return explicitRelative(dirname(sourceFile.name), target.slice(0, -3));\n}\n\nfunction atomicWrite(path: string, content: string) {\n if (pathExistsSync(path)) {\n return;\n }\n let suffix = randomBytes(8).toString('hex');\n outputFileSync(path + suffix, content);\n try {\n renameSync(path + suffix, path);\n } catch (err: any) {\n // windows throws EPERM for concurrent access. For us it's not an error\n // condition because the other thread is writing the exact same value we\n // would have.\n if (err.code !== 'EPERM') {\n throw err;\n }\n }\n}\n\nexport default function main(babel: typeof Babel) {\n let t = babel.types;\n return {\n visitor: {\n Program: {\n enter(path: NodePath<t.Program>, state: State) {\n let opts = ensureOpts(state);\n state.adjustFile = new AdjustFile(path.hub.file.opts.filename, opts.relocatedFiles, opts.appRoot);\n let adder = new ImportUtil(t, path);\n addExtraImports(adder, t, path, opts.extraImports);\n },\n exit(path: NodePath<t.Program>, state: State) {\n for (let child of path.get('body')) {\n if (child.isImportDeclaration() || child.isExportNamedDeclaration() || child.isExportAllDeclaration()) {\n rewriteTopLevelImport(child, state);\n }\n }\n },\n },\n CallExpression(path: NodePath<t.CallExpression>, state: State) {\n if (isImportSyncExpression(t, path) || isDynamicImportExpression(t, path)) {\n const [source] = path.get('arguments');\n let opts = ensureOpts(state);\n let specifier = adjustSpecifier((source.node as any).value, state.adjustFile, opts, true);\n source.replaceWith(t.stringLiteral(specifier));\n return;\n }\n\n // Should/can we make this early exit when the first define was found?\n if (!isDefineExpression(t, path)) {\n return;\n }\n\n let pkg = state.adjustFile.owningPackage();\n if (pkg && pkg.isV2Ember() && !pkg.meta['auto-upgraded']) {\n throw new Error(\n `The file ${state.adjustFile.originalFile} in package ${pkg.name} tried to use AMD define. Native V2 Ember addons are forbidden from using AMD define, they must use ECMA export only.`\n );\n }\n\n let opts = ensureOpts(state);\n\n const dependencies = path.node.arguments[1];\n\n const specifiers = dependencies.elements.slice();\n specifiers.push(path.node.arguments[0]);\n\n for (let source of specifiers) {\n if (!source) {\n continue;\n }\n\n if (source.type !== 'StringLiteral') {\n throw path.buildCodeFrameError(`expected only string literal arguments`);\n }\n\n if (source.value === 'exports' || source.value === 'require') {\n // skip \"special\" AMD dependencies\n continue;\n }\n\n let specifier = adjustSpecifier(source.value, state.adjustFile, opts, false);\n\n if (specifier !== source.value) {\n source.value = specifier;\n }\n }\n },\n },\n };\n}\n\nfunction rewriteTopLevelImport(\n path: NodePath<t.ImportDeclaration | t.ExportNamedDeclaration | t.ExportAllDeclaration>,\n state: State\n) {\n let opts = ensureOpts(state);\n const { source } = path.node;\n if (source === null || source === undefined) {\n return;\n }\n\n let specifier = adjustSpecifier(source.value, state.adjustFile, opts, false);\n if (specifier !== source.value) {\n source.value = specifier;\n }\n}\n\n(main as any).baseDir = function () {\n return join(__dirname, '..');\n};\n\nfunction addExtraImports(\n adder: ImportUtil,\n t: BabelTypes,\n path: NodePath<t.Program>,\n extraImports: Required<Options>['extraImports']\n) {\n for (let { absPath, target, runtimeName } of extraImports) {\n if (absPath === path.hub.file.opts.filename) {\n if (runtimeName) {\n path.node.body.unshift(amdDefine(t, adder, path, target, runtimeName));\n } else {\n adder.importForSideEffect(target);\n }\n }\n }\n}\n\nfunction amdDefine(t: BabelTypes, adder: ImportUtil, path: NodePath<t.Program>, target: string, runtimeName: string) {\n let value = t.callExpression(adder.import(path, '@embroider/macros', 'importSync'), [t.stringLiteral(target)]);\n return t.expressionStatement(\n t.callExpression(t.memberExpression(t.identifier('window'), t.identifier('define')), [\n t.stringLiteral(runtimeName),\n t.functionExpression(null, [], t.blockStatement([t.returnStatement(value)])),\n ])\n );\n}\n\nclass AdjustFile {\n readonly originalFile: string;\n private packageCache: PackageCache;\n\n constructor(public name: string, relocatedFiles: Options['relocatedFiles'], appRoot: string) {\n this.packageCache = PackageCache.shared('embroider-stage3', appRoot);\n if (!name) {\n throw new Error(`bug: adjust-imports plugin was run without a filename`);\n }\n this.originalFile = relocatedFiles[name] || name;\n }\n\n get isRelocated() {\n return this.originalFile !== this.name;\n }\n\n @Memoize()\n owningPackage(): Package | undefined {\n return this.packageCache.ownerOfFile(this.originalFile);\n }\n\n @Memoize()\n relocatedIntoPackage(): V2Package | undefined {\n if (this.isRelocated) {\n let owning = this.packageCache.ownerOfFile(this.name);\n if (owning && !owning.isV2Ember()) {\n throw new Error(`bug: it should only be possible to get relocated into a v2 ember package here`);\n }\n return owning;\n }\n }\n}\n\nfunction ensureOpts(state: State): Options {\n let { opts } = state;\n if ('adjustImportsOptionsPath' in opts) {\n // eslint-disable-next-line @typescript-eslint/no-require-imports\n return (state.opts = { ...require(opts.adjustImportsOptionsPath), ...require(opts.relocatedFilesPath) });\n }\n return opts;\n}\n\n// we don't want to allow things that resolve only by accident that are likely\n// to break in other setups. For example: import your dependencies'\n// dependencies, or importing your own name from within a monorepo (which will\n// work because of the symlinking) without setting up \"exports\" (which makes\n// your own name reliably resolvable)\nfunction reliablyResolvable(pkg: V2Package, packageName: string) {\n if (pkg.hasDependency(packageName)) {\n return true;\n }\n\n if (pkg.name === packageName && pkg.packageJSON.exports) {\n return true;\n }\n\n if (emberVirtualPeerDeps.has(packageName)) {\n return true;\n }\n\n return false;\n}\n"]}