@embroider/core 2.0.1 → 2.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (53) hide show
  1. package/package.json +1 -1
  2. package/src/babel-plugin-adjust-imports.d.ts +3 -34
  3. package/src/babel-plugin-adjust-imports.js +94 -318
  4. package/src/babel-plugin-adjust-imports.js.map +1 -1
  5. package/src/babel-plugin-inline-hbs-deps-node.d.ts +14 -0
  6. package/src/babel-plugin-inline-hbs-deps-node.js +27 -0
  7. package/src/babel-plugin-inline-hbs-deps-node.js.map +1 -0
  8. package/src/babel-plugin-inline-hbs-deps.d.ts +10 -0
  9. package/src/babel-plugin-inline-hbs-deps.js +134 -0
  10. package/src/babel-plugin-inline-hbs-deps.js.map +1 -0
  11. package/src/babel-plugin-inline-hbs-node.d.ts +7 -0
  12. package/src/babel-plugin-inline-hbs-node.js +15 -0
  13. package/src/babel-plugin-inline-hbs-node.js.map +1 -0
  14. package/src/babel-plugin-inline-hbs.d.ts +24 -0
  15. package/src/babel-plugin-inline-hbs.js +142 -0
  16. package/src/babel-plugin-inline-hbs.js.map +1 -0
  17. package/src/babel-plugin-stage1-inline-hbs-node.d.ts +12 -0
  18. package/src/babel-plugin-stage1-inline-hbs-node.js +9 -0
  19. package/src/babel-plugin-stage1-inline-hbs-node.js.map +1 -0
  20. package/src/babel-plugin-stage1-inline-hbs.d.ts +9 -0
  21. package/src/babel-plugin-stage1-inline-hbs.js +96 -0
  22. package/src/babel-plugin-stage1-inline-hbs.js.map +1 -0
  23. package/src/browser-index.d.ts +1 -0
  24. package/src/browser-index.js +6 -0
  25. package/src/browser-index.js.map +1 -0
  26. package/src/ember-template-compiler-types.d.ts +41 -0
  27. package/src/ember-template-compiler-types.js +3 -0
  28. package/src/ember-template-compiler-types.js.map +1 -0
  29. package/src/load-ember-template-compiler.d.ts +6 -0
  30. package/src/load-ember-template-compiler.js +57 -0
  31. package/src/load-ember-template-compiler.js.map +1 -0
  32. package/src/messages.d.ts +0 -1
  33. package/src/mini-modules-polyfill.d.ts +12 -0
  34. package/src/mini-modules-polyfill.js +80 -0
  35. package/src/mini-modules-polyfill.js.map +1 -0
  36. package/src/module-resolver.d.ts +46 -0
  37. package/src/module-resolver.js +260 -0
  38. package/src/module-resolver.js.map +1 -0
  39. package/src/patch-template-compiler.d.ts +1 -0
  40. package/src/patch-template-compiler.js +176 -0
  41. package/src/patch-template-compiler.js.map +1 -0
  42. package/src/resolver-plugin.d.ts +3 -0
  43. package/src/resolver-plugin.js +53 -0
  44. package/src/resolver-plugin.js.map +1 -0
  45. package/src/resolver.d.ts +14 -0
  46. package/src/resolver.js +3 -0
  47. package/src/resolver.js.map +1 -0
  48. package/src/template-compiler-common.d.ts +70 -0
  49. package/src/template-compiler-common.js +176 -0
  50. package/src/template-compiler-common.js.map +1 -0
  51. package/src/template-compiler-node.d.ts +17 -0
  52. package/src/template-compiler-node.js +32 -0
  53. package/src/template-compiler-node.js.map +1 -0
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@embroider/core",
3
- "version": "2.0.1",
3
+ "version": "2.1.0",
4
4
  "private": false,
5
5
  "description": "A build system for EmberJS applications.",
6
6
  "repository": {
@@ -1,38 +1,17 @@
1
1
  import type { NodePath } from '@babel/traverse';
2
2
  import type * as Babel from '@babel/core';
3
3
  import type { types as t } from '@babel/core';
4
- import { Package, V2Package } from '@embroider/shared-internals';
4
+ import { Options, Resolver } from './module-resolver';
5
5
  interface State {
6
- adjustFile: AdjustFile;
6
+ resolver: Resolver;
7
7
  opts: Options | DeflatedOptions;
8
8
  }
9
+ export { Options };
9
10
  export interface DeflatedOptions {
10
11
  adjustImportsOptionsPath: string;
11
12
  relocatedFilesPath: string;
12
13
  }
13
14
  declare type BabelTypes = typeof t;
14
- export interface Options {
15
- renamePackages: {
16
- [fromName: string]: string;
17
- };
18
- renameModules: {
19
- [fromName: string]: string;
20
- };
21
- extraImports: {
22
- absPath: string;
23
- target: string;
24
- runtimeName?: string;
25
- }[];
26
- externalsDir: string;
27
- activeAddons: {
28
- [packageName: string]: string;
29
- };
30
- relocatedFiles: {
31
- [relativePath: string]: string;
32
- };
33
- resolvableExtensions: string[];
34
- appRoot: string;
35
- }
36
15
  declare type DefineExpressionPath = NodePath<t.CallExpression> & {
37
16
  node: t.CallExpression & {
38
17
  arguments: [t.StringLiteral, t.ArrayExpression, Function];
@@ -50,13 +29,3 @@ export default function main(babel: typeof Babel): {
50
29
  CallExpression(path: NodePath<Babel.types.CallExpression>, state: State): void;
51
30
  };
52
31
  };
53
- declare class AdjustFile {
54
- name: string;
55
- readonly originalFile: string;
56
- private packageCache;
57
- constructor(name: string, relocatedFiles: Options['relocatedFiles'], appRoot: string);
58
- get isRelocated(): boolean;
59
- owningPackage(): Package | undefined;
60
- relocatedIntoPackage(): V2Package | undefined;
61
- }
62
- export {};
@@ -1,20 +1,17 @@
1
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;
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.isDefineExpression = exports.isDynamicImportExpression = exports.isImportSyncExpression = void 0;
10
- const shared_internals_1 = require("@embroider/shared-internals");
11
7
  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
8
  const babel_import_util_1 = require("babel-import-util");
16
9
  const crypto_1 = require("crypto");
17
10
  const fs_extra_1 = require("fs-extra");
11
+ const shared_internals_1 = require("@embroider/shared-internals");
12
+ const js_handlebars_1 = require("./js-handlebars");
13
+ const module_resolver_1 = require("./module-resolver");
14
+ const assert_never_1 = __importDefault(require("assert-never"));
18
15
  function isImportSyncExpression(t, path) {
19
16
  if (!path ||
20
17
  !path.isCallExpression() ||
@@ -48,254 +45,6 @@ function isDefineExpression(t, path) {
48
45
  t.isFunction(args[2]));
49
46
  }
50
47
  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
48
  function main(babel) {
300
49
  let t = babel.types;
301
50
  return {
@@ -303,7 +52,7 @@ function main(babel) {
303
52
  Program: {
304
53
  enter(path, state) {
305
54
  let opts = ensureOpts(state);
306
- state.adjustFile = new AdjustFile(path.hub.file.opts.filename, opts.relocatedFiles, opts.appRoot);
55
+ state.resolver = new module_resolver_1.Resolver(path.hub.file.opts.filename, opts);
307
56
  let adder = new babel_import_util_1.ImportUtil(t, path);
308
57
  addExtraImports(adder, t, path, opts.extraImports);
309
58
  },
@@ -318,24 +67,23 @@ function main(babel) {
318
67
  CallExpression(path, state) {
319
68
  if (isImportSyncExpression(t, path) || isDynamicImportExpression(t, path)) {
320
69
  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));
70
+ resolve(source.node.value, true, state, newSpecifier => {
71
+ source.replaceWith(t.stringLiteral(newSpecifier));
72
+ });
324
73
  return;
325
74
  }
326
75
  // Should/can we make this early exit when the first define was found?
327
76
  if (!isDefineExpression(t, path)) {
328
77
  return;
329
78
  }
330
- let pkg = state.adjustFile.owningPackage();
79
+ let pkg = state.resolver.owningPackage();
331
80
  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.`);
81
+ throw new Error(`The file ${state.resolver.originalFilename} 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
82
  }
334
- let opts = ensureOpts(state);
335
83
  const dependencies = path.node.arguments[1];
336
84
  const specifiers = dependencies.elements.slice();
337
85
  specifiers.push(path.node.arguments[0]);
338
- for (let source of specifiers) {
86
+ for (const source of specifiers) {
339
87
  if (!source) {
340
88
  continue;
341
89
  }
@@ -346,10 +94,9 @@ function main(babel) {
346
94
  // skip "special" AMD dependencies
347
95
  continue;
348
96
  }
349
- let specifier = adjustSpecifier(source.value, state.adjustFile, opts, false);
350
- if (specifier !== source.value) {
351
- source.value = specifier;
352
- }
97
+ resolve(source.value, false, state, newSpecifier => {
98
+ source.value = newSpecifier;
99
+ });
353
100
  }
354
101
  },
355
102
  },
@@ -357,15 +104,13 @@ function main(babel) {
357
104
  }
358
105
  exports.default = main;
359
106
  function rewriteTopLevelImport(path, state) {
360
- let opts = ensureOpts(state);
361
107
  const { source } = path.node;
362
108
  if (source === null || source === undefined) {
363
109
  return;
364
110
  }
365
- let specifier = adjustSpecifier(source.value, state.adjustFile, opts, false);
366
- if (specifier !== source.value) {
367
- source.value = specifier;
368
- }
111
+ resolve(source.value, false, state, newSpecifier => {
112
+ source.value = newSpecifier;
113
+ });
369
114
  }
370
115
  main.baseDir = function () {
371
116
  return (0, path_1.join)(__dirname, '..');
@@ -389,37 +134,6 @@ function amdDefine(t, adder, path, target, runtimeName) {
389
134
  t.functionExpression(null, [], t.blockStatement([t.returnStatement(value)])),
390
135
  ]));
391
136
  }
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
137
  function ensureOpts(state) {
424
138
  let { opts } = state;
425
139
  if ('adjustImportsOptionsPath' in opts) {
@@ -428,21 +142,83 @@ function ensureOpts(state) {
428
142
  }
429
143
  return opts;
430
144
  }
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;
145
+ function makeExternal(specifier, sourceFile, opts) {
146
+ let target = (0, path_1.join)(opts.externalsDir, specifier + '.js');
147
+ atomicWrite(target, externalTemplate({
148
+ runtimeName: specifier,
149
+ }));
150
+ return (0, shared_internals_1.explicitRelative)((0, path_1.dirname)(sourceFile), target.slice(0, -3));
151
+ }
152
+ function atomicWrite(path, content) {
153
+ if ((0, fs_extra_1.pathExistsSync)(path)) {
154
+ return;
155
+ }
156
+ let suffix = (0, crypto_1.randomBytes)(8).toString('hex');
157
+ (0, fs_extra_1.outputFileSync)(path + suffix, content);
158
+ try {
159
+ (0, fs_extra_1.renameSync)(path + suffix, path);
439
160
  }
440
- if (pkg.name === packageName && pkg.packageJSON.exports) {
441
- return true;
161
+ catch (err) {
162
+ // windows throws EPERM for concurrent access. For us it's not an error
163
+ // condition because the other thread is writing the exact same value we
164
+ // would have.
165
+ if (err.code !== 'EPERM') {
166
+ throw err;
167
+ }
442
168
  }
443
- if (shared_internals_1.emberVirtualPeerDeps.has(packageName)) {
444
- return true;
169
+ }
170
+ function makeMissingModule(specifier, sourceFile, opts) {
171
+ let target = (0, path_1.join)(opts.externalsDir, 'missing', specifier + '.js');
172
+ atomicWrite(target, dynamicMissingModule({
173
+ moduleName: specifier,
174
+ }));
175
+ return (0, shared_internals_1.explicitRelative)((0, path_1.dirname)(sourceFile), target.slice(0, -3));
176
+ }
177
+ const dynamicMissingModule = (0, js_handlebars_1.compile)(`
178
+ throw new Error('Could not find module \`{{{js-string-escape moduleName}}}\`');
179
+ `);
180
+ const externalTemplate = (0, js_handlebars_1.compile)(`
181
+ {{#if (eq runtimeName "require")}}
182
+ const m = window.requirejs;
183
+ {{else}}
184
+ const m = window.require("{{{js-string-escape runtimeName}}}");
185
+ {{/if}}
186
+ {{!-
187
+ There are plenty of hand-written AMD defines floating around
188
+ that lack this, and they will break when other build systems
189
+ encounter them.
190
+
191
+ As far as I can tell, Ember's loader was already treating this
192
+ case as a module, so in theory we aren't breaking anything by
193
+ marking it as such when other packagers come looking.
194
+
195
+ todo: get review on this part.
196
+ -}}
197
+ if (m.default && !m.__esModule) {
198
+ m.__esModule = true;
199
+ }
200
+ module.exports = m;
201
+ `);
202
+ function resolve(specifier, isDynamic, state, setter) {
203
+ let resolution = state.resolver.resolve(specifier, isDynamic);
204
+ let newSpecifier;
205
+ switch (resolution.result) {
206
+ case 'continue':
207
+ return;
208
+ case 'external':
209
+ newSpecifier = makeExternal(resolution.specifier, state.resolver.filename, ensureOpts(state));
210
+ break;
211
+ case 'redirect-to':
212
+ newSpecifier = resolution.specifier;
213
+ break;
214
+ case 'runtime-failure':
215
+ newSpecifier = makeMissingModule(resolution.specifier, state.resolver.filename, ensureOpts(state));
216
+ break;
217
+ default:
218
+ throw (0, assert_never_1.default)(resolution);
219
+ }
220
+ if (newSpecifier) {
221
+ setter(newSpecifier);
445
222
  }
446
- return false;
447
223
  }
448
224
  //# sourceMappingURL=babel-plugin-adjust-imports.js.map