@embroider/core 2.0.2 → 2.1.1-unstable.21eae41

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.
@@ -0,0 +1,589 @@
1
+ "use strict";
2
+ var __importDefault = (this && this.__importDefault) || function (mod) {
3
+ return (mod && mod.__esModule) ? mod : { "default": mod };
4
+ };
5
+ Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.Resolver = void 0;
7
+ const shared_internals_1 = require("@embroider/shared-internals");
8
+ const path_1 = require("path");
9
+ const shared_internals_2 = require("@embroider/shared-internals");
10
+ const debug_1 = __importDefault(require("debug"));
11
+ const assert_never_1 = __importDefault(require("assert-never"));
12
+ const resolve_1 = __importDefault(require("resolve"));
13
+ const virtual_content_1 = require("./virtual-content");
14
+ const debug = (0, debug_1.default)('embroider:resolver');
15
+ function logTransition(reason, before, after = before) {
16
+ if (after.isVirtual) {
17
+ debug(`virtualized %s in %s because %s`, before.specifier, before.fromFile, reason);
18
+ }
19
+ else if (before.specifier !== after.specifier) {
20
+ if (before.fromFile !== after.fromFile) {
21
+ debug(`aliased and rehomed: %s to %s, from %s to %s because %s`, before.specifier, after.specifier, before.fromFile, after.fromFile, reason);
22
+ }
23
+ else {
24
+ debug(`aliased: %s to %s in %s because`, before.specifier, after.specifier, before.fromFile, reason);
25
+ }
26
+ }
27
+ else if (before.fromFile !== after.fromFile) {
28
+ debug(`rehomed: %s from %s to %s because`, before.specifier, before.fromFile, after.fromFile, reason);
29
+ }
30
+ else {
31
+ debug(`unchanged: %s in %s because %s`, before.specifier, before.fromFile, reason);
32
+ }
33
+ return after;
34
+ }
35
+ const compatPattern = /#embroider_compat\/(?<type>[^\/]+)\/(?<rest>.*)/;
36
+ class NodeModuleRequest {
37
+ constructor(specifier, fromFile, isVirtual = false) {
38
+ this.specifier = specifier;
39
+ this.fromFile = fromFile;
40
+ this.isVirtual = isVirtual;
41
+ }
42
+ alias(specifier) {
43
+ return new NodeModuleRequest(specifier, this.fromFile);
44
+ }
45
+ rehome(fromFile) {
46
+ return new NodeModuleRequest(this.specifier, fromFile);
47
+ }
48
+ virtualize(filename) {
49
+ return new NodeModuleRequest(filename, this.fromFile, true);
50
+ }
51
+ }
52
+ class Resolver {
53
+ constructor(options) {
54
+ this.options = options;
55
+ }
56
+ beforeResolve(request) {
57
+ if (request.specifier === '@embroider/macros') {
58
+ // the macros package is always handled directly within babel (not
59
+ // necessarily as a real resolvable package), so we should not mess with it.
60
+ // It might not get compiled away until *after* our plugin has run, which is
61
+ // why we need to know about it.
62
+ return logTransition('early exit', request);
63
+ }
64
+ request = this.handleGlobalsCompat(request);
65
+ request = this.handleRenaming(request);
66
+ return this.preHandleExternal(request);
67
+ }
68
+ // This encapsulates the whole resolving process. Given a `defaultResolve`
69
+ // that calls your build system's normal module resolver, this does both pre-
70
+ // and post-resolution adjustments as needed to implement our compatibility
71
+ // rules.
72
+ //
73
+ // Depending on the plugin architecture you're working in, it may be easier to
74
+ // call beforeResolve and fallbackResolve directly, in which case matching the
75
+ // details of the recursion to what this method does are your responsibility.
76
+ async resolve(request, defaultResolve) {
77
+ let gen = this.internalResolve(request, defaultResolve);
78
+ let out = gen.next();
79
+ while (!out.done) {
80
+ out = gen.next(await out.value);
81
+ }
82
+ return out.value;
83
+ }
84
+ // synchronous alternative to resolve() above. Because our own internals are
85
+ // all synchronous, you can use this if your defaultResolve function is
86
+ // synchronous. At present, we need this for the case where we are compiling
87
+ // non-strict templates and doing component resolutions inside the template
88
+ // compiler inside babel, which is a synchronous context.
89
+ resolveSync(request, defaultResolve) {
90
+ let gen = this.internalResolve(request, defaultResolve);
91
+ let out = gen.next();
92
+ while (!out.done) {
93
+ out = gen.next(out.value);
94
+ }
95
+ return out.value;
96
+ }
97
+ // Our core implementation is a generator so it can power both resolve() and
98
+ // resolveSync()
99
+ *internalResolve(request, defaultResolve) {
100
+ request = this.beforeResolve(request);
101
+ let resolution = yield defaultResolve(request);
102
+ switch (resolution.type) {
103
+ case 'found':
104
+ return resolution;
105
+ case 'not_found':
106
+ break;
107
+ default:
108
+ throw (0, assert_never_1.default)(resolution);
109
+ }
110
+ let nextRequest = this.fallbackResolve(request);
111
+ if (nextRequest === request) {
112
+ // no additional fallback is available.
113
+ return resolution;
114
+ }
115
+ if (nextRequest.isVirtual) {
116
+ // virtual requests are terminal, there is no more beforeResolve or
117
+ // fallbackResolve around them. The defaultResolve is expected to know how
118
+ // to implement them.
119
+ return yield defaultResolve(nextRequest);
120
+ }
121
+ return yield* this.internalResolve(nextRequest, defaultResolve);
122
+ }
123
+ // Use standard NodeJS resolving, with our required compatibility rules on
124
+ // top. This is a convenience method for calling resolveSync with the
125
+ // defaultResolve already configured to be "do the normal node thing".
126
+ nodeResolve(specifier, fromFile) {
127
+ let resolution = this.resolveSync(new NodeModuleRequest(specifier, fromFile), request => {
128
+ if (request.isVirtual) {
129
+ return {
130
+ type: 'found',
131
+ result: {
132
+ type: 'virtual',
133
+ content: (0, virtual_content_1.virtualContent)(request.specifier),
134
+ filename: request.specifier,
135
+ },
136
+ };
137
+ }
138
+ try {
139
+ let filename = resolve_1.default.sync(request.specifier, {
140
+ basedir: (0, path_1.dirname)(request.fromFile),
141
+ extensions: this.options.resolvableExtensions,
142
+ });
143
+ return { type: 'found', result: { type: 'real', filename } };
144
+ }
145
+ catch (err) {
146
+ if (err.code !== 'MODULE_NOT_FOUND') {
147
+ throw err;
148
+ }
149
+ return { type: 'not_found', err };
150
+ }
151
+ });
152
+ switch (resolution.type) {
153
+ case 'not_found':
154
+ return resolution;
155
+ case 'found':
156
+ return resolution.result;
157
+ default:
158
+ throw (0, assert_never_1.default)(resolution);
159
+ }
160
+ }
161
+ owningPackage(fromFile) {
162
+ return shared_internals_2.PackageCache.shared('embroider-stage3', this.options.appRoot).ownerOfFile(fromFile);
163
+ }
164
+ originalPackage(fromFile) {
165
+ let originalFile = this.options.relocatedFiles[fromFile];
166
+ if (originalFile) {
167
+ let owning = shared_internals_2.PackageCache.shared('embroider-stage3', this.options.appRoot).ownerOfFile(originalFile);
168
+ if (owning && !owning.isV2Ember()) {
169
+ throw new Error(`bug: it should only be possible for a v2 ember package to own relocated files`);
170
+ }
171
+ return owning;
172
+ }
173
+ }
174
+ handleGlobalsCompat(request) {
175
+ let match = compatPattern.exec(request.specifier);
176
+ if (!match) {
177
+ return request;
178
+ }
179
+ let { type, rest } = match.groups;
180
+ let fromPkg = this.owningPackage(request.fromFile);
181
+ if (!(fromPkg === null || fromPkg === void 0 ? void 0 : fromPkg.isV2Ember())) {
182
+ return request;
183
+ }
184
+ let engine = this.owningEngine(fromPkg);
185
+ switch (type) {
186
+ case 'helpers':
187
+ return this.resolveHelper(rest, engine, request);
188
+ case 'components':
189
+ return this.resolveComponent(rest, engine, request);
190
+ case 'modifiers':
191
+ return this.resolveModifier(rest, engine, request);
192
+ case 'ambiguous':
193
+ return this.resolveHelperOrComponent(rest, engine, request);
194
+ default:
195
+ throw new Error(`bug: unexepected #embroider_compat specifier: ${request.specifier}`);
196
+ }
197
+ }
198
+ resolveHelper(path, inEngine, request) {
199
+ let target = this.parseGlobalPath(path, inEngine);
200
+ return request
201
+ .alias(`${target.packageName}/helpers/${target.memberName}`)
202
+ .rehome((0, path_1.resolve)(inEngine.root, 'package.json'));
203
+ }
204
+ resolveComponent(path, inEngine, request) {
205
+ let target = this.parseGlobalPath(path, inEngine);
206
+ let hbsModule = null;
207
+ let jsModule = null;
208
+ // first, the various places our template might be.
209
+ for (let candidate of this.componentTemplateCandidates(target.packageName)) {
210
+ let resolution = this.nodeResolve(`${target.packageName}${candidate.prefix}${target.memberName}${candidate.suffix}`, target.from);
211
+ if (resolution.type === 'real') {
212
+ hbsModule = resolution.filename;
213
+ break;
214
+ }
215
+ }
216
+ // then the various places our javascript might be.
217
+ for (let candidate of this.componentJSCandidates(target.packageName)) {
218
+ let resolution = this.nodeResolve(`${target.packageName}${candidate.prefix}${target.memberName}${candidate.suffix}`, target.from);
219
+ // .hbs is a resolvable extension for us, so we need to exclude it here.
220
+ // It matches as a priority lower than .js, so finding an .hbs means
221
+ // there's definitely not a .js.
222
+ if (resolution.type === 'real' && !resolution.filename.endsWith('.hbs')) {
223
+ jsModule = resolution.filename;
224
+ break;
225
+ }
226
+ }
227
+ if (hbsModule) {
228
+ return logTransition(`resolveComponent found legacy HBS`, request, request.virtualize((0, virtual_content_1.virtualPairComponent)(hbsModule, jsModule)));
229
+ }
230
+ else if (jsModule) {
231
+ return logTransition(`resolveComponent found only JS`, request, request.alias(jsModule).rehome(target.from));
232
+ }
233
+ else {
234
+ return logTransition(`resolveComponent failed`, request);
235
+ }
236
+ }
237
+ resolveHelperOrComponent(path, inEngine, request) {
238
+ // resolveHelper just rewrites our request to one that should target the
239
+ // component, so here to resolve the ambiguity we need to actually resolve
240
+ // that candidate to see if it works.
241
+ let helperCandidate = this.resolveHelper(path, inEngine, request);
242
+ let helperMatch = this.nodeResolve(helperCandidate.specifier, helperCandidate.fromFile);
243
+ if (helperMatch.type === 'real') {
244
+ return helperCandidate;
245
+ }
246
+ // unlike resolveHelper, resolveComponent already does pre-resolution in
247
+ // order to deal with its own internal ambiguity around JS vs HBS vs
248
+ // colocation.≥
249
+ let componentMatch = this.resolveComponent(path, inEngine, request);
250
+ if (componentMatch !== request) {
251
+ return componentMatch;
252
+ }
253
+ // this is the hard failure case -- we were supposed to find something and
254
+ // didn't. Let the normal resolution process progress so the user gets a
255
+ // normal build error.
256
+ return request;
257
+ }
258
+ resolveModifier(path, inEngine, request) {
259
+ let target = this.parseGlobalPath(path, inEngine);
260
+ return request
261
+ .alias(`${target.packageName}/modifiers/${target.memberName}`)
262
+ .rehome((0, path_1.resolve)(inEngine.root, 'package.json'));
263
+ }
264
+ *componentTemplateCandidates(inPackageName) {
265
+ yield { prefix: '/templates/components/', suffix: '' };
266
+ yield { prefix: '/components/', suffix: '/template' };
267
+ let pods = this.podPrefix(inPackageName);
268
+ if (pods) {
269
+ yield { prefix: `${pods}/components/`, suffix: '/template' };
270
+ }
271
+ }
272
+ *componentJSCandidates(inPackageName) {
273
+ yield { prefix: '/components/', suffix: '' };
274
+ yield { prefix: '/components/', suffix: '/component' };
275
+ let pods = this.podPrefix(inPackageName);
276
+ if (pods) {
277
+ yield { prefix: `${pods}/components/`, suffix: '/component' };
278
+ }
279
+ }
280
+ podPrefix(targetPackageName) {
281
+ if (targetPackageName === this.options.modulePrefix && this.options.podModulePrefix) {
282
+ if (!this.options.podModulePrefix.startsWith(this.options.modulePrefix)) {
283
+ throw new Error(`Your podModulePrefix (${this.options.podModulePrefix}) does not start with your app module prefix (${this.options.modulePrefix}). Not gonna support that silliness.`);
284
+ }
285
+ return this.options.podModulePrefix.slice(this.options.modulePrefix.length);
286
+ }
287
+ }
288
+ // for paths that come from non-strict templates
289
+ parseGlobalPath(path, inEngine) {
290
+ let parts = path.split('@');
291
+ if (parts.length > 1 && parts[0].length > 0) {
292
+ return { packageName: parts[0], memberName: parts[1], from: (0, path_1.resolve)(inEngine.root, 'pacakge.json') };
293
+ }
294
+ else {
295
+ return { packageName: inEngine.packageName, memberName: path, from: (0, path_1.resolve)(inEngine.root, 'pacakge.json') };
296
+ }
297
+ }
298
+ owningEngine(pkg) {
299
+ if (pkg.root === this.options.appRoot) {
300
+ // the app is always the first engine
301
+ return this.options.engines[0];
302
+ }
303
+ let owningEngine = this.options.engines.find(e => e.activeAddons.find(a => a.root === pkg.root));
304
+ if (!owningEngine) {
305
+ throw new Error(`bug in @embroider/core/src/module-resolver: cannot figure out the owning engine for ${pkg.root}`);
306
+ }
307
+ return owningEngine;
308
+ }
309
+ handleRenaming(request) {
310
+ if (request.isVirtual) {
311
+ return request;
312
+ }
313
+ let packageName = (0, shared_internals_1.packageName)(request.specifier);
314
+ if (!packageName) {
315
+ return request;
316
+ }
317
+ for (let [candidate, replacement] of Object.entries(this.options.renameModules)) {
318
+ if (candidate === request.specifier) {
319
+ return logTransition(`renameModules`, request, request.alias(replacement));
320
+ }
321
+ for (let extension of this.options.resolvableExtensions) {
322
+ if (candidate === request.specifier + '/index' + extension) {
323
+ return logTransition(`renameModules`, request, request.alias(replacement));
324
+ }
325
+ if (candidate === request.specifier + extension) {
326
+ return logTransition(`renameModules`, request, request.alias(replacement));
327
+ }
328
+ }
329
+ }
330
+ if (this.options.renamePackages[packageName]) {
331
+ return logTransition(`renamePackages`, request, request.alias(request.specifier.replace(packageName, this.options.renamePackages[packageName])));
332
+ }
333
+ let pkg = this.owningPackage(request.fromFile);
334
+ if (!pkg || !pkg.isV2Ember()) {
335
+ return request;
336
+ }
337
+ if (pkg.meta['auto-upgraded'] && pkg.name === packageName) {
338
+ // we found a self-import, resolve it for them. Only auto-upgraded
339
+ // packages get this help, v2 packages are natively supposed to make their
340
+ // own modules resolvable, and we want to push them all to do that
341
+ // correctly.
342
+ return logTransition(`v1 self-import`, request, this.resolveWithinPackage(request, pkg));
343
+ }
344
+ let originalPkg = this.originalPackage(request.fromFile);
345
+ if (originalPkg && pkg.meta['auto-upgraded'] && originalPkg.name === packageName) {
346
+ // A file that was relocated out of a package is importing that package's
347
+ // name, it should find its own original copy.
348
+ return logTransition(`self-import in app-js`, request, this.resolveWithinPackage(request, originalPkg));
349
+ }
350
+ return request;
351
+ }
352
+ resolveWithinPackage(request, pkg) {
353
+ if ('exports' in pkg.packageJSON) {
354
+ // this is the easy case -- a package that uses exports can safely resolve
355
+ // its own name, so it's enough to let it resolve the (self-targeting)
356
+ // specifier from its own package root.
357
+ return request.rehome((0, path_1.resolve)(pkg.root, 'package.json'));
358
+ }
359
+ else {
360
+ // otherwise we need to just assume that internal naming is simple
361
+ return request.alias(request.specifier.replace(pkg.name, '.')).rehome((0, path_1.resolve)(pkg.root, 'package.json'));
362
+ }
363
+ }
364
+ preHandleExternal(request) {
365
+ if (request.isVirtual) {
366
+ return request;
367
+ }
368
+ let { specifier, fromFile } = request;
369
+ let pkg = this.owningPackage(fromFile);
370
+ if (!pkg || !pkg.isV2Ember()) {
371
+ return request;
372
+ }
373
+ let packageName = (0, shared_internals_1.packageName)(specifier);
374
+ if (!packageName) {
375
+ // This is a relative import. We don't automatically externalize those
376
+ // because it's rare, and by keeping them static we give better errors. But
377
+ // we do allow them to be explicitly externalized by the package author (or
378
+ // a compat adapter). In the metadata, they would be listed in
379
+ // package-relative form, so we need to convert this specifier to that.
380
+ let absoluteSpecifier = (0, path_1.resolve)((0, path_1.dirname)(fromFile), specifier);
381
+ let packageRelativeSpecifier = (0, shared_internals_2.explicitRelative)(pkg.root, absoluteSpecifier);
382
+ if (isExplicitlyExternal(packageRelativeSpecifier, pkg)) {
383
+ let publicSpecifier = absoluteSpecifier.replace(pkg.root, pkg.name);
384
+ return external('beforeResolve', request, publicSpecifier);
385
+ }
386
+ else {
387
+ return request;
388
+ }
389
+ }
390
+ // absolute package imports can also be explicitly external based on their
391
+ // full specifier name
392
+ if (isExplicitlyExternal(specifier, pkg)) {
393
+ return external('beforeResolve', request, specifier);
394
+ }
395
+ if (!pkg.meta['auto-upgraded'] && shared_internals_1.emberVirtualPeerDeps.has(packageName)) {
396
+ // Native v2 addons are allowed to use the emberVirtualPeerDeps like
397
+ // `@glimmer/component`. And like all v2 addons, it's important that they
398
+ // see those dependencies after those dependencies have been converted to
399
+ // v2.
400
+ //
401
+ // But unlike auto-upgraded addons, native v2 addons are not necessarily
402
+ // copied out of their original place in node_modules. And from that
403
+ // original place they might accidentally resolve the emberVirtualPeerDeps
404
+ // that are present there in v1 format.
405
+ //
406
+ // So before we let normal resolving happen, we adjust these imports to
407
+ // point at the app's copies instead.
408
+ if (!this.options.activeAddons[packageName]) {
409
+ throw new Error(`${pkg.name} is trying to import the app's ${packageName} package, but it seems to be missing`);
410
+ }
411
+ let newHome = (0, path_1.resolve)(this.options.appRoot, 'package.json');
412
+ return logTransition(`emberVirtualPeerDeps in v2 addon`, request, request.rehome(newHome));
413
+ }
414
+ if (pkg.meta['auto-upgraded'] && !pkg.hasDependency('ember-auto-import')) {
415
+ try {
416
+ let dep = shared_internals_2.PackageCache.shared('embroider-stage3', this.options.appRoot).resolve(packageName, pkg);
417
+ if (!dep.isEmberPackage()) {
418
+ // classic ember addons can only import non-ember dependencies if they
419
+ // have ember-auto-import.
420
+ return external('v1 package without auto-import', request, specifier);
421
+ }
422
+ }
423
+ catch (err) {
424
+ if (err.code !== 'MODULE_NOT_FOUND') {
425
+ throw err;
426
+ }
427
+ }
428
+ }
429
+ // assertions on what native v2 addons can import
430
+ if (!pkg.meta['auto-upgraded']) {
431
+ let originalPkg = this.originalPackage(fromFile);
432
+ if (originalPkg) {
433
+ // this file has been moved into another package (presumably the app).
434
+ if (packageName !== pkg.name) {
435
+ // the only thing that native v2 addons are allowed to import from
436
+ // within the app tree is their own name.
437
+ 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`);
438
+ }
439
+ }
440
+ else {
441
+ // this file has not been moved. The normal case.
442
+ if (!pkg.meta['auto-upgraded'] && !reliablyResolvable(pkg, packageName)) {
443
+ throw new Error(`${pkg.name} is trying to import from ${packageName} but that is not one of its explicit dependencies`);
444
+ }
445
+ }
446
+ }
447
+ return request;
448
+ }
449
+ fallbackResolve(request) {
450
+ let { specifier, fromFile } = request;
451
+ if (compatPattern.test(specifier)) {
452
+ // Some kinds of compat requests get rewritten into other things
453
+ // deterministically. For example, "#embroider_compat/helpers/whatever"
454
+ // means only "the-current-engine/helpers/whatever", and if that doesn't
455
+ // actually exist it's that path that will show up as a missing import.
456
+ //
457
+ // But others have an ambiguous meaning. For example,
458
+ // #embroider_compat/components/whatever can mean several different
459
+ // things. If we're unable to find any of them, the actual
460
+ // "#embroider_compat" request will fall through all the way to here.
461
+ //
462
+ // In that case, we don't want to externalize that failure. We know it's
463
+ // not a classic runtime thing. It's better to let it be a build error
464
+ // here.
465
+ return request;
466
+ }
467
+ let pkg = this.owningPackage(fromFile);
468
+ if (!pkg || !pkg.isV2Ember()) {
469
+ return request;
470
+ }
471
+ let packageName = (0, shared_internals_1.packageName)(specifier);
472
+ if (!packageName) {
473
+ // this is a relative import, we have nothing more to for it.
474
+ return request;
475
+ }
476
+ let originalPkg = this.originalPackage(fromFile);
477
+ if (originalPkg) {
478
+ // we didn't find it from the original package, so try from the relocated
479
+ // package
480
+ return logTransition(`relocation fallback`, request, request.rehome((0, path_1.resolve)(originalPkg.root, 'package.json')));
481
+ }
482
+ // auto-upgraded packages can fall back to the set of known active addons
483
+ //
484
+ // v2 packages can fall back to the set of known active addons only to find
485
+ // themselves (which is needed due to app tree merging)
486
+ if ((pkg.meta['auto-upgraded'] || packageName === pkg.name) && this.options.activeAddons[packageName]) {
487
+ return logTransition(`activeAddons`, request, this.resolveWithinPackage(request, shared_internals_2.PackageCache.shared('embroider-stage3', this.options.appRoot).get(this.options.activeAddons[packageName])));
488
+ }
489
+ if (pkg.meta['auto-upgraded']) {
490
+ // auto-upgraded packages can fall back to attempting to find dependencies at
491
+ // runtime. Native v2 packages can only get this behavior in the
492
+ // isExplicitlyExternal case above because they need to explicitly ask for
493
+ // externals.
494
+ return external('v1 catch-all fallback', request, specifier);
495
+ }
496
+ else {
497
+ // native v2 packages don't automatically externalize *everything* the way
498
+ // auto-upgraded packages do, but they still externalize known and approved
499
+ // ember virtual packages (like @ember/component)
500
+ if (shared_internals_1.emberVirtualPackages.has(packageName)) {
501
+ return external('emberVirtualPackages', request, specifier);
502
+ }
503
+ }
504
+ // this is falling through with the original specifier which was
505
+ // non-resolvable, which will presumably cause a static build error in stage3.
506
+ return request;
507
+ }
508
+ // check whether the given file with the given owningPackage is an addon's
509
+ // appTree, and if so return the notional location within the app (or owning
510
+ // engine) that it "logically" lives at.
511
+ reverseSearchAppTree(owningPackage, fromFile) {
512
+ // if the requesting file is in an addon's app-js, the request should
513
+ // really be understood as a request for a module in the containing engine
514
+ if (owningPackage.isV2Addon()) {
515
+ let appJS = owningPackage.meta['app-js'];
516
+ if (appJS) {
517
+ let fromPackageRelativePath = (0, shared_internals_2.explicitRelative)(owningPackage.root, fromFile);
518
+ for (let [inAppName, inAddonName] of Object.entries(appJS)) {
519
+ if (inAddonName === fromPackageRelativePath) {
520
+ return { owningEngine: this.owningEngine(owningPackage), inAppName };
521
+ }
522
+ }
523
+ }
524
+ }
525
+ }
526
+ // check if this file is resolvable as a global component, and if so return
527
+ // its dasherized name
528
+ reverseComponentLookup(filename) {
529
+ const owningPackage = this.owningPackage(filename);
530
+ if (!(owningPackage === null || owningPackage === void 0 ? void 0 : owningPackage.isV2Ember())) {
531
+ return;
532
+ }
533
+ let engineConfig = this.options.engines.find(e => e.root === owningPackage.root);
534
+ if (engineConfig) {
535
+ // we're directly inside an engine, so we're potentially resolvable as a
536
+ // global component
537
+ // this kind of mapping is not true in general for all packages, but it
538
+ // *is* true for all classical engines (which includes apps) since they
539
+ // don't support package.json `exports`. As for a future v2 engine or app:
540
+ // this whole method is only relevant for implementing packageRules, which
541
+ // should only be for classic stuff. v2 packages should do the right
542
+ // things from the beginning and not need packageRules about themselves.
543
+ let inAppName = (0, shared_internals_2.explicitRelative)(engineConfig.root, filename);
544
+ return this.tryReverseComponent(engineConfig.packageName, inAppName);
545
+ }
546
+ let engineInfo = this.reverseSearchAppTree(owningPackage, filename);
547
+ if (engineInfo) {
548
+ // we're in some addon's app tree, so we're potentially resolvable as a
549
+ // global component
550
+ return this.tryReverseComponent(engineInfo.owningEngine.packageName, engineInfo.inAppName);
551
+ }
552
+ }
553
+ tryReverseComponent(inEngineName, relativePath) {
554
+ let extensionless = relativePath.replace((0, shared_internals_1.extensionsPattern)(this.options.resolvableExtensions), '');
555
+ let candidates = [...this.componentJSCandidates(inEngineName), ...this.componentTemplateCandidates(inEngineName)];
556
+ for (let candidate of candidates) {
557
+ if (extensionless.startsWith(`.${candidate.prefix}`) && extensionless.endsWith(candidate.suffix)) {
558
+ return extensionless.slice(candidate.prefix.length + 1, extensionless.length - candidate.suffix.length);
559
+ }
560
+ }
561
+ return undefined;
562
+ }
563
+ }
564
+ exports.Resolver = Resolver;
565
+ function isExplicitlyExternal(specifier, fromPkg) {
566
+ return Boolean(fromPkg.isV2Addon() && fromPkg.meta['externals'] && fromPkg.meta['externals'].includes(specifier));
567
+ }
568
+ // we don't want to allow things that resolve only by accident that are likely
569
+ // to break in other setups. For example: import your dependencies'
570
+ // dependencies, or importing your own name from within a monorepo (which will
571
+ // work because of the symlinking) without setting up "exports" (which makes
572
+ // your own name reliably resolvable)
573
+ function reliablyResolvable(pkg, packageName) {
574
+ if (pkg.hasDependency(packageName)) {
575
+ return true;
576
+ }
577
+ if (pkg.name === packageName && pkg.packageJSON.exports) {
578
+ return true;
579
+ }
580
+ if (shared_internals_1.emberVirtualPeerDeps.has(packageName) || shared_internals_1.emberVirtualPackages.has(packageName)) {
581
+ return true;
582
+ }
583
+ return false;
584
+ }
585
+ function external(label, request, specifier) {
586
+ let filename = (0, virtual_content_1.virtualExternalModule)(specifier);
587
+ return logTransition(label, request, request.virtualize(filename));
588
+ }
589
+ //# sourceMappingURL=module-resolver.js.map