diff --git a/src/classify-module.ts b/src/classify-module.ts index 291a647..114b420 100644 --- a/src/classify-module.ts +++ b/src/classify-module.ts @@ -21,10 +21,13 @@ const readPJType = cachedMtime( }) ) -export const classifyModule = (fileName: string): PackageJsonType | 'json' => { +export const classifyModule = ( + fileName: string +): PackageJsonType | 'json' => { if (fileName.endsWith('.json')) { return 'json' - } if (fileName.endsWith('.cts') || fileName.endsWith('.cjs')) { + } + if (fileName.endsWith('.cts') || fileName.endsWith('.cjs')) { return 'commonjs' } else if (fileName.endsWith('.mts') || fileName.endsWith('.mjs')) { return 'module' diff --git a/src/hooks/hooks.mts b/src/hooks/hooks.mts index d812543..c6de4a2 100644 --- a/src/hooks/hooks.mts +++ b/src/hooks/hooks.mts @@ -58,31 +58,42 @@ export const resolve: ResolveHook = async ( ) => { const { parentURL } = context - if (url.startsWith("#")) { - const { contents, pathToJSON } = getPackageJSON(process.cwd())!; + if (url.startsWith('#')) { + const { contents, pathToJSON } = getPackageJSON(process.cwd())! if (pathToJSON && contents) { - const { imports } = contents as { imports: Record }; + const { imports } = contents as { + imports: Record + } if (imports) { - for (let [importSubpath, relativeSubpath] of Object.entries(imports)) { - if (!importSubpath.startsWith("#") || !importSubpath.endsWith("/*") || !relativeSubpath.endsWith("/*")) - continue; - importSubpath = relative(dirname(importSubpath), url); - if (importSubpath.includes("#")) - continue; + for (let [importSubpath, relativeSubpath] of Object.entries( + imports + )) { + if ( + !importSubpath.startsWith('#') || + !importSubpath.endsWith('/*') || + !relativeSubpath.endsWith('/*') + ) + continue + importSubpath = relative(dirname(importSubpath), url) + if (importSubpath.includes('#')) continue - url = pathResolve(dirname(pathToJSON), dirname(relativeSubpath), importSubpath) - break; + url = pathResolve( + dirname(pathToJSON), + dirname(relativeSubpath), + importSubpath + ) + break } } } } let target = - /* c8 ignore start */ - parentURL && (url.startsWith('./') || url.startsWith('../')) - ? /* c8 ignore stop */ - String(new URL(url, parentURL)) - : url + /* c8 ignore start */ + parentURL && (url.startsWith('./') || url.startsWith('../')) + ? /* c8 ignore stop */ + String(new URL(url, parentURL)) + : url return nextResolve( target.startsWith('file://') && !startsWithCS(target, nm) diff --git a/src/service/get-package-json.ts b/src/service/get-package-json.ts index a3e6678..fd105fa 100644 --- a/src/service/get-package-json.ts +++ b/src/service/get-package-json.ts @@ -1,21 +1,26 @@ import { walkUp } from 'walk-up-path' -import { normalizePath, fileExists, readFile } from '../ts-sys-cached.js' +import { + normalizePath, + fileExists, + readFile, +} from '../ts-sys-cached.js' function getPackageJSONPath(dir: string): string | undefined { for (const d of walkUp(dir)) { - let pjPath = normalizePath(d + "/package.json"); - if (fileExists(pjPath)) - return pjPath; + let pjPath = normalizePath(d + '/package.json') + if (fileExists(pjPath)) return pjPath } } -export default function getPackageJSON(dir: string): {contents: object, pathToJSON: string} | undefined { - const pathToJSON = getPackageJSONPath(dir); +export default function getPackageJSON( + dir: string +): { contents: object; pathToJSON: string } | undefined { + const pathToJSON = getPackageJSONPath(dir) if (pathToJSON) { - const json = readFile(pathToJSON) as string; + const json = readFile(pathToJSON) as string return { - contents: JSON.parse(json) as object, + contents: JSON.parse(json) as object, pathToJSON, - }; + } } -} \ No newline at end of file +} diff --git a/src/service/transpile-only.ts b/src/service/transpile-only.ts index ca0cf2f..cc08fe1 100644 --- a/src/service/transpile-only.ts +++ b/src/service/transpile-only.ts @@ -133,9 +133,9 @@ const createTsTranspileModule = ({ packageJsonFileName = dir + '/package.json' if (pjType) packageJsonType = pjType else { - const pj = (getPackageJSON(dir))?.contents as { + const pj = getPackageJSON(dir)?.contents as { type?: 'commonjs' | 'module' - }; + } if (pj?.type) { packageJsonType = pj.type } diff --git a/test/hooks/hooks.ts b/test/hooks/hooks.ts index 34b7264..360a5aa 100644 --- a/test/hooks/hooks.ts +++ b/test/hooks/hooks.ts @@ -113,15 +113,15 @@ t.test('resolve', async t => { t.test('resolve with subpath import', async t => { const dir = t.testdir({ 'package.json': JSON.stringify({ - "type": "module", - "imports": { - "#source/*": "./my/source/*" - } + type: 'module', + imports: { + '#source/*': './my/source/*', + }, }), - "project": { + project: { 'foo.ts': 'console.log("feet")', - } + }, }) const cwd = process.cwd() @@ -137,8 +137,10 @@ t.test('resolve with subpath import', async t => { } )) as typeof import('../../dist/esm/hooks/hooks.mjs') - const nextResolve = (url: string, context: ResolveHookContext | undefined) => { - console.log(url); + const nextResolve = ( + url: string, + context: ResolveHookContext | undefined + ) => { return { url, parentURL: context?.parentURL } } @@ -148,11 +150,14 @@ t.test('resolve with subpath import', async t => { { parentURL: 'file:///asdf/asdf.js', conditions: [], - importAssertions: {} + importAssertions: {}, }, nextResolve ), - { url: dir + '/my/source/subdir/constant.js', parentURL: 'file:///asdf/asdf.js' } + { + url: dir + '/my/source/subdir/constant.js', + parentURL: 'file:///asdf/asdf.js', + } ) process.chdir(cwd)