Skip to content

Commit

Permalink
Merge pull request #1504 from embroider-build/fix-distance-relative-i…
Browse files Browse the repository at this point in the history
…mports

fix inter-package relative imports in addon's app-js
  • Loading branch information
ef4 authored Jul 1, 2023
2 parents db25d62 + be73a0f commit 47fc75d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -802,6 +802,15 @@ export class Resolver {
// a compat adapter). In the metadata, they would be listed in
// package-relative form, so we need to convert this specifier to that.
let absoluteSpecifier = resolve(dirname(fromFile), specifier);

if (!absoluteSpecifier.startsWith(pkg.root)) {
// this relative path escape its package. So it's not really using
// normal inter-package resolving and we should leave it alone. This
// case comes up especially when babel transforms are trying to insert
// references to runtime utilities, like we do in @embroider/macros.
return logTransition('beforeResolve: relative path escapes its package', request);
}

let packageRelativeSpecifier = explicitRelative(pkg.root, absoluteSpecifier);
if (isExplicitlyExternal(packageRelativeSpecifier, pkg)) {
let publicSpecifier = absoluteSpecifier.replace(pkg.root, pkg.name);
Expand Down
19 changes: 19 additions & 0 deletions tests/scenarios/core-resolver-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -479,6 +479,25 @@ Scenarios.fromProject(() => new Project())
.to('./node_modules/my-addon/_app_/hello-world.js');
});

test('app-js module in addon can still do relative imports that escape its package', async function () {
givenFiles({
'node_modules/extra.js': '',
'node_modules/my-addon/_app_/hello-world.js': 'import "../../extra.js"',
'app.js': `import "my-app/hello-world"`,
});

await configure({
addonMeta: {
'app-js': { './hello-world.js': './_app_/hello-world.js' },
},
});

expectAudit
.module('./node_modules/my-addon/_app_/hello-world.js')
.resolves('../../extra.js')
.to('./node_modules/extra.js');
});

test('hbs in addon is found', async function () {
givenFiles({
'node_modules/my-addon/_app_/templates/hello-world.hbs': '',
Expand Down

0 comments on commit 47fc75d

Please sign in to comment.