From a0a8021185dd879e5ef23822e29e26d3f82095e1 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Wed, 1 May 2024 12:24:57 +0200 Subject: [PATCH 1/8] Add commonAncestorDirectories util --- package-lock.json | 8 ++++- packages/ember-auto-import/package.json | 1 + packages/ember-auto-import/ts/tests/util.ts | 38 +++++++++++++++++++++ packages/ember-auto-import/ts/util.ts | 23 +++++++++++++ 4 files changed, 69 insertions(+), 1 deletion(-) create mode 100644 packages/ember-auto-import/ts/tests/util.ts diff --git a/package-lock.json b/package-lock.json index f0601c10..8719698b 100644 --- a/package-lock.json +++ b/package-lock.json @@ -22906,6 +22906,8 @@ }, "node_modules/is-windows": { "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz", + "integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==", "license": "MIT", "engines": { "node": ">=0.10.0" @@ -35285,6 +35287,7 @@ "fs-extra": "^10.0.0", "fs-tree-diff": "^2.0.0", "handlebars": "^4.3.1", + "is-subdir": "^1.2.0", "js-string-escape": "^1.0.1", "lodash": "^4.17.19", "mini-css-extract-plugin": "^2.5.2", @@ -49769,6 +49772,7 @@ "fs-extra": "^10.0.0", "fs-tree-diff": "^2.0.0", "handlebars": "^4.3.1", + "is-subdir": "^1.2.0", "js-string-escape": "^1.0.1", "lodash": "^4.17.19", "mini-css-extract-plugin": "^2.5.2", @@ -58143,7 +58147,9 @@ } }, "is-windows": { - "version": "1.0.2" + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/is-windows/-/is-windows-1.0.2.tgz", + "integrity": "sha512-eXK1UInq2bPmjyX6e3VHIzMLobc4J94i4AWn+Hpq3OU5KkrRC96OAcR3PRJ/pGu6m8TRnBHP9dkXQVsT/COVIA==" }, "is-wsl": { "version": "2.2.0", diff --git a/packages/ember-auto-import/package.json b/packages/ember-auto-import/package.json index 0155775a..b7c221f0 100644 --- a/packages/ember-auto-import/package.json +++ b/packages/ember-auto-import/package.json @@ -56,6 +56,7 @@ "fs-extra": "^10.0.0", "fs-tree-diff": "^2.0.0", "handlebars": "^4.3.1", + "is-subdir": "^1.2.0", "js-string-escape": "^1.0.1", "lodash": "^4.17.19", "mini-css-extract-plugin": "^2.5.2", diff --git a/packages/ember-auto-import/ts/tests/util.ts b/packages/ember-auto-import/ts/tests/util.ts new file mode 100644 index 00000000..03338e01 --- /dev/null +++ b/packages/ember-auto-import/ts/tests/util.ts @@ -0,0 +1,38 @@ +import QUnit from 'qunit'; +import 'qunit-assertions-extra'; +import { commonAncestorDirectories } from '../util'; + +const { module: Qmodule, test } = QUnit; + +Qmodule('commonAncestorDirectories', function () { + test('returns same dirs if no nested', function (assert) { + const result = commonAncestorDirectories([ + '/a/b/c/index.js', + '/d/index.js', + ]); + + assert.deepEqual(result, ['/a/b/c', '/d']); + }); + + test('returns common dirs', function (assert) { + const result = commonAncestorDirectories([ + '/a/b/c/index.js', + '/a/b/index.js', + '/d/index.js', + '/d/e/f/index.js', + ]); + + assert.deepEqual(result, ['/a/b', '/d']); + }); + + test('ignores duplicates', function (assert) { + const result = commonAncestorDirectories([ + '/a/b/c/index.js', + '/a/b/index.js', + '/a/b/c/index.js', + '/a/b/index.js', + ]); + + assert.deepEqual(result, ['/a/b']); + }); +}); diff --git a/packages/ember-auto-import/ts/util.ts b/packages/ember-auto-import/ts/util.ts index 6859aca2..c33f73b1 100644 --- a/packages/ember-auto-import/ts/util.ts +++ b/packages/ember-auto-import/ts/util.ts @@ -1,3 +1,6 @@ +import isSubdir from 'is-subdir'; +import { dirname } from 'path'; + export function shallowEqual(a: any[], b: any[]) { return ( a && @@ -10,3 +13,23 @@ export function shallowEqual(a: any[], b: any[]) { export function stripQuery(path: string) { return path.split('?')[0]; } + +export function commonAncestorDirectories(dirs: string[]): string[] { + return dirs.reduce((results, fileOrDir) => { + let dir = dirname(fileOrDir); + + if (results.length === 0) { + return [dir]; + } + + let newResults = results.filter( + (existingDir) => !isSubdir(dir, existingDir) + ); + + if (!newResults.some((existingDir) => isSubdir(existingDir, dir))) { + newResults.push(dir); + } + + return newResults; + }, [] as string[]); +} From aa4dbb84ee7b319d1337bf215fe4f8c8e84edfad Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Wed, 1 May 2024 15:15:20 +0200 Subject: [PATCH 2/8] Add getImportableModules util --- package-lock.json | 9 + package.json | 1 + .../ember-auto-import/ts/tests/util-test.ts | 231 ++++++++++++++++++ packages/ember-auto-import/ts/tests/util.ts | 38 --- packages/ember-auto-import/ts/util.ts | 21 ++ 5 files changed, 262 insertions(+), 38 deletions(-) create mode 100644 packages/ember-auto-import/ts/tests/util-test.ts delete mode 100644 packages/ember-auto-import/ts/tests/util.ts diff --git a/package-lock.json b/package-lock.json index 8719698b..7ca1edbc 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,6 +11,7 @@ "types/*" ], "dependencies": { + "pkg-entry-points": "^1.0.2", "resolve-package-path": "^3.1.0", "semver": "^7.3.5" }, @@ -28584,6 +28585,14 @@ "node": ">=8" } }, + "node_modules/pkg-entry-points": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/pkg-entry-points/-/pkg-entry-points-1.0.2.tgz", + "integrity": "sha512-LZKo+SJNMC80gk+vANlN+A5C7LIAGuyQ4aD6Ro3+ZDAd2Av+g8RRlMX4AXcutIH9Vdw27YpNk9GmanH9dch6Gg==", + "funding": { + "url": "https://github.com/privatenumber/pkg-entry-points?sponsor=1" + } + }, "node_modules/pkg-up": { "version": "3.1.0", "dev": true, diff --git a/package.json b/package.json index 82481164..6ee62cc3 100644 --- a/package.json +++ b/package.json @@ -13,6 +13,7 @@ "watch": "cd packages/ember-auto-import && npm run compile -- --watch" }, "dependencies": { + "pkg-entry-points": "^1.0.2", "resolve-package-path": "^3.1.0", "semver": "^7.3.5" }, diff --git a/packages/ember-auto-import/ts/tests/util-test.ts b/packages/ember-auto-import/ts/tests/util-test.ts new file mode 100644 index 00000000..d4aabbd6 --- /dev/null +++ b/packages/ember-auto-import/ts/tests/util-test.ts @@ -0,0 +1,231 @@ +import QUnit from 'qunit'; +import 'qunit-assertions-extra'; +import { commonAncestorDirectories, getImportableModules } from '../util'; +import { Project } from 'scenario-tester'; + +const { module: Qmodule, test } = QUnit; + +async function generateProject(packageJson = {}) { + const project = new Project('my-package', { + files: { + 'package.json': JSON.stringify(packageJson), + src: { + 'index.js': 'export default 123', + 'module.js': 'export default 123', + nested: { + 'module.js': 'export default 123', + }, + }, + dist: { + 'index.js': 'export default 123', + 'module.js': 'export default 123', + nested: { + 'module.js': 'export default 123', + }, + }, + declarations: { + 'index.d.ts': 'export default 123', + 'module.d.ts': 'export default 123', + nested: { + 'module.d.ts': 'export default 123', + }, + }, + lib: { + 'module.js': 'export default 123', + }, + }, + }); + + await project.write(); + + return project; +} + +Qmodule('commonAncestorDirectories', function () { + test('returns same dirs if no nested', function (assert) { + const result = commonAncestorDirectories([ + '/a/b/c/index.js', + '/d/index.js', + ]); + + assert.deepEqual(result, ['/a/b/c', '/d']); + }); + + test('returns common dirs', function (assert) { + const result = commonAncestorDirectories([ + '/a/b/c/index.js', + '/a/b/index.js', + '/d/index.js', + '/d/e/f/index.js', + ]); + + assert.deepEqual(result, ['/a/b', '/d']); + }); + + test('ignores duplicates', function (assert) { + const result = commonAncestorDirectories([ + '/a/b/c/index.js', + '/a/b/index.js', + '/a/b/c/index.js', + '/a/b/index.js', + ]); + + assert.deepEqual(result, ['/a/b']); + }); +}); + +Qmodule('importableModules', function (hooks) { + let project: Project; + + hooks.afterEach(function (this: any) { + project?.dispose(); + }); + + test('returns only modules declared in exports', async function (assert) { + project = await generateProject({ + exports: './dist/index.js', + }); + + const result = await getImportableModules(project.baseDir); + + assert.deepEqual(result, ['./dist/index.js']); + }); + + test('ignores types condition', async function (assert) { + project = await generateProject({ + exports: { + '.': { + types: './declarations/index.d.ts', + default: './dist/index.js', + }, + }, + }); + + const result = await getImportableModules(project.baseDir); + + assert.deepEqual(result, ['./dist/index.js']); + }); + + test('ignores node condition', async function (assert) { + project = await generateProject({ + exports: { + '.': { + types: './declarations/index.d.ts', + default: './dist/index.js', + }, + 'lib/module': { + node: './lib/module.js', + }, + }, + }); + + const result = await getImportableModules(project.baseDir); + + assert.deepEqual(result, ['./dist/index.js']); + }); + + test('supports import condition', async function (assert) { + project = await generateProject({ + exports: { + '.': { + types: './declarations/index.d.ts', + import: './dist/index.js', + }, + }, + }); + + const result = await getImportableModules(project.baseDir); + + assert.deepEqual(result, ['./dist/index.js']); + }); + + test('supports nested conditions', async function (assert) { + project = await generateProject({ + exports: { + '.': { + import: { + types: './declarations/index.d.ts', + default: './dist/index.js', + }, + }, + }, + }); + + const result = await getImportableModules(project.baseDir); + + assert.deepEqual(result, ['./dist/index.js']); + }); + + test('supports subpaths', async function (assert) { + project = await generateProject({ + exports: { + '.': { + types: './declarations/index.d.ts', + default: './dist/index.js', + }, + module: { + types: './declarations/module.d.ts', + default: './dist/module.js', + }, + 'nested/module': { + types: './declarations/nested/module.d.ts', + default: './dist/nested/module.js', + }, + }, + }); + + const result = await getImportableModules(project.baseDir); + + assert.deepEqual(result, [ + './dist/index.js', + './dist/module.js', + './dist/nested/module.js', + ]); + }); + + test('supports globstar patterns', async function (assert) { + project = await generateProject({ + exports: { + '.': { + types: './declarations/index.d.ts', + default: './dist/index.js', + }, + './*': { + types: './declarations/*.d.ts', + default: './dist/*.js', + }, + }, + }); + + const result = await getImportableModules(project.baseDir); + + assert.deepEqual(result, [ + './dist/index.js', + './dist/module.js', + './dist/nested/module.js', + ]); + }); + + test('returns all possible imports when having only main export', async function (assert) { + project = await generateProject({ + main: './dist/index.js', + }); + + const result = await getImportableModules(project.baseDir); + + assert.deepEqual(result, [ + './declarations/index.d.ts', + './declarations/module.d.ts', + './declarations/nested/module.d.ts', + './dist/index.js', + './dist/module.js', + './dist/nested/module.js', + './index.js', + './lib/module.js', + './package.json', + './src/index.js', + './src/module.js', + './src/nested/module.js', + ]); + }); +}); diff --git a/packages/ember-auto-import/ts/tests/util.ts b/packages/ember-auto-import/ts/tests/util.ts deleted file mode 100644 index 03338e01..00000000 --- a/packages/ember-auto-import/ts/tests/util.ts +++ /dev/null @@ -1,38 +0,0 @@ -import QUnit from 'qunit'; -import 'qunit-assertions-extra'; -import { commonAncestorDirectories } from '../util'; - -const { module: Qmodule, test } = QUnit; - -Qmodule('commonAncestorDirectories', function () { - test('returns same dirs if no nested', function (assert) { - const result = commonAncestorDirectories([ - '/a/b/c/index.js', - '/d/index.js', - ]); - - assert.deepEqual(result, ['/a/b/c', '/d']); - }); - - test('returns common dirs', function (assert) { - const result = commonAncestorDirectories([ - '/a/b/c/index.js', - '/a/b/index.js', - '/d/index.js', - '/d/e/f/index.js', - ]); - - assert.deepEqual(result, ['/a/b', '/d']); - }); - - test('ignores duplicates', function (assert) { - const result = commonAncestorDirectories([ - '/a/b/c/index.js', - '/a/b/index.js', - '/a/b/c/index.js', - '/a/b/index.js', - ]); - - assert.deepEqual(result, ['/a/b']); - }); -}); diff --git a/packages/ember-auto-import/ts/util.ts b/packages/ember-auto-import/ts/util.ts index c33f73b1..3587c67d 100644 --- a/packages/ember-auto-import/ts/util.ts +++ b/packages/ember-auto-import/ts/util.ts @@ -1,5 +1,7 @@ import isSubdir from 'is-subdir'; import { dirname } from 'path'; +// @ts-expect-error types don't resolve +import { getPackageEntryPoints } from 'pkg-entry-points'; export function shallowEqual(a: any[], b: any[]) { return ( @@ -33,3 +35,22 @@ export function commonAncestorDirectories(dirs: string[]): string[] { return newResults; }, [] as string[]); } + +export async function getImportableModules( + packagePath: string +): Promise { + const entryPoints = await getPackageEntryPoints(packagePath); + + return Object.values(entryPoints) + .map( + (alternatives) => + alternatives.find( + ([conditions]) => + (conditions.includes('import') || conditions.includes('default')) && + !conditions.includes('types') && + !conditions.includes('node') + )?.[1] + ) + .filter((item): item is string => !!item) + .filter((item, index, array) => array.indexOf(item) === index); +} From 5c05672f3c1a1c4ceab4903b43d06f5702f114f0 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Wed, 1 May 2024 16:20:00 +0200 Subject: [PATCH 3/8] Add getWatchedDirectories util --- .../ember-auto-import/ts/tests/util-test.ts | 42 ++++++++++++++++++- packages/ember-auto-import/ts/util.ts | 22 +++++++++- 2 files changed, 60 insertions(+), 4 deletions(-) diff --git a/packages/ember-auto-import/ts/tests/util-test.ts b/packages/ember-auto-import/ts/tests/util-test.ts index d4aabbd6..e622ccf4 100644 --- a/packages/ember-auto-import/ts/tests/util-test.ts +++ b/packages/ember-auto-import/ts/tests/util-test.ts @@ -1,11 +1,15 @@ import QUnit from 'qunit'; import 'qunit-assertions-extra'; -import { commonAncestorDirectories, getImportableModules } from '../util'; +import { + commonAncestorDirectories, + getImportableModules, + getWatchedDirectories, +} from '../util'; import { Project } from 'scenario-tester'; const { module: Qmodule, test } = QUnit; -async function generateProject(packageJson = {}) { +async function generateProject(packageJson = {}, additionalFiles = {}) { const project = new Project('my-package', { files: { 'package.json': JSON.stringify(packageJson), @@ -33,6 +37,7 @@ async function generateProject(packageJson = {}) { lib: { 'module.js': 'export default 123', }, + ...additionalFiles, }, }); @@ -229,3 +234,36 @@ Qmodule('importableModules', function (hooks) { ]); }); }); + +Qmodule('getWatchedDirectories', function (hooks) { + let project: Project; + + hooks.afterEach(function (this: any) { + project?.dispose(); + }); + + test('returns only dist for typical v2 addon', async function (assert) { + project = await generateProject( + { + exports: { + '.': { + types: './declarations/index.d.ts', + default: './dist/index.js', + }, + './*': { + types: './declarations/*.d.ts', + default: './dist/*.js', + }, + './addon-main.js': './addon-main.cjs', + }, + }, + { + 'addon-main.cjs': 'module.exports = {}', + } + ); + + const result = await getWatchedDirectories(project.baseDir); + + assert.deepEqual(result, ['./dist']); + }); +}); diff --git a/packages/ember-auto-import/ts/util.ts b/packages/ember-auto-import/ts/util.ts index 3587c67d..39a02843 100644 --- a/packages/ember-auto-import/ts/util.ts +++ b/packages/ember-auto-import/ts/util.ts @@ -1,8 +1,14 @@ import isSubdir from 'is-subdir'; import { dirname } from 'path'; -// @ts-expect-error types don't resolve +// @ts-expect-error types don't resolve as this package exposes them only via package.json exports, which our old TS version does not support import { getPackageEntryPoints } from 'pkg-entry-points'; +// copied from pkg-entry-points, as we cannot use their types, see comment above +type ConditionToPath = [conditions: string[], internalPath: string]; +type PackageEntryPoints = { + [subpath: string]: ConditionToPath[]; +}; + export function shallowEqual(a: any[], b: any[]) { return ( a && @@ -39,7 +45,9 @@ export function commonAncestorDirectories(dirs: string[]): string[] { export async function getImportableModules( packagePath: string ): Promise { - const entryPoints = await getPackageEntryPoints(packagePath); + const entryPoints: PackageEntryPoints = await getPackageEntryPoints( + packagePath + ); return Object.values(entryPoints) .map( @@ -48,9 +56,19 @@ export async function getImportableModules( ([conditions]) => (conditions.includes('import') || conditions.includes('default')) && !conditions.includes('types') && + !conditions.includes('require') && !conditions.includes('node') )?.[1] ) .filter((item): item is string => !!item) .filter((item, index, array) => array.indexOf(item) === index); } + +export async function getWatchedDirectories( + packagePath: string +): Promise { + const modules = (await getImportableModules(packagePath)).filter( + (module) => module !== './addon-main.cjs' + ); + return commonAncestorDirectories(modules); +} From 4d52e2ee3d265f2970a6dbb48eacd3ed3d5b4809 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Wed, 1 May 2024 16:26:12 +0200 Subject: [PATCH 4/8] Move watch utils into separate module --- .../{util-test.ts => watch-utils-test.ts} | 2 +- packages/ember-auto-import/ts/util.ts | 62 ------------------- packages/ember-auto-import/ts/watch-utils.ts | 61 ++++++++++++++++++ 3 files changed, 62 insertions(+), 63 deletions(-) rename packages/ember-auto-import/ts/tests/{util-test.ts => watch-utils-test.ts} (99%) create mode 100644 packages/ember-auto-import/ts/watch-utils.ts diff --git a/packages/ember-auto-import/ts/tests/util-test.ts b/packages/ember-auto-import/ts/tests/watch-utils-test.ts similarity index 99% rename from packages/ember-auto-import/ts/tests/util-test.ts rename to packages/ember-auto-import/ts/tests/watch-utils-test.ts index e622ccf4..962ab994 100644 --- a/packages/ember-auto-import/ts/tests/util-test.ts +++ b/packages/ember-auto-import/ts/tests/watch-utils-test.ts @@ -4,7 +4,7 @@ import { commonAncestorDirectories, getImportableModules, getWatchedDirectories, -} from '../util'; +} from '../watch-utils'; import { Project } from 'scenario-tester'; const { module: Qmodule, test } = QUnit; diff --git a/packages/ember-auto-import/ts/util.ts b/packages/ember-auto-import/ts/util.ts index 39a02843..6859aca2 100644 --- a/packages/ember-auto-import/ts/util.ts +++ b/packages/ember-auto-import/ts/util.ts @@ -1,14 +1,3 @@ -import isSubdir from 'is-subdir'; -import { dirname } from 'path'; -// @ts-expect-error types don't resolve as this package exposes them only via package.json exports, which our old TS version does not support -import { getPackageEntryPoints } from 'pkg-entry-points'; - -// copied from pkg-entry-points, as we cannot use their types, see comment above -type ConditionToPath = [conditions: string[], internalPath: string]; -type PackageEntryPoints = { - [subpath: string]: ConditionToPath[]; -}; - export function shallowEqual(a: any[], b: any[]) { return ( a && @@ -21,54 +10,3 @@ export function shallowEqual(a: any[], b: any[]) { export function stripQuery(path: string) { return path.split('?')[0]; } - -export function commonAncestorDirectories(dirs: string[]): string[] { - return dirs.reduce((results, fileOrDir) => { - let dir = dirname(fileOrDir); - - if (results.length === 0) { - return [dir]; - } - - let newResults = results.filter( - (existingDir) => !isSubdir(dir, existingDir) - ); - - if (!newResults.some((existingDir) => isSubdir(existingDir, dir))) { - newResults.push(dir); - } - - return newResults; - }, [] as string[]); -} - -export async function getImportableModules( - packagePath: string -): Promise { - const entryPoints: PackageEntryPoints = await getPackageEntryPoints( - packagePath - ); - - return Object.values(entryPoints) - .map( - (alternatives) => - alternatives.find( - ([conditions]) => - (conditions.includes('import') || conditions.includes('default')) && - !conditions.includes('types') && - !conditions.includes('require') && - !conditions.includes('node') - )?.[1] - ) - .filter((item): item is string => !!item) - .filter((item, index, array) => array.indexOf(item) === index); -} - -export async function getWatchedDirectories( - packagePath: string -): Promise { - const modules = (await getImportableModules(packagePath)).filter( - (module) => module !== './addon-main.cjs' - ); - return commonAncestorDirectories(modules); -} diff --git a/packages/ember-auto-import/ts/watch-utils.ts b/packages/ember-auto-import/ts/watch-utils.ts new file mode 100644 index 00000000..12e7ea90 --- /dev/null +++ b/packages/ember-auto-import/ts/watch-utils.ts @@ -0,0 +1,61 @@ +import isSubdir from 'is-subdir'; +import { dirname } from 'path'; +// @ts-expect-error types don't resolve as this package exposes them only via package.json exports, which our old TS version does not support +import { getPackageEntryPoints } from 'pkg-entry-points'; + +// copied from pkg-entry-points, as we cannot use their types, see comment above +type ConditionToPath = [conditions: string[], internalPath: string]; +type PackageEntryPoints = { + [subpath: string]: ConditionToPath[]; +}; + +export function commonAncestorDirectories(dirs: string[]): string[] { + return dirs.reduce((results, fileOrDir) => { + let dir = dirname(fileOrDir); + + if (results.length === 0) { + return [dir]; + } + + let newResults = results.filter( + (existingDir) => !isSubdir(dir, existingDir) + ); + + if (!newResults.some((existingDir) => isSubdir(existingDir, dir))) { + newResults.push(dir); + } + + return newResults; + }, [] as string[]); +} + +export async function getImportableModules( + packagePath: string +): Promise { + const entryPoints: PackageEntryPoints = await getPackageEntryPoints( + packagePath + ); + + return Object.values(entryPoints) + .map( + (alternatives) => + alternatives.find( + ([conditions]) => + (conditions.includes('import') || conditions.includes('default')) && + !conditions.includes('types') && + !conditions.includes('require') && + !conditions.includes('node') + )?.[1] + ) + .filter((item): item is string => !!item) + .filter((item, index, array) => array.indexOf(item) === index); +} + +export async function getWatchedDirectories( + packagePath: string +): Promise { + const modules = (await getImportableModules(packagePath)).filter( + (module) => module !== './addon-main.cjs' + ); + return commonAncestorDirectories(modules); +} From e0815c900241e17c210f1717b9a36a1bc10b9ace Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Wed, 1 May 2024 18:04:11 +0200 Subject: [PATCH 5/8] Add code comments --- packages/ember-auto-import/ts/watch-utils.ts | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/packages/ember-auto-import/ts/watch-utils.ts b/packages/ember-auto-import/ts/watch-utils.ts index 12e7ea90..a49bfe27 100644 --- a/packages/ember-auto-import/ts/watch-utils.ts +++ b/packages/ember-auto-import/ts/watch-utils.ts @@ -9,6 +9,9 @@ type PackageEntryPoints = { [subpath: string]: ConditionToPath[]; }; +/** + * Given a list of files, it will return the smallest set of directories that contain all these files + */ export function commonAncestorDirectories(dirs: string[]): string[] { return dirs.reduce((results, fileOrDir) => { let dir = dirname(fileOrDir); @@ -29,6 +32,10 @@ export function commonAncestorDirectories(dirs: string[]): string[] { }, [] as string[]); } +/** + * Given a path to a package, it will return all its internal(!) module files that are importable, + * taking into account explicit package.json exports, filtered down to only include importable runtime code + */ export async function getImportableModules( packagePath: string ): Promise { @@ -51,11 +58,17 @@ export async function getImportableModules( .filter((item, index, array) => array.indexOf(item) === index); } +/** + * Given a package path, it will return the list smallest set of directories that contain importable code. + * This can be used to constrain the set of directories used for file watching, to not include the whole package directory. + */ export async function getWatchedDirectories( packagePath: string ): Promise { - const modules = (await getImportableModules(packagePath)).filter( - (module) => module !== './addon-main.cjs' + const modules = (await getImportableModules(packagePath)).filter((module) => + // this is a workaround for excluding the addon-main.cjs module commonly used in v2 addons, which is _not_ importable in runtime code, + // but the generic logic based on (conditional) exports does not exclude that out of the box. + module.match(/\/addon-main.c?js$/) ); return commonAncestorDirectories(modules); } From 104e753bc024c4617ceed24880fe00b259137426 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Wed, 1 May 2024 19:12:42 +0200 Subject: [PATCH 6/8] Convert to sync API Patched pkg-entry-points for now, awaiting upstream PR... --- .../ts/tests/watch-utils-test.ts | 18 ++++---- packages/ember-auto-import/ts/watch-utils.ts | 24 +++++------ patches/pkg-entry-points+1.0.2.patch | 42 +++++++++++++++++++ 3 files changed, 61 insertions(+), 23 deletions(-) create mode 100644 patches/pkg-entry-points+1.0.2.patch diff --git a/packages/ember-auto-import/ts/tests/watch-utils-test.ts b/packages/ember-auto-import/ts/tests/watch-utils-test.ts index 962ab994..52d87add 100644 --- a/packages/ember-auto-import/ts/tests/watch-utils-test.ts +++ b/packages/ember-auto-import/ts/tests/watch-utils-test.ts @@ -91,7 +91,7 @@ Qmodule('importableModules', function (hooks) { exports: './dist/index.js', }); - const result = await getImportableModules(project.baseDir); + const result = getImportableModules(project.baseDir); assert.deepEqual(result, ['./dist/index.js']); }); @@ -106,7 +106,7 @@ Qmodule('importableModules', function (hooks) { }, }); - const result = await getImportableModules(project.baseDir); + const result = getImportableModules(project.baseDir); assert.deepEqual(result, ['./dist/index.js']); }); @@ -124,7 +124,7 @@ Qmodule('importableModules', function (hooks) { }, }); - const result = await getImportableModules(project.baseDir); + const result = getImportableModules(project.baseDir); assert.deepEqual(result, ['./dist/index.js']); }); @@ -139,7 +139,7 @@ Qmodule('importableModules', function (hooks) { }, }); - const result = await getImportableModules(project.baseDir); + const result = getImportableModules(project.baseDir); assert.deepEqual(result, ['./dist/index.js']); }); @@ -156,7 +156,7 @@ Qmodule('importableModules', function (hooks) { }, }); - const result = await getImportableModules(project.baseDir); + const result = getImportableModules(project.baseDir); assert.deepEqual(result, ['./dist/index.js']); }); @@ -179,7 +179,7 @@ Qmodule('importableModules', function (hooks) { }, }); - const result = await getImportableModules(project.baseDir); + const result = getImportableModules(project.baseDir); assert.deepEqual(result, [ './dist/index.js', @@ -202,7 +202,7 @@ Qmodule('importableModules', function (hooks) { }, }); - const result = await getImportableModules(project.baseDir); + const result = getImportableModules(project.baseDir); assert.deepEqual(result, [ './dist/index.js', @@ -216,7 +216,7 @@ Qmodule('importableModules', function (hooks) { main: './dist/index.js', }); - const result = await getImportableModules(project.baseDir); + const result = getImportableModules(project.baseDir); assert.deepEqual(result, [ './declarations/index.d.ts', @@ -262,7 +262,7 @@ Qmodule('getWatchedDirectories', function (hooks) { } ); - const result = await getWatchedDirectories(project.baseDir); + const result = getWatchedDirectories(project.baseDir); assert.deepEqual(result, ['./dist']); }); diff --git a/packages/ember-auto-import/ts/watch-utils.ts b/packages/ember-auto-import/ts/watch-utils.ts index a49bfe27..d0a40da5 100644 --- a/packages/ember-auto-import/ts/watch-utils.ts +++ b/packages/ember-auto-import/ts/watch-utils.ts @@ -1,7 +1,7 @@ import isSubdir from 'is-subdir'; import { dirname } from 'path'; // @ts-expect-error types don't resolve as this package exposes them only via package.json exports, which our old TS version does not support -import { getPackageEntryPoints } from 'pkg-entry-points'; +import { getPackageEntryPointsSync } from 'pkg-entry-points'; // copied from pkg-entry-points, as we cannot use their types, see comment above type ConditionToPath = [conditions: string[], internalPath: string]; @@ -36,12 +36,9 @@ export function commonAncestorDirectories(dirs: string[]): string[] { * Given a path to a package, it will return all its internal(!) module files that are importable, * taking into account explicit package.json exports, filtered down to only include importable runtime code */ -export async function getImportableModules( - packagePath: string -): Promise { - const entryPoints: PackageEntryPoints = await getPackageEntryPoints( - packagePath - ); +export function getImportableModules(packagePath: string): string[] { + const entryPoints: PackageEntryPoints = + getPackageEntryPointsSync(packagePath); return Object.values(entryPoints) .map( @@ -62,13 +59,12 @@ export async function getImportableModules( * Given a package path, it will return the list smallest set of directories that contain importable code. * This can be used to constrain the set of directories used for file watching, to not include the whole package directory. */ -export async function getWatchedDirectories( - packagePath: string -): Promise { - const modules = (await getImportableModules(packagePath)).filter((module) => - // this is a workaround for excluding the addon-main.cjs module commonly used in v2 addons, which is _not_ importable in runtime code, - // but the generic logic based on (conditional) exports does not exclude that out of the box. - module.match(/\/addon-main.c?js$/) +export function getWatchedDirectories(packagePath: string): string[] { + const modules = getImportableModules(packagePath).filter( + (module) => + // this is a workaround for excluding the addon-main.cjs module commonly used in v2 addons, which is _not_ importable in runtime code, + // but the generic logic based on (conditional) exports does not exclude that out of the box. + !module.match(/\/addon-main.c?js$/) ); return commonAncestorDirectories(modules); } diff --git a/patches/pkg-entry-points+1.0.2.patch b/patches/pkg-entry-points+1.0.2.patch new file mode 100644 index 00000000..270333a5 --- /dev/null +++ b/patches/pkg-entry-points+1.0.2.patch @@ -0,0 +1,42 @@ +diff --git a/node_modules/pkg-entry-points/.DS_Store b/node_modules/pkg-entry-points/.DS_Store +new file mode 100644 +index 0000000..c88a062 +Binary files /dev/null and b/node_modules/pkg-entry-points/.DS_Store differ +diff --git a/node_modules/pkg-entry-points/dist/index.cjs b/node_modules/pkg-entry-points/dist/index.cjs +index ff7dada..4da9967 100755 +--- a/node_modules/pkg-entry-points/dist/index.cjs ++++ b/node_modules/pkg-entry-points/dist/index.cjs +@@ -1 +1 @@ +-"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _=require("fs"),A=require("path");function O(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var E=O(_),h=O(A);const m=async(e,t,i)=>{const c=await e.readdir(t);return(await Promise.all(c.map(async a=>{const l=h.default.join(t,a);if((await e.stat(l)).isDirectory()){const o=await m(e,l,!0);return i?o:o.map(r=>`./${h.default.relative(t,r)}`)}return i?l:`./${h.default.relative(t,l)}`}))).flat()},y="*",w=e=>{const t=e.split(y),i=t[0],c=t.length-1,n=t[c],a=t.slice(1,c);return{prefix:i,middle:a,suffix:n}},v=({prefix:e,suffix:t,middle:i},c)=>{if(!c.startsWith(e)||!c.endsWith(t))return;const n=c.slice(e.length,-t.length||void 0);if(i.length===0)return n;let a=0,l="";for(const s of i){const o=n.indexOf(s,a);if(o===-1)return;const r=n.slice(a,o);if(!l)l=r;else if(l!==r)return;a=o+s.length}return l},j=(e,t,i=[],c={})=>{if(t===null||typeof t=="string"){i.length===0&&i.push("default");const n=JSON.stringify(i);if(!Object.hasOwn(c,n))if(t===null)c[n]=t;else if(t.includes(y)){const a=w(t);c[n]=e.map(l=>{const s=v(a,l);return s&&[l,s]}).filter(l=>l!==void 0)}else e.includes(t)&&(c[n]=[t])}else if(Array.isArray(t))for(const n of t)j(e,n,i,c);else if(typeof t=="object"&&t)for(const n in t){if(!Object.hasOwn(t,n))continue;const a=i.slice();a.includes(n)||a.push(n),j(e,t[n],a.sort(),c)}return c},k=(e,t)=>{if(e===null)return{};let i=Object.keys(e);i[0][0]==="."||(e={".":e},i=["."]);const n={},a=[];for(const s of i){const o=j(t,e[s]),r=s.includes(y);for(const f in o){if(!Object.hasOwn(o,f))continue;const d=o[f];if(d)for(let u of d){let g=s;if(r){const p=Array.isArray(u);g=s.split(y).join(p?u[1]:"_"),p&&([u]=u)}n[g]||(n[g]={}),n[g][f]=Array.isArray(u)?u[0]:u}else a.push([r?w(s):s,f])}}const l={};for(const s in n){if(!Object.hasOwn(n,s))continue;const o=Object.entries(n[s]).filter(([r])=>!a.some(([f,d])=>(typeof f=="string"?f===s:v(f,s))&&r===d)).map(([r,f])=>[JSON.parse(r),f]).sort(([r],[f])=>r.length-f.length);o.length>0&&(l[s]=o)}return l},b=e=>[[["default"],e]],M=async(e,t=E.default.promises)=>{var i;const c=await t.readFile(h.default.join(e,"package.json"),"utf8"),n=JSON.parse(c),a=await m(t,e);if(n.exports!==void 0)return k(n.exports,a);const l=/\.(?:json|[cm]?js|d\.ts)$/,s=Object.fromEntries(a.filter(r=>l.test(r)).map(r=>[r,b(r)]));let o=(i=n.main)!=null?i:"./index.js";o[0]!=="."&&(o=`./${o}`);for(const r of["",".js",".json"]){const f=o+r;if(a.includes(f)){s["."]=b(f),s[f]=b(f);break}}return s};exports.getPackageEntryPoints=M; ++"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _=require("fs"),A=require("path");function O(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var h=O(_),u=O(A);const v=async(e,t,a)=>{const l=await e.readdir(t);return(await Promise.all(l.map(async r=>{const c=u.default.join(t,r);if((await e.stat(c)).isDirectory()){const o=await v(e,c,!0);return a?o:o.map(s=>`./${u.default.relative(t,s)}`)}return a?c:`./${u.default.relative(t,c)}`}))).flat()},k=(e,t,a)=>e.readdirSync(t).map(r=>{const c=u.default.join(t,r);if(e.statSync(c).isDirectory()){const o=k(e,c,!0);return a?o:o.map(s=>`./${u.default.relative(t,s)}`)}return a?c:`./${u.default.relative(t,c)}`}).flat(),p="*",E=e=>{const t=e.split(p),a=t[0],l=t.length-1,n=t[l],r=t.slice(1,l);return{prefix:a,middle:r,suffix:n}},S=({prefix:e,suffix:t,middle:a},l)=>{if(!l.startsWith(e)||!l.endsWith(t))return;const n=l.slice(e.length,-t.length||void 0);if(a.length===0)return n;let r=0,c="";for(const i of a){const o=n.indexOf(i,r);if(o===-1)return;const s=n.slice(r,o);if(!c)c=s;else if(c!==s)return;r=o+i.length}return c},b=(e,t,a=[],l={})=>{if(t===null||typeof t=="string"){a.length===0&&a.push("default");const n=JSON.stringify(a);if(!Object.hasOwn(l,n))if(t===null)l[n]=t;else if(t.includes(p)){const r=E(t);l[n]=e.map(c=>{const i=S(r,c);return i&&[c,i]}).filter(c=>c!==void 0)}else e.includes(t)&&(l[n]=[t])}else if(Array.isArray(t))for(const n of t)b(e,n,a,l);else if(typeof t=="object"&&t)for(const n in t){if(!Object.hasOwn(t,n))continue;const r=a.slice();r.includes(n)||r.push(n),b(e,t[n],r.sort(),l)}return l},w=(e,t)=>{if(e===null)return{};let a=Object.keys(e);a[0][0]==="."||(e={".":e},a=["."]);const n={},r=[];for(const i of a){const o=b(t,e[i]),s=i.includes(p);for(const f in o){if(!Object.hasOwn(o,f))continue;const j=o[f];if(j)for(let d of j){let y=i;if(s){const m=Array.isArray(d);y=i.split(p).join(m?d[1]:"_"),m&&([d]=d)}n[y]||(n[y]={}),n[y][f]=Array.isArray(d)?d[0]:d}else r.push([s?E(i):i,f])}}const c={};for(const i in n){if(!Object.hasOwn(n,i))continue;const o=Object.entries(n[i]).filter(([s])=>!r.some(([f,j])=>(typeof f=="string"?f===i:S(f,i))&&s===j)).map(([s,f])=>[JSON.parse(s),f]).sort(([s],[f])=>s.length-f.length);o.length>0&&(c[i]=o)}return c},g=e=>[[["default"],e]],J=async(e,t=h.default.promises)=>{var a;const l=await t.readFile(u.default.join(e,"package.json"),"utf8"),n=JSON.parse(l),r=await v(t,e);if(n.exports!==void 0)return w(n.exports,r);const c=/\.(?:json|[cm]?js|d\.ts)$/,i=Object.fromEntries(r.filter(s=>c.test(s)).map(s=>[s,g(s)]));let o=(a=n.main)!=null?a:"./index.js";o[0]!=="."&&(o=`./${o}`);for(const s of["",".js",".json"]){const f=o+s;if(r.includes(f)){i["."]=g(f),i[f]=g(f);break}}return i},$=(e,t=h.default)=>{var a;const l=t.readFileSync(u.default.join(e,"package.json"),"utf8"),n=JSON.parse(l),r=k(t,e);if(n.exports!==void 0)return w(n.exports,r);const c=/\.(?:json|[cm]?js|d\.ts)$/,i=Object.fromEntries(r.filter(s=>c.test(s)).map(s=>[s,g(s)]));let o=(a=n.main)!=null?a:"./index.js";o[0]!=="."&&(o=`./${o}`);for(const s of["",".js",".json"]){const f=o+s;if(r.includes(f)){i["."]=g(f),i[f]=g(f);break}}return i};exports.getPackageEntryPoints=J,exports.getPackageEntryPointsSync=$; +diff --git a/node_modules/pkg-entry-points/dist/index.d.cts b/node_modules/pkg-entry-points/dist/index.d.cts +index c42093a..ce1e0ea 100644 +--- a/node_modules/pkg-entry-points/dist/index.d.cts ++++ b/node_modules/pkg-entry-points/dist/index.d.cts +@@ -5,5 +5,6 @@ type PackageEntryPoints = { + [subpath: string]: ConditionToPath[]; + }; + declare const getPackageEntryPoints: (packagePath: string, fs?: typeof _fs.promises) => Promise; ++declare const getPackageEntryPointsSync: (packagePath: string, fs?: typeof _fs) => PackageEntryPoints; + +-export { PackageEntryPoints, getPackageEntryPoints }; ++export { PackageEntryPoints, getPackageEntryPoints, getPackageEntryPointsSync }; +diff --git a/node_modules/pkg-entry-points/dist/index.d.mts b/node_modules/pkg-entry-points/dist/index.d.mts +index c42093a..ce1e0ea 100644 +--- a/node_modules/pkg-entry-points/dist/index.d.mts ++++ b/node_modules/pkg-entry-points/dist/index.d.mts +@@ -5,5 +5,6 @@ type PackageEntryPoints = { + [subpath: string]: ConditionToPath[]; + }; + declare const getPackageEntryPoints: (packagePath: string, fs?: typeof _fs.promises) => Promise; ++declare const getPackageEntryPointsSync: (packagePath: string, fs?: typeof _fs) => PackageEntryPoints; + +-export { PackageEntryPoints, getPackageEntryPoints }; ++export { PackageEntryPoints, getPackageEntryPoints, getPackageEntryPointsSync }; +diff --git a/node_modules/pkg-entry-points/dist/index.mjs b/node_modules/pkg-entry-points/dist/index.mjs +index 2dade03..7a55a55 100755 +--- a/node_modules/pkg-entry-points/dist/index.mjs ++++ b/node_modules/pkg-entry-points/dist/index.mjs +@@ -1 +1 @@ +-import A from"fs";import g from"path";const O=async(e,t,i)=>{const c=await e.readdir(t);return(await Promise.all(c.map(async r=>{const o=g.join(t,r);if((await e.stat(o)).isDirectory()){const l=await O(e,o,!0);return i?l:l.map(a=>`./${g.relative(t,a)}`)}return i?o:`./${g.relative(t,o)}`}))).flat()},j="*",b=e=>{const t=e.split(j),i=t[0],c=t.length-1,n=t[c],r=t.slice(1,c);return{prefix:i,middle:r,suffix:n}},w=({prefix:e,suffix:t,middle:i},c)=>{if(!c.startsWith(e)||!c.endsWith(t))return;const n=c.slice(e.length,-t.length||void 0);if(i.length===0)return n;let r=0,o="";for(const s of i){const l=n.indexOf(s,r);if(l===-1)return;const a=n.slice(r,l);if(!o)o=a;else if(o!==a)return;r=l+s.length}return o},y=(e,t,i=[],c={})=>{if(t===null||typeof t=="string"){i.length===0&&i.push("default");const n=JSON.stringify(i);if(!Object.hasOwn(c,n))if(t===null)c[n]=t;else if(t.includes(j)){const r=b(t);c[n]=e.map(o=>{const s=w(r,o);return s&&[o,s]}).filter(o=>o!==void 0)}else e.includes(t)&&(c[n]=[t])}else if(Array.isArray(t))for(const n of t)y(e,n,i,c);else if(typeof t=="object"&&t)for(const n in t){if(!Object.hasOwn(t,n))continue;const r=i.slice();r.includes(n)||r.push(n),y(e,t[n],r.sort(),c)}return c},E=(e,t)=>{if(e===null)return{};let i=Object.keys(e);i[0][0]==="."||(e={".":e},i=["."]);const n={},r=[];for(const s of i){const l=y(t,e[s]),a=s.includes(j);for(const f in l){if(!Object.hasOwn(l,f))continue;const h=l[f];if(h)for(let u of h){let d=s;if(a){const p=Array.isArray(u);d=s.split(j).join(p?u[1]:"_"),p&&([u]=u)}n[d]||(n[d]={}),n[d][f]=Array.isArray(u)?u[0]:u}else r.push([a?b(s):s,f])}}const o={};for(const s in n){if(!Object.hasOwn(n,s))continue;const l=Object.entries(n[s]).filter(([a])=>!r.some(([f,h])=>(typeof f=="string"?f===s:w(f,s))&&a===h)).map(([a,f])=>[JSON.parse(a),f]).sort(([a],[f])=>a.length-f.length);l.length>0&&(o[s]=l)}return o},m=e=>[[["default"],e]],v=async(e,t=A.promises)=>{var i;const c=await t.readFile(g.join(e,"package.json"),"utf8"),n=JSON.parse(c),r=await O(t,e);if(n.exports!==void 0)return E(n.exports,r);const o=/\.(?:json|[cm]?js|d\.ts)$/,s=Object.fromEntries(r.filter(a=>o.test(a)).map(a=>[a,m(a)]));let l=(i=n.main)!=null?i:"./index.js";l[0]!=="."&&(l=`./${l}`);for(const a of["",".js",".json"]){const f=l+a;if(r.includes(f)){s["."]=m(f),s[f]=m(f);break}}return s};export{v as getPackageEntryPoints}; ++import b from"fs";import u from"path";const h=async(e,t,r)=>{const l=await e.readdir(t);return(await Promise.all(l.map(async o=>{const c=u.join(t,o);if((await e.stat(c)).isDirectory()){const a=await h(e,c,!0);return r?a:a.map(s=>`./${u.relative(t,s)}`)}return r?c:`./${u.relative(t,c)}`}))).flat()},w=(e,t,r)=>e.readdirSync(t).map(o=>{const c=u.join(t,o);if(e.statSync(c).isDirectory()){const a=w(e,c,!0);return r?a:a.map(s=>`./${u.relative(t,s)}`)}return r?c:`./${u.relative(t,c)}`}).flat(),y="*",S=e=>{const t=e.split(y),r=t[0],l=t.length-1,n=t[l],o=t.slice(1,l);return{prefix:r,middle:o,suffix:n}},k=({prefix:e,suffix:t,middle:r},l)=>{if(!l.startsWith(e)||!l.endsWith(t))return;const n=l.slice(e.length,-t.length||void 0);if(r.length===0)return n;let o=0,c="";for(const i of r){const a=n.indexOf(i,o);if(a===-1)return;const s=n.slice(o,a);if(!c)c=s;else if(c!==s)return;o=a+i.length}return c},m=(e,t,r=[],l={})=>{if(t===null||typeof t=="string"){r.length===0&&r.push("default");const n=JSON.stringify(r);if(!Object.hasOwn(l,n))if(t===null)l[n]=t;else if(t.includes(y)){const o=S(t);l[n]=e.map(c=>{const i=k(o,c);return i&&[c,i]}).filter(c=>c!==void 0)}else e.includes(t)&&(l[n]=[t])}else if(Array.isArray(t))for(const n of t)m(e,n,r,l);else if(typeof t=="object"&&t)for(const n in t){if(!Object.hasOwn(t,n))continue;const o=r.slice();o.includes(n)||o.push(n),m(e,t[n],o.sort(),l)}return l},E=(e,t)=>{if(e===null)return{};let r=Object.keys(e);r[0][0]==="."||(e={".":e},r=["."]);const n={},o=[];for(const i of r){const a=m(t,e[i]),s=i.includes(y);for(const f in a){if(!Object.hasOwn(a,f))continue;const p=a[f];if(p)for(let g of p){let d=i;if(s){const O=Array.isArray(g);d=i.split(y).join(O?g[1]:"_"),O&&([g]=g)}n[d]||(n[d]={}),n[d][f]=Array.isArray(g)?g[0]:g}else o.push([s?S(i):i,f])}}const c={};for(const i in n){if(!Object.hasOwn(n,i))continue;const a=Object.entries(n[i]).filter(([s])=>!o.some(([f,p])=>(typeof f=="string"?f===i:k(f,i))&&s===p)).map(([s,f])=>[JSON.parse(s),f]).sort(([s],[f])=>s.length-f.length);a.length>0&&(c[i]=a)}return c},j=e=>[[["default"],e]],v=async(e,t=b.promises)=>{var r;const l=await t.readFile(u.join(e,"package.json"),"utf8"),n=JSON.parse(l),o=await h(t,e);if(n.exports!==void 0)return E(n.exports,o);const c=/\.(?:json|[cm]?js|d\.ts)$/,i=Object.fromEntries(o.filter(s=>c.test(s)).map(s=>[s,j(s)]));let a=(r=n.main)!=null?r:"./index.js";a[0]!=="."&&(a=`./${a}`);for(const s of["",".js",".json"]){const f=a+s;if(o.includes(f)){i["."]=j(f),i[f]=j(f);break}}return i},A=(e,t=b)=>{var r;const l=t.readFileSync(u.join(e,"package.json"),"utf8"),n=JSON.parse(l),o=w(t,e);if(n.exports!==void 0)return E(n.exports,o);const c=/\.(?:json|[cm]?js|d\.ts)$/,i=Object.fromEntries(o.filter(s=>c.test(s)).map(s=>[s,j(s)]));let a=(r=n.main)!=null?r:"./index.js";a[0]!=="."&&(a=`./${a}`);for(const s of["",".js",".json"]){const f=a+s;if(o.includes(f)){i["."]=j(f),i[f]=j(f);break}}return i};export{v as getPackageEntryPoints,A as getPackageEntryPointsSync}; From de5f6e5134f7416fc559b96cd51cf6c5e41f7688 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Thu, 2 May 2024 16:12:12 +0200 Subject: [PATCH 7/8] Use new util for watching --- packages/ember-auto-import/ts/auto-import.ts | 3 +++ packages/ember-auto-import/ts/package.ts | 9 ++++++--- 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/packages/ember-auto-import/ts/auto-import.ts b/packages/ember-auto-import/ts/auto-import.ts index 15377c98..96eb7680 100644 --- a/packages/ember-auto-import/ts/auto-import.ts +++ b/packages/ember-auto-import/ts/auto-import.ts @@ -26,8 +26,10 @@ import type { TransformOptions } from '@babel/core'; import { MARKER } from './analyzer-syntax'; import path from 'path'; import funnel from 'broccoli-funnel'; +import makeDebug from 'debug'; const debugTree = buildDebugCallback('ember-auto-import'); +const debugWatch = makeDebug('ember-auto-import:watch'); // This interface must be stable across all versions of ember-auto-import that // speak the same leader-election protocol. So don't change this unless you know @@ -244,6 +246,7 @@ function depsFor(allAppTree: Node, packages: Set) { let watched = pkg.watchedDirectories; if (watched) { deps = deps.concat(watched.map((dir) => new WatchedDir(dir))); + debugWatch(`Adding watched directories: ${watched.join(', ')}`); } } return deps; diff --git a/packages/ember-auto-import/ts/package.ts b/packages/ember-auto-import/ts/package.ts index 64e49668..e5c55b54 100644 --- a/packages/ember-auto-import/ts/package.ts +++ b/packages/ember-auto-import/ts/package.ts @@ -14,6 +14,7 @@ import type { PluginItem, TransformOptions } from '@babel/core'; import { MacrosConfig } from '@embroider/macros/src/node'; import minimatch from 'minimatch'; import { stripQuery } from './util'; +import { getWatchedDirectories } from './watch-utils'; // from child addon instance to their parent package const parentCache: WeakMap = new WeakMap(); @@ -514,13 +515,15 @@ export default class Package { for (let name of names) { let path = resolvePackagePath(name, cursor); if (!path) { - return undefined; + return []; } cursor = dirname(path); } - return cursor; + return getWatchedDirectories(cursor).map((relativeDir) => + join(cursor, relativeDir) + ); }) - .filter(Boolean) as string[]; + .flat(); } } From d874b8497271855b0ef5ed6e62c34a608c923c82 Mon Sep 17 00:00:00 2001 From: Simon Ihmig Date: Tue, 10 Sep 2024 17:55:00 +0200 Subject: [PATCH 8/8] Update pkg-entry-points, remove patch --- package-lock.json | 24 ++++++----- package.json | 1 - packages/ember-auto-import/package.json | 1 + packages/ember-auto-import/ts/watch-utils.ts | 1 - patches/pkg-entry-points+1.0.2.patch | 42 -------------------- 5 files changed, 16 insertions(+), 53 deletions(-) delete mode 100644 patches/pkg-entry-points+1.0.2.patch diff --git a/package-lock.json b/package-lock.json index 7ca1edbc..b77de0b8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -11,7 +11,6 @@ "types/*" ], "dependencies": { - "pkg-entry-points": "^1.0.2", "resolve-package-path": "^3.1.0", "semver": "^7.3.5" }, @@ -28585,14 +28584,6 @@ "node": ">=8" } }, - "node_modules/pkg-entry-points": { - "version": "1.0.2", - "resolved": "https://registry.npmjs.org/pkg-entry-points/-/pkg-entry-points-1.0.2.tgz", - "integrity": "sha512-LZKo+SJNMC80gk+vANlN+A5C7LIAGuyQ4aD6Ro3+ZDAd2Av+g8RRlMX4AXcutIH9Vdw27YpNk9GmanH9dch6Gg==", - "funding": { - "url": "https://github.com/privatenumber/pkg-entry-points?sponsor=1" - } - }, "node_modules/pkg-up": { "version": "3.1.0", "dev": true, @@ -35302,6 +35293,7 @@ "mini-css-extract-plugin": "^2.5.2", "minimatch": "^3.0.0", "parse5": "^6.0.1", + "pkg-entry-points": "^1.1.0", "resolve": "^1.20.0", "resolve-package-path": "^4.0.3", "semver": "^7.3.4", @@ -35599,6 +35591,14 @@ "@types/node": "*" } }, + "packages/ember-auto-import/node_modules/pkg-entry-points": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pkg-entry-points/-/pkg-entry-points-1.1.0.tgz", + "integrity": "sha512-9vL2T/he5Kb97GVY+V3Ih4jCC1lF3PQGIDUJIUqKM4Q6twmhrUSAa0OFj+kb8IEs4wYzEgB6kcc4oYy21kZnQw==", + "funding": { + "url": "https://github.com/privatenumber/pkg-entry-points?sponsor=1" + } + }, "packages/ember-auto-import/node_modules/resolve-package-path": { "version": "4.0.3", "license": "MIT", @@ -49788,6 +49788,7 @@ "minimatch": "^3.0.0", "npm-run-all": "^4.1.5", "parse5": "^6.0.1", + "pkg-entry-points": "^1.1.0", "prettier": "^2.5.1", "quick-temp": "^0.1.8", "qunit": "^2.17.2", @@ -49974,6 +49975,11 @@ "walk-sync": "^3.0.0" } }, + "pkg-entry-points": { + "version": "1.1.0", + "resolved": "https://registry.npmjs.org/pkg-entry-points/-/pkg-entry-points-1.1.0.tgz", + "integrity": "sha512-9vL2T/he5Kb97GVY+V3Ih4jCC1lF3PQGIDUJIUqKM4Q6twmhrUSAa0OFj+kb8IEs4wYzEgB6kcc4oYy21kZnQw==" + }, "resolve-package-path": { "version": "4.0.3", "requires": { diff --git a/package.json b/package.json index 6ee62cc3..82481164 100644 --- a/package.json +++ b/package.json @@ -13,7 +13,6 @@ "watch": "cd packages/ember-auto-import && npm run compile -- --watch" }, "dependencies": { - "pkg-entry-points": "^1.0.2", "resolve-package-path": "^3.1.0", "semver": "^7.3.5" }, diff --git a/packages/ember-auto-import/package.json b/packages/ember-auto-import/package.json index b7c221f0..cc208346 100644 --- a/packages/ember-auto-import/package.json +++ b/packages/ember-auto-import/package.json @@ -62,6 +62,7 @@ "mini-css-extract-plugin": "^2.5.2", "minimatch": "^3.0.0", "parse5": "^6.0.1", + "pkg-entry-points": "^1.1.0", "resolve": "^1.20.0", "resolve-package-path": "^4.0.3", "semver": "^7.3.4", diff --git a/packages/ember-auto-import/ts/watch-utils.ts b/packages/ember-auto-import/ts/watch-utils.ts index d0a40da5..4fea733e 100644 --- a/packages/ember-auto-import/ts/watch-utils.ts +++ b/packages/ember-auto-import/ts/watch-utils.ts @@ -1,6 +1,5 @@ import isSubdir from 'is-subdir'; import { dirname } from 'path'; -// @ts-expect-error types don't resolve as this package exposes them only via package.json exports, which our old TS version does not support import { getPackageEntryPointsSync } from 'pkg-entry-points'; // copied from pkg-entry-points, as we cannot use their types, see comment above diff --git a/patches/pkg-entry-points+1.0.2.patch b/patches/pkg-entry-points+1.0.2.patch deleted file mode 100644 index 270333a5..00000000 --- a/patches/pkg-entry-points+1.0.2.patch +++ /dev/null @@ -1,42 +0,0 @@ -diff --git a/node_modules/pkg-entry-points/.DS_Store b/node_modules/pkg-entry-points/.DS_Store -new file mode 100644 -index 0000000..c88a062 -Binary files /dev/null and b/node_modules/pkg-entry-points/.DS_Store differ -diff --git a/node_modules/pkg-entry-points/dist/index.cjs b/node_modules/pkg-entry-points/dist/index.cjs -index ff7dada..4da9967 100755 ---- a/node_modules/pkg-entry-points/dist/index.cjs -+++ b/node_modules/pkg-entry-points/dist/index.cjs -@@ -1 +1 @@ --"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _=require("fs"),A=require("path");function O(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var E=O(_),h=O(A);const m=async(e,t,i)=>{const c=await e.readdir(t);return(await Promise.all(c.map(async a=>{const l=h.default.join(t,a);if((await e.stat(l)).isDirectory()){const o=await m(e,l,!0);return i?o:o.map(r=>`./${h.default.relative(t,r)}`)}return i?l:`./${h.default.relative(t,l)}`}))).flat()},y="*",w=e=>{const t=e.split(y),i=t[0],c=t.length-1,n=t[c],a=t.slice(1,c);return{prefix:i,middle:a,suffix:n}},v=({prefix:e,suffix:t,middle:i},c)=>{if(!c.startsWith(e)||!c.endsWith(t))return;const n=c.slice(e.length,-t.length||void 0);if(i.length===0)return n;let a=0,l="";for(const s of i){const o=n.indexOf(s,a);if(o===-1)return;const r=n.slice(a,o);if(!l)l=r;else if(l!==r)return;a=o+s.length}return l},j=(e,t,i=[],c={})=>{if(t===null||typeof t=="string"){i.length===0&&i.push("default");const n=JSON.stringify(i);if(!Object.hasOwn(c,n))if(t===null)c[n]=t;else if(t.includes(y)){const a=w(t);c[n]=e.map(l=>{const s=v(a,l);return s&&[l,s]}).filter(l=>l!==void 0)}else e.includes(t)&&(c[n]=[t])}else if(Array.isArray(t))for(const n of t)j(e,n,i,c);else if(typeof t=="object"&&t)for(const n in t){if(!Object.hasOwn(t,n))continue;const a=i.slice();a.includes(n)||a.push(n),j(e,t[n],a.sort(),c)}return c},k=(e,t)=>{if(e===null)return{};let i=Object.keys(e);i[0][0]==="."||(e={".":e},i=["."]);const n={},a=[];for(const s of i){const o=j(t,e[s]),r=s.includes(y);for(const f in o){if(!Object.hasOwn(o,f))continue;const d=o[f];if(d)for(let u of d){let g=s;if(r){const p=Array.isArray(u);g=s.split(y).join(p?u[1]:"_"),p&&([u]=u)}n[g]||(n[g]={}),n[g][f]=Array.isArray(u)?u[0]:u}else a.push([r?w(s):s,f])}}const l={};for(const s in n){if(!Object.hasOwn(n,s))continue;const o=Object.entries(n[s]).filter(([r])=>!a.some(([f,d])=>(typeof f=="string"?f===s:v(f,s))&&r===d)).map(([r,f])=>[JSON.parse(r),f]).sort(([r],[f])=>r.length-f.length);o.length>0&&(l[s]=o)}return l},b=e=>[[["default"],e]],M=async(e,t=E.default.promises)=>{var i;const c=await t.readFile(h.default.join(e,"package.json"),"utf8"),n=JSON.parse(c),a=await m(t,e);if(n.exports!==void 0)return k(n.exports,a);const l=/\.(?:json|[cm]?js|d\.ts)$/,s=Object.fromEntries(a.filter(r=>l.test(r)).map(r=>[r,b(r)]));let o=(i=n.main)!=null?i:"./index.js";o[0]!=="."&&(o=`./${o}`);for(const r of["",".js",".json"]){const f=o+r;if(a.includes(f)){s["."]=b(f),s[f]=b(f);break}}return s};exports.getPackageEntryPoints=M; -+"use strict";Object.defineProperty(exports,"__esModule",{value:!0});var _=require("fs"),A=require("path");function O(e){return e&&typeof e=="object"&&"default"in e?e:{default:e}}var h=O(_),u=O(A);const v=async(e,t,a)=>{const l=await e.readdir(t);return(await Promise.all(l.map(async r=>{const c=u.default.join(t,r);if((await e.stat(c)).isDirectory()){const o=await v(e,c,!0);return a?o:o.map(s=>`./${u.default.relative(t,s)}`)}return a?c:`./${u.default.relative(t,c)}`}))).flat()},k=(e,t,a)=>e.readdirSync(t).map(r=>{const c=u.default.join(t,r);if(e.statSync(c).isDirectory()){const o=k(e,c,!0);return a?o:o.map(s=>`./${u.default.relative(t,s)}`)}return a?c:`./${u.default.relative(t,c)}`}).flat(),p="*",E=e=>{const t=e.split(p),a=t[0],l=t.length-1,n=t[l],r=t.slice(1,l);return{prefix:a,middle:r,suffix:n}},S=({prefix:e,suffix:t,middle:a},l)=>{if(!l.startsWith(e)||!l.endsWith(t))return;const n=l.slice(e.length,-t.length||void 0);if(a.length===0)return n;let r=0,c="";for(const i of a){const o=n.indexOf(i,r);if(o===-1)return;const s=n.slice(r,o);if(!c)c=s;else if(c!==s)return;r=o+i.length}return c},b=(e,t,a=[],l={})=>{if(t===null||typeof t=="string"){a.length===0&&a.push("default");const n=JSON.stringify(a);if(!Object.hasOwn(l,n))if(t===null)l[n]=t;else if(t.includes(p)){const r=E(t);l[n]=e.map(c=>{const i=S(r,c);return i&&[c,i]}).filter(c=>c!==void 0)}else e.includes(t)&&(l[n]=[t])}else if(Array.isArray(t))for(const n of t)b(e,n,a,l);else if(typeof t=="object"&&t)for(const n in t){if(!Object.hasOwn(t,n))continue;const r=a.slice();r.includes(n)||r.push(n),b(e,t[n],r.sort(),l)}return l},w=(e,t)=>{if(e===null)return{};let a=Object.keys(e);a[0][0]==="."||(e={".":e},a=["."]);const n={},r=[];for(const i of a){const o=b(t,e[i]),s=i.includes(p);for(const f in o){if(!Object.hasOwn(o,f))continue;const j=o[f];if(j)for(let d of j){let y=i;if(s){const m=Array.isArray(d);y=i.split(p).join(m?d[1]:"_"),m&&([d]=d)}n[y]||(n[y]={}),n[y][f]=Array.isArray(d)?d[0]:d}else r.push([s?E(i):i,f])}}const c={};for(const i in n){if(!Object.hasOwn(n,i))continue;const o=Object.entries(n[i]).filter(([s])=>!r.some(([f,j])=>(typeof f=="string"?f===i:S(f,i))&&s===j)).map(([s,f])=>[JSON.parse(s),f]).sort(([s],[f])=>s.length-f.length);o.length>0&&(c[i]=o)}return c},g=e=>[[["default"],e]],J=async(e,t=h.default.promises)=>{var a;const l=await t.readFile(u.default.join(e,"package.json"),"utf8"),n=JSON.parse(l),r=await v(t,e);if(n.exports!==void 0)return w(n.exports,r);const c=/\.(?:json|[cm]?js|d\.ts)$/,i=Object.fromEntries(r.filter(s=>c.test(s)).map(s=>[s,g(s)]));let o=(a=n.main)!=null?a:"./index.js";o[0]!=="."&&(o=`./${o}`);for(const s of["",".js",".json"]){const f=o+s;if(r.includes(f)){i["."]=g(f),i[f]=g(f);break}}return i},$=(e,t=h.default)=>{var a;const l=t.readFileSync(u.default.join(e,"package.json"),"utf8"),n=JSON.parse(l),r=k(t,e);if(n.exports!==void 0)return w(n.exports,r);const c=/\.(?:json|[cm]?js|d\.ts)$/,i=Object.fromEntries(r.filter(s=>c.test(s)).map(s=>[s,g(s)]));let o=(a=n.main)!=null?a:"./index.js";o[0]!=="."&&(o=`./${o}`);for(const s of["",".js",".json"]){const f=o+s;if(r.includes(f)){i["."]=g(f),i[f]=g(f);break}}return i};exports.getPackageEntryPoints=J,exports.getPackageEntryPointsSync=$; -diff --git a/node_modules/pkg-entry-points/dist/index.d.cts b/node_modules/pkg-entry-points/dist/index.d.cts -index c42093a..ce1e0ea 100644 ---- a/node_modules/pkg-entry-points/dist/index.d.cts -+++ b/node_modules/pkg-entry-points/dist/index.d.cts -@@ -5,5 +5,6 @@ type PackageEntryPoints = { - [subpath: string]: ConditionToPath[]; - }; - declare const getPackageEntryPoints: (packagePath: string, fs?: typeof _fs.promises) => Promise; -+declare const getPackageEntryPointsSync: (packagePath: string, fs?: typeof _fs) => PackageEntryPoints; - --export { PackageEntryPoints, getPackageEntryPoints }; -+export { PackageEntryPoints, getPackageEntryPoints, getPackageEntryPointsSync }; -diff --git a/node_modules/pkg-entry-points/dist/index.d.mts b/node_modules/pkg-entry-points/dist/index.d.mts -index c42093a..ce1e0ea 100644 ---- a/node_modules/pkg-entry-points/dist/index.d.mts -+++ b/node_modules/pkg-entry-points/dist/index.d.mts -@@ -5,5 +5,6 @@ type PackageEntryPoints = { - [subpath: string]: ConditionToPath[]; - }; - declare const getPackageEntryPoints: (packagePath: string, fs?: typeof _fs.promises) => Promise; -+declare const getPackageEntryPointsSync: (packagePath: string, fs?: typeof _fs) => PackageEntryPoints; - --export { PackageEntryPoints, getPackageEntryPoints }; -+export { PackageEntryPoints, getPackageEntryPoints, getPackageEntryPointsSync }; -diff --git a/node_modules/pkg-entry-points/dist/index.mjs b/node_modules/pkg-entry-points/dist/index.mjs -index 2dade03..7a55a55 100755 ---- a/node_modules/pkg-entry-points/dist/index.mjs -+++ b/node_modules/pkg-entry-points/dist/index.mjs -@@ -1 +1 @@ --import A from"fs";import g from"path";const O=async(e,t,i)=>{const c=await e.readdir(t);return(await Promise.all(c.map(async r=>{const o=g.join(t,r);if((await e.stat(o)).isDirectory()){const l=await O(e,o,!0);return i?l:l.map(a=>`./${g.relative(t,a)}`)}return i?o:`./${g.relative(t,o)}`}))).flat()},j="*",b=e=>{const t=e.split(j),i=t[0],c=t.length-1,n=t[c],r=t.slice(1,c);return{prefix:i,middle:r,suffix:n}},w=({prefix:e,suffix:t,middle:i},c)=>{if(!c.startsWith(e)||!c.endsWith(t))return;const n=c.slice(e.length,-t.length||void 0);if(i.length===0)return n;let r=0,o="";for(const s of i){const l=n.indexOf(s,r);if(l===-1)return;const a=n.slice(r,l);if(!o)o=a;else if(o!==a)return;r=l+s.length}return o},y=(e,t,i=[],c={})=>{if(t===null||typeof t=="string"){i.length===0&&i.push("default");const n=JSON.stringify(i);if(!Object.hasOwn(c,n))if(t===null)c[n]=t;else if(t.includes(j)){const r=b(t);c[n]=e.map(o=>{const s=w(r,o);return s&&[o,s]}).filter(o=>o!==void 0)}else e.includes(t)&&(c[n]=[t])}else if(Array.isArray(t))for(const n of t)y(e,n,i,c);else if(typeof t=="object"&&t)for(const n in t){if(!Object.hasOwn(t,n))continue;const r=i.slice();r.includes(n)||r.push(n),y(e,t[n],r.sort(),c)}return c},E=(e,t)=>{if(e===null)return{};let i=Object.keys(e);i[0][0]==="."||(e={".":e},i=["."]);const n={},r=[];for(const s of i){const l=y(t,e[s]),a=s.includes(j);for(const f in l){if(!Object.hasOwn(l,f))continue;const h=l[f];if(h)for(let u of h){let d=s;if(a){const p=Array.isArray(u);d=s.split(j).join(p?u[1]:"_"),p&&([u]=u)}n[d]||(n[d]={}),n[d][f]=Array.isArray(u)?u[0]:u}else r.push([a?b(s):s,f])}}const o={};for(const s in n){if(!Object.hasOwn(n,s))continue;const l=Object.entries(n[s]).filter(([a])=>!r.some(([f,h])=>(typeof f=="string"?f===s:w(f,s))&&a===h)).map(([a,f])=>[JSON.parse(a),f]).sort(([a],[f])=>a.length-f.length);l.length>0&&(o[s]=l)}return o},m=e=>[[["default"],e]],v=async(e,t=A.promises)=>{var i;const c=await t.readFile(g.join(e,"package.json"),"utf8"),n=JSON.parse(c),r=await O(t,e);if(n.exports!==void 0)return E(n.exports,r);const o=/\.(?:json|[cm]?js|d\.ts)$/,s=Object.fromEntries(r.filter(a=>o.test(a)).map(a=>[a,m(a)]));let l=(i=n.main)!=null?i:"./index.js";l[0]!=="."&&(l=`./${l}`);for(const a of["",".js",".json"]){const f=l+a;if(r.includes(f)){s["."]=m(f),s[f]=m(f);break}}return s};export{v as getPackageEntryPoints}; -+import b from"fs";import u from"path";const h=async(e,t,r)=>{const l=await e.readdir(t);return(await Promise.all(l.map(async o=>{const c=u.join(t,o);if((await e.stat(c)).isDirectory()){const a=await h(e,c,!0);return r?a:a.map(s=>`./${u.relative(t,s)}`)}return r?c:`./${u.relative(t,c)}`}))).flat()},w=(e,t,r)=>e.readdirSync(t).map(o=>{const c=u.join(t,o);if(e.statSync(c).isDirectory()){const a=w(e,c,!0);return r?a:a.map(s=>`./${u.relative(t,s)}`)}return r?c:`./${u.relative(t,c)}`}).flat(),y="*",S=e=>{const t=e.split(y),r=t[0],l=t.length-1,n=t[l],o=t.slice(1,l);return{prefix:r,middle:o,suffix:n}},k=({prefix:e,suffix:t,middle:r},l)=>{if(!l.startsWith(e)||!l.endsWith(t))return;const n=l.slice(e.length,-t.length||void 0);if(r.length===0)return n;let o=0,c="";for(const i of r){const a=n.indexOf(i,o);if(a===-1)return;const s=n.slice(o,a);if(!c)c=s;else if(c!==s)return;o=a+i.length}return c},m=(e,t,r=[],l={})=>{if(t===null||typeof t=="string"){r.length===0&&r.push("default");const n=JSON.stringify(r);if(!Object.hasOwn(l,n))if(t===null)l[n]=t;else if(t.includes(y)){const o=S(t);l[n]=e.map(c=>{const i=k(o,c);return i&&[c,i]}).filter(c=>c!==void 0)}else e.includes(t)&&(l[n]=[t])}else if(Array.isArray(t))for(const n of t)m(e,n,r,l);else if(typeof t=="object"&&t)for(const n in t){if(!Object.hasOwn(t,n))continue;const o=r.slice();o.includes(n)||o.push(n),m(e,t[n],o.sort(),l)}return l},E=(e,t)=>{if(e===null)return{};let r=Object.keys(e);r[0][0]==="."||(e={".":e},r=["."]);const n={},o=[];for(const i of r){const a=m(t,e[i]),s=i.includes(y);for(const f in a){if(!Object.hasOwn(a,f))continue;const p=a[f];if(p)for(let g of p){let d=i;if(s){const O=Array.isArray(g);d=i.split(y).join(O?g[1]:"_"),O&&([g]=g)}n[d]||(n[d]={}),n[d][f]=Array.isArray(g)?g[0]:g}else o.push([s?S(i):i,f])}}const c={};for(const i in n){if(!Object.hasOwn(n,i))continue;const a=Object.entries(n[i]).filter(([s])=>!o.some(([f,p])=>(typeof f=="string"?f===i:k(f,i))&&s===p)).map(([s,f])=>[JSON.parse(s),f]).sort(([s],[f])=>s.length-f.length);a.length>0&&(c[i]=a)}return c},j=e=>[[["default"],e]],v=async(e,t=b.promises)=>{var r;const l=await t.readFile(u.join(e,"package.json"),"utf8"),n=JSON.parse(l),o=await h(t,e);if(n.exports!==void 0)return E(n.exports,o);const c=/\.(?:json|[cm]?js|d\.ts)$/,i=Object.fromEntries(o.filter(s=>c.test(s)).map(s=>[s,j(s)]));let a=(r=n.main)!=null?r:"./index.js";a[0]!=="."&&(a=`./${a}`);for(const s of["",".js",".json"]){const f=a+s;if(o.includes(f)){i["."]=j(f),i[f]=j(f);break}}return i},A=(e,t=b)=>{var r;const l=t.readFileSync(u.join(e,"package.json"),"utf8"),n=JSON.parse(l),o=w(t,e);if(n.exports!==void 0)return E(n.exports,o);const c=/\.(?:json|[cm]?js|d\.ts)$/,i=Object.fromEntries(o.filter(s=>c.test(s)).map(s=>[s,j(s)]));let a=(r=n.main)!=null?r:"./index.js";a[0]!=="."&&(a=`./${a}`);for(const s of["",".js",".json"]){const f=a+s;if(o.includes(f)){i["."]=j(f),i[f]=j(f);break}}return i};export{v as getPackageEntryPoints,A as getPackageEntryPointsSync};