Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix v1 app self import #2010

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions packages/core/src/module-resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1007,15 +1007,21 @@ export class Resolver {
// supported fancy package.json exports features so this direct mapping
// to the root is always right.

// "my-package/foo" -> "./foo"
// "my-package" -> "./" (this can't be just "." because node's require.resolve doesn't reliable support that)
let selfImportPath = request.specifier === pkg.name ? './' : request.specifier.replace(pkg.name, '.');
// "my-app/foo" -> "./foo" from app's package.json
// "my-addon/foo" -> "my-addon/foo" from a package that's guaranteed to be able to resolve my-addon

return logTransition(
`v1 self-import`,
request,
request.alias(selfImportPath).rehome(resolve(pkg.root, 'package.json'))
);
let owningEngine = this.owningEngine(pkg);
let addonConfig = owningEngine.activeAddons.find(a => a.root === pkg.root);
if (addonConfig) {
return logTransition(`v1 addon self-import`, request, request.rehome(addonConfig.canResolveFromFile));
} else {
let selfImportPath = request.specifier === pkg.name ? './' : request.specifier.replace(pkg.name, '.');
return logTransition(
`v1 app self-import`,
request,
request.alias(selfImportPath).rehome(resolve(pkg.root, 'package.json'))
);
}
} else {
// v2 packages are supposed to use package.json `exports` to enable
// self-imports, but not all build tools actually follow the spec. This
Expand Down Expand Up @@ -1223,7 +1229,7 @@ export class Resolver {
}
if (!entry) {
throw new Error(
`A module tried to resolve "${request.specifier}" and didn't find it (${label}).
`[${request.debugType}] A module tried to resolve "${request.specifier}" and didn't find it (${label}).

- Maybe a dependency declaration is missing?
- Remember that v1 addons can only import non-Ember-addon NPM dependencies if they include ember-auto-import in their dependencies.
Expand Down
2 changes: 1 addition & 1 deletion packages/vite/src/esbuild-request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export class EsBuildModuleRequest implements ModuleRequest {
return;
}

if (source && importer && source[0] !== '\0') {
if (source && importer && source[0] !== '\0' && !source.startsWith('virtual-module:')) {
let fromFile = cleanUrl(importer);
return new EsBuildModuleRequest(
context,
Expand Down
Loading