@embroider/core 2.1.0 → 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.
- package/package.json +3 -4
- package/src/app-files.d.ts +1 -1
- package/src/app-files.js.map +1 -1
- package/src/app.d.ts +9 -7
- package/src/app.js +20 -31
- package/src/app.js.map +1 -1
- package/src/index.d.ts +3 -0
- package/src/index.js +5 -1
- package/src/index.js.map +1 -1
- package/src/module-resolver.d.ts +60 -22
- package/src/module-resolver.js +465 -136
- package/src/module-resolver.js.map +1 -1
- package/src/virtual-content.d.ts +3 -0
- package/src/virtual-content.js +87 -0
- package/src/virtual-content.js.map +1 -0
- package/src/wait-for-trees.js.map +1 -1
- package/src/babel-plugin-adjust-imports.d.ts +0 -31
- package/src/babel-plugin-adjust-imports.js +0 -224
- package/src/babel-plugin-adjust-imports.js.map +0 -1
- package/src/babel-plugin-inline-hbs-deps-node.d.ts +0 -14
- package/src/babel-plugin-inline-hbs-deps-node.js +0 -27
- package/src/babel-plugin-inline-hbs-deps-node.js.map +0 -1
- package/src/babel-plugin-inline-hbs-deps.d.ts +0 -10
- package/src/babel-plugin-inline-hbs-deps.js +0 -134
- package/src/babel-plugin-inline-hbs-deps.js.map +0 -1
- package/src/babel-plugin-inline-hbs-node.d.ts +0 -7
- package/src/babel-plugin-inline-hbs-node.js +0 -15
- package/src/babel-plugin-inline-hbs-node.js.map +0 -1
- package/src/babel-plugin-inline-hbs.d.ts +0 -24
- package/src/babel-plugin-inline-hbs.js +0 -142
- package/src/babel-plugin-inline-hbs.js.map +0 -1
- package/src/babel-plugin-stage1-inline-hbs-node.d.ts +0 -12
- package/src/babel-plugin-stage1-inline-hbs-node.js +0 -9
- package/src/babel-plugin-stage1-inline-hbs-node.js.map +0 -1
- package/src/babel-plugin-stage1-inline-hbs.d.ts +0 -9
- package/src/babel-plugin-stage1-inline-hbs.js +0 -96
- package/src/babel-plugin-stage1-inline-hbs.js.map +0 -1
- package/src/browser-index.d.ts +0 -1
- package/src/browser-index.js +0 -6
- package/src/browser-index.js.map +0 -1
- package/src/ember-template-compiler-types.d.ts +0 -41
- package/src/ember-template-compiler-types.js +0 -3
- package/src/ember-template-compiler-types.js.map +0 -1
- package/src/load-ember-template-compiler.d.ts +0 -6
- package/src/load-ember-template-compiler.js +0 -57
- package/src/load-ember-template-compiler.js.map +0 -1
- package/src/mini-modules-polyfill.d.ts +0 -12
- package/src/mini-modules-polyfill.js +0 -80
- package/src/mini-modules-polyfill.js.map +0 -1
- package/src/patch-template-compiler.d.ts +0 -1
- package/src/patch-template-compiler.js +0 -176
- package/src/patch-template-compiler.js.map +0 -1
- package/src/resolver-plugin.d.ts +0 -3
- package/src/resolver-plugin.js +0 -53
- package/src/resolver-plugin.js.map +0 -1
- package/src/resolver.d.ts +0 -14
- package/src/resolver.js +0 -3
- package/src/resolver.js.map +0 -1
- package/src/template-compiler-common.d.ts +0 -70
- package/src/template-compiler-common.js +0 -176
- package/src/template-compiler-common.js.map +0 -1
- package/src/template-compiler-node.d.ts +0 -17
- package/src/template-compiler-node.js +0 -32
- package/src/template-compiler-node.js.map +0 -1
package/src/module-resolver.js
CHANGED
|
@@ -1,96 +1,374 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var
|
|
3
|
-
|
|
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;
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
7
4
|
};
|
|
8
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
6
|
exports.Resolver = void 0;
|
|
10
7
|
const shared_internals_1 = require("@embroider/shared-internals");
|
|
11
8
|
const path_1 = require("path");
|
|
12
9
|
const shared_internals_2 = require("@embroider/shared-internals");
|
|
13
|
-
const
|
|
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
|
+
}
|
|
14
52
|
class Resolver {
|
|
15
|
-
constructor(
|
|
16
|
-
this.filename = filename;
|
|
53
|
+
constructor(options) {
|
|
17
54
|
this.options = options;
|
|
18
|
-
this.originalFilename = options.relocatedFiles[filename] || filename;
|
|
19
55
|
}
|
|
20
|
-
|
|
21
|
-
if (specifier === '@embroider/macros') {
|
|
56
|
+
beforeResolve(request) {
|
|
57
|
+
if (request.specifier === '@embroider/macros') {
|
|
22
58
|
// the macros package is always handled directly within babel (not
|
|
23
59
|
// necessarily as a real resolvable package), so we should not mess with it.
|
|
24
60
|
// It might not get compiled away until *after* our plugin has run, which is
|
|
25
61
|
// why we need to know about it.
|
|
26
|
-
return
|
|
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);
|
|
27
120
|
}
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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);
|
|
32
159
|
}
|
|
33
|
-
return resolution;
|
|
34
160
|
}
|
|
35
|
-
owningPackage() {
|
|
36
|
-
return shared_internals_2.PackageCache.shared('embroider-stage3', this.options.appRoot).ownerOfFile(
|
|
161
|
+
owningPackage(fromFile) {
|
|
162
|
+
return shared_internals_2.PackageCache.shared('embroider-stage3', this.options.appRoot).ownerOfFile(fromFile);
|
|
37
163
|
}
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
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);
|
|
41
168
|
if (owning && !owning.isV2Ember()) {
|
|
42
|
-
throw new Error(`bug: it should only be possible
|
|
169
|
+
throw new Error(`bug: it should only be possible for a v2 ember package to own relocated files`);
|
|
43
170
|
}
|
|
44
171
|
return owning;
|
|
45
172
|
}
|
|
46
173
|
}
|
|
47
|
-
|
|
48
|
-
let
|
|
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);
|
|
49
314
|
if (!packageName) {
|
|
50
|
-
return
|
|
315
|
+
return request;
|
|
51
316
|
}
|
|
52
317
|
for (let [candidate, replacement] of Object.entries(this.options.renameModules)) {
|
|
53
|
-
if (candidate === specifier) {
|
|
54
|
-
return replacement;
|
|
318
|
+
if (candidate === request.specifier) {
|
|
319
|
+
return logTransition(`renameModules`, request, request.alias(replacement));
|
|
55
320
|
}
|
|
56
321
|
for (let extension of this.options.resolvableExtensions) {
|
|
57
|
-
if (candidate === specifier + '/index' + extension) {
|
|
58
|
-
return replacement;
|
|
322
|
+
if (candidate === request.specifier + '/index' + extension) {
|
|
323
|
+
return logTransition(`renameModules`, request, request.alias(replacement));
|
|
59
324
|
}
|
|
60
|
-
if (candidate === specifier + extension) {
|
|
61
|
-
return replacement;
|
|
325
|
+
if (candidate === request.specifier + extension) {
|
|
326
|
+
return logTransition(`renameModules`, request, request.alias(replacement));
|
|
62
327
|
}
|
|
63
328
|
}
|
|
64
329
|
}
|
|
65
330
|
if (this.options.renamePackages[packageName]) {
|
|
66
|
-
return specifier.replace(packageName, this.options.renamePackages[packageName]);
|
|
331
|
+
return logTransition(`renamePackages`, request, request.alias(request.specifier.replace(packageName, this.options.renamePackages[packageName])));
|
|
67
332
|
}
|
|
68
|
-
let pkg = this.owningPackage();
|
|
333
|
+
let pkg = this.owningPackage(request.fromFile);
|
|
69
334
|
if (!pkg || !pkg.isV2Ember()) {
|
|
70
|
-
return
|
|
335
|
+
return request;
|
|
71
336
|
}
|
|
72
337
|
if (pkg.meta['auto-upgraded'] && pkg.name === packageName) {
|
|
73
|
-
// we found a self-import,
|
|
74
|
-
// this help, v2 packages are natively supposed to
|
|
75
|
-
//
|
|
76
|
-
|
|
77
|
-
return (
|
|
78
|
-
}
|
|
79
|
-
let
|
|
80
|
-
if (
|
|
81
|
-
//
|
|
82
|
-
//
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
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);
|
|
92
370
|
if (!pkg || !pkg.isV2Ember()) {
|
|
93
|
-
return
|
|
371
|
+
return request;
|
|
94
372
|
}
|
|
95
373
|
let packageName = (0, shared_internals_1.packageName)(specifier);
|
|
96
374
|
if (!packageName) {
|
|
@@ -99,20 +377,20 @@ class Resolver {
|
|
|
99
377
|
// we do allow them to be explicitly externalized by the package author (or
|
|
100
378
|
// a compat adapter). In the metadata, they would be listed in
|
|
101
379
|
// package-relative form, so we need to convert this specifier to that.
|
|
102
|
-
let absoluteSpecifier = (0, path_1.resolve)((0, path_1.dirname)(
|
|
380
|
+
let absoluteSpecifier = (0, path_1.resolve)((0, path_1.dirname)(fromFile), specifier);
|
|
103
381
|
let packageRelativeSpecifier = (0, shared_internals_2.explicitRelative)(pkg.root, absoluteSpecifier);
|
|
104
382
|
if (isExplicitlyExternal(packageRelativeSpecifier, pkg)) {
|
|
105
383
|
let publicSpecifier = absoluteSpecifier.replace(pkg.root, pkg.name);
|
|
106
|
-
return
|
|
384
|
+
return external('beforeResolve', request, publicSpecifier);
|
|
107
385
|
}
|
|
108
386
|
else {
|
|
109
|
-
return
|
|
387
|
+
return request;
|
|
110
388
|
}
|
|
111
389
|
}
|
|
112
390
|
// absolute package imports can also be explicitly external based on their
|
|
113
391
|
// full specifier name
|
|
114
392
|
if (isExplicitlyExternal(specifier, pkg)) {
|
|
115
|
-
return
|
|
393
|
+
return external('beforeResolve', request, specifier);
|
|
116
394
|
}
|
|
117
395
|
if (!pkg.meta['auto-upgraded'] && shared_internals_1.emberVirtualPeerDeps.has(packageName)) {
|
|
118
396
|
// Native v2 addons are allowed to use the emberVirtualPeerDeps like
|
|
@@ -125,120 +403,167 @@ class Resolver {
|
|
|
125
403
|
// original place they might accidentally resolve the emberVirtualPeerDeps
|
|
126
404
|
// that are present there in v1 format.
|
|
127
405
|
//
|
|
128
|
-
// So before we
|
|
129
|
-
// the app's copies instead.
|
|
130
|
-
if (
|
|
131
|
-
|
|
132
|
-
throw new Error(`${pkg.name} is trying to import the app's ${packageName} package, but it seems to be missing`);
|
|
133
|
-
}
|
|
134
|
-
return {
|
|
135
|
-
result: 'redirect-to',
|
|
136
|
-
specifier: (0, shared_internals_2.explicitRelative)((0, path_1.dirname)(this.filename), specifier.replace(packageName, this.options.activeAddons[packageName])),
|
|
137
|
-
};
|
|
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`);
|
|
138
410
|
}
|
|
411
|
+
let newHome = (0, path_1.resolve)(this.options.appRoot, 'package.json');
|
|
412
|
+
return logTransition(`emberVirtualPeerDeps in v2 addon`, request, request.rehome(newHome));
|
|
139
413
|
}
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
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`);
|
|
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);
|
|
148
421
|
}
|
|
149
|
-
return { result: 'continue' };
|
|
150
422
|
}
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
if (targetPkg) {
|
|
155
|
-
// self-imports are legal in the app tree, even for v2 packages.
|
|
156
|
-
if (!pkg.meta['auto-upgraded'] && packageName !== pkg.name) {
|
|
157
|
-
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`);
|
|
158
|
-
}
|
|
159
|
-
// we found it, but we need to rewrite it because it's not really going to
|
|
160
|
-
// resolve from where its sitting
|
|
161
|
-
return {
|
|
162
|
-
result: 'redirect-to',
|
|
163
|
-
specifier: (0, shared_internals_2.explicitRelative)((0, path_1.dirname)(this.filename), specifier.replace(packageName, targetPkg.root)),
|
|
164
|
-
};
|
|
423
|
+
catch (err) {
|
|
424
|
+
if (err.code !== 'MODULE_NOT_FOUND') {
|
|
425
|
+
throw err;
|
|
165
426
|
}
|
|
166
427
|
}
|
|
167
428
|
}
|
|
168
|
-
|
|
169
|
-
|
|
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.
|
|
170
442
|
if (!pkg.meta['auto-upgraded'] && !reliablyResolvable(pkg, packageName)) {
|
|
171
443
|
throw new Error(`${pkg.name} is trying to import from ${packageName} but that is not one of its explicit dependencies`);
|
|
172
444
|
}
|
|
173
|
-
return { result: 'continue' };
|
|
174
445
|
}
|
|
175
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
|
+
}
|
|
176
482
|
// auto-upgraded packages can fall back to the set of known active addons
|
|
177
483
|
//
|
|
178
484
|
// v2 packages can fall back to the set of known active addons only to find
|
|
179
485
|
// themselves (which is needed due to app tree merging)
|
|
180
486
|
if ((pkg.meta['auto-upgraded'] || packageName === pkg.name) && this.options.activeAddons[packageName]) {
|
|
181
|
-
return
|
|
182
|
-
result: 'redirect-to',
|
|
183
|
-
specifier: (0, shared_internals_2.explicitRelative)((0, path_1.dirname)(this.filename), specifier.replace(packageName, this.options.activeAddons[packageName])),
|
|
184
|
-
};
|
|
487
|
+
return logTransition(`activeAddons`, request, this.resolveWithinPackage(request, shared_internals_2.PackageCache.shared('embroider-stage3', this.options.appRoot).get(this.options.activeAddons[packageName])));
|
|
185
488
|
}
|
|
186
489
|
if (pkg.meta['auto-upgraded']) {
|
|
187
490
|
// auto-upgraded packages can fall back to attempting to find dependencies at
|
|
188
491
|
// runtime. Native v2 packages can only get this behavior in the
|
|
189
492
|
// isExplicitlyExternal case above because they need to explicitly ask for
|
|
190
493
|
// externals.
|
|
191
|
-
return
|
|
494
|
+
return external('v1 catch-all fallback', request, specifier);
|
|
192
495
|
}
|
|
193
496
|
else {
|
|
194
497
|
// native v2 packages don't automatically externalize *everything* the way
|
|
195
498
|
// auto-upgraded packages do, but they still externalize known and approved
|
|
196
499
|
// ember virtual packages (like @ember/component)
|
|
197
500
|
if (shared_internals_1.emberVirtualPackages.has(packageName)) {
|
|
198
|
-
return
|
|
501
|
+
return external('emberVirtualPackages', request, specifier);
|
|
199
502
|
}
|
|
200
503
|
}
|
|
201
|
-
// non-resolvable imports in dynamic positions become runtime errors, not
|
|
202
|
-
// build-time errors, so we emit the runtime error module here before the
|
|
203
|
-
// stage3 packager has a chance to see the missing module. (Maybe some stage3
|
|
204
|
-
// packagers will have this behavior by default, because it would make sense,
|
|
205
|
-
// but webpack at least does not.)
|
|
206
|
-
if (isDynamic) {
|
|
207
|
-
return { result: 'runtime-failure', specifier };
|
|
208
|
-
}
|
|
209
504
|
// this is falling through with the original specifier which was
|
|
210
505
|
// non-resolvable, which will presumably cause a static build error in stage3.
|
|
211
|
-
return
|
|
506
|
+
return request;
|
|
212
507
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
|
|
227
|
-
|
|
228
|
-
|
|
229
|
-
// have ember-auto-import.
|
|
230
|
-
//
|
|
231
|
-
// whereas native v2 packages can always import any dependency
|
|
232
|
-
return false;
|
|
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
|
+
}
|
|
233
524
|
}
|
|
234
|
-
return dep;
|
|
235
525
|
}
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
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);
|
|
239
551
|
}
|
|
240
|
-
return false;
|
|
241
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));
|
|
242
567
|
}
|
|
243
568
|
// we don't want to allow things that resolve only by accident that are likely
|
|
244
569
|
// to break in other setups. For example: import your dependencies'
|
|
@@ -252,9 +577,13 @@ function reliablyResolvable(pkg, packageName) {
|
|
|
252
577
|
if (pkg.name === packageName && pkg.packageJSON.exports) {
|
|
253
578
|
return true;
|
|
254
579
|
}
|
|
255
|
-
if (shared_internals_1.emberVirtualPeerDeps.has(packageName)) {
|
|
580
|
+
if (shared_internals_1.emberVirtualPeerDeps.has(packageName) || shared_internals_1.emberVirtualPackages.has(packageName)) {
|
|
256
581
|
return true;
|
|
257
582
|
}
|
|
258
583
|
return false;
|
|
259
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
|
+
}
|
|
260
589
|
//# sourceMappingURL=module-resolver.js.map
|