Skip to content

Commit

Permalink
fix: tapjs#11, basic subpath import support (format)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajvincent committed Jun 16, 2024
1 parent 3439cf4 commit 3d2a2b4
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 40 deletions.
7 changes: 5 additions & 2 deletions src/classify-module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down
43 changes: 27 additions & 16 deletions src/hooks/hooks.mts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, string> };
const { imports } = contents as {
imports: Record<string, string>
}
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)
Expand Down
25 changes: 15 additions & 10 deletions src/service/get-package-json.ts
Original file line number Diff line number Diff line change
@@ -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,
};
}
}
}
}
4 changes: 2 additions & 2 deletions src/service/transpile-only.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down
25 changes: 15 additions & 10 deletions test/hooks/hooks.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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 }
}

Expand All @@ -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)
Expand Down

0 comments on commit 3d2a2b4

Please sign in to comment.