From 81ff92e1b3e66ab04698d8719ec88b512653c1d3 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Mon, 7 Oct 2024 12:55:36 +0200 Subject: [PATCH 1/2] Add failing test for absolutely aliased dependency --- test-scenarios/static-import-test.ts | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/test-scenarios/static-import-test.ts b/test-scenarios/static-import-test.ts index 0c7fe83c..3af634fc 100644 --- a/test-scenarios/static-import-test.ts +++ b/test-scenarios/static-import-test.ts @@ -32,11 +32,13 @@ function staticImportTest(project: Project) { merge(project.files, { 'ember-cli-build.js': ` const EmberApp = require('ember-cli/lib/broccoli/ember-app'); + const { dirname, join } = require('path'); module.exports = function (defaults) { let app = new EmberApp(defaults, { autoImport: { alias: { - 'my-aliased-package': 'original-package' + 'my-aliased-package': 'original-package', + 'dual-build-package': join(dirname(require.resolve('dual-build-package', { paths: [process.cwd()] })), 'dist/index.mjs'), }, watchDependencies: [ 'original-package' @@ -235,6 +237,16 @@ function staticImportTest(project: Project) { }); }); `, + 'absolute-alias-test.js': ` + import { module, test } from 'qunit'; + import value from 'dual-build-package'; + + module('Unit | absolute alias', function () { + test('can import package with absolute alias', function (assert) { + assert.equal(value, 'This is ESM'); + }); + }); + `, 'allow-app-imports-test.js': ` import { module, test } from 'qunit'; import example1 from '@ef4/app-template/lib/example1'; @@ -359,6 +371,17 @@ function staticImportTest(project: Project) { }, }); + project.addDevDependency('dual-build-package', { + files: { + 'index.js': `throw new Error('This should not get imported!)`, + dist: { + 'index.mjs': ` + const value = 'This is ESM'; + export default value;`, + }, + }, + }); + project.addDevDependency('@ef4/scoped-lib', { files: { 'index.js': ` From ac316b30e0cd4d59f81c531991697ccb6ed61a1a Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Mon, 7 Oct 2024 13:33:19 +0200 Subject: [PATCH 2/2] Handle alias with absolute path --- packages/ember-auto-import/ts/package.ts | 24 +++++++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/packages/ember-auto-import/ts/package.ts b/packages/ember-auto-import/ts/package.ts index e5c55b54..57596bb5 100644 --- a/packages/ember-auto-import/ts/package.ts +++ b/packages/ember-auto-import/ts/package.ts @@ -326,9 +326,27 @@ export default class Package { // relative or absolute path, rather than a package name. If the // originally authored import was an absolute or relative path, it would // have hit our { type: 'local' } condition before we ran aliasFor. - // - // At the moment, we don't try to handle this case, but we could in the - // future. + + packageName = getPackageName(importedPath); + + if (packageName) { + let packageRoot: string | undefined; + let packagePath = resolvePackagePath(packageName, this.root); + + if (packagePath) { + packageRoot = dirname(packagePath); + } + + if (packageRoot) { + return { + type: 'package', + path, + packageName, + packageRoot, + }; + } + } + return { type: 'local', local: path,