Skip to content
Merged
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
1 change: 1 addition & 0 deletions packages/runtime-bundler/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
"dependencies": {
"@douglasneuroinformatics/libjs": "catalog:",
"esbuild": "catalog:",
"oxc-parser": "catalog:",
"zod": "workspace:zod__3.x@*"
}
}
85 changes: 61 additions & 24 deletions packages/runtime-bundler/src/__tests__/plugin.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import * as fs from 'node:fs/promises';

import type { OnLoadArgs, OnResolveArgs, PluginBuild } from 'esbuild';
import ts from 'typescript';
import { beforeEach, describe, expect, it, vi } from 'vitest';

import { assetPlugin, dtsPlugin } from '../plugin.js';
Expand All @@ -23,11 +22,6 @@ vi.mock('module', async (importOriginal) => ({

vi.mock('fs/promises', () => ({ readFile: vi.fn() }));

vi.mock('typescript', async (importOriginal) => {
const actual = (await importOriginal<{ default: typeof import('typescript') }>()).default;
return { default: { ...actual, transform: vi.fn(actual.transform) } };
});

/** Maps `'<packageJsonId>@<importerFilepath>'` to the resolved package.json path the mocked require would return. */
const packageJsonResolutions = new Map<string, string>();

Expand Down Expand Up @@ -163,10 +157,66 @@ describe('dtsPlugin', () => {
const { onLoadCallbacks } = setupPlugin(dtsPlugin({ configFilepath, entryPoints, outdir: '/out', packages }));
const result: any = await onLoadCallbacks[0]!({ path: importerFilepath } as OnLoadArgs);
expect(result.loader).toBe('copy');
expect(result.contents).toContain('from "../some-pkg/index.d.ts"');
expect(result.contents).toContain("from '../some-pkg/index.d.ts'");
expect(result.contents).toContain("from './local.js'");
});

it('should change nothing but the text inside each bare specifier, so quote style and comments survive', async () => {
packageJsonResolutions.set(`some-pkg/package.json@${importerFilepath}`, '/repo/node_modules/some-pkg/package.json');
const packages: ResolvedPackage[] = [
{
exports: { '.': { types: '/repo/node_modules/some-pkg/index.d.ts' } },
name: 'some-pkg',
packageJsonPath: '/repo/node_modules/some-pkg/package.json',
packageRoot: '/repo/node_modules/some-pkg'
}
];
const entryPoints: EntryPoint[] = [
{ in: '/repo/node_modules/some-pkg/index.d.ts', out: 'some-pkg/index.d' },
{ in: importerFilepath, out: 'app/index.d' }
];
vi.mocked(fs.readFile).mockResolvedValueOnce(
"import type { X } from \"some-pkg\";\n/** café: import { Y } from 'some-pkg'; */\nimport type { Z } from 'some-pkg';\n"
);
const { onLoadCallbacks } = setupPlugin(dtsPlugin({ configFilepath, entryPoints, outdir: '/out', packages }));
const result: any = await onLoadCallbacks[0]!({ path: importerFilepath } as OnLoadArgs);
expect(result.contents).toBe(
"import type { X } from \"../some-pkg/index.d.ts\";\n/** café: import { Y } from 'some-pkg'; */\nimport type { Z } from '../some-pkg/index.d.ts';\n"
);
});

it('should rewrite a bare import nested in a declare module block, not only top-level imports', async () => {
packageJsonResolutions.set(`some-pkg/package.json@${importerFilepath}`, '/repo/node_modules/some-pkg/package.json');
const packages: ResolvedPackage[] = [
{
exports: { '.': { types: '/repo/node_modules/some-pkg/index.d.ts' } },
name: 'some-pkg',
packageJsonPath: '/repo/node_modules/some-pkg/package.json',
packageRoot: '/repo/node_modules/some-pkg'
}
];
const entryPoints: EntryPoint[] = [
{ in: '/repo/node_modules/some-pkg/index.d.ts', out: 'some-pkg/index.d' },
{ in: importerFilepath, out: 'app/index.d' }
];
vi.mocked(fs.readFile).mockResolvedValueOnce(
"declare module 'augmented' {\n import type { X } from 'some-pkg';\n export const x: X;\n}\n"
);
const { onLoadCallbacks } = setupPlugin(dtsPlugin({ configFilepath, entryPoints, outdir: '/out', packages }));
const result: any = await onLoadCallbacks[0]!({ path: importerFilepath } as OnLoadArgs);
expect(result.contents).toContain("import type { X } from '../some-pkg/index.d.ts';");
});

it('should leave an export-from declaration untouched, so its package need not be included', async () => {
const sourceText = "export type { X } from 'unlisted-pkg';\n";
vi.mocked(fs.readFile).mockResolvedValueOnce(sourceText);
const { onLoadCallbacks } = setupPlugin(
dtsPlugin({ configFilepath, entryPoints: [], outdir: '/out', packages: [] })
);
const result: any = await onLoadCallbacks[0]!({ path: importerFilepath } as OnLoadArgs);
expect(result.contents).toBe(sourceText);
});

it('should resolve a subpath import of a scoped package against its own export key', async () => {
packageJsonResolutions.set(
`@scope/pkg/package.json@${importerFilepath}`,
Expand All @@ -187,7 +237,7 @@ describe('dtsPlugin', () => {
vi.mocked(fs.readFile).mockResolvedValueOnce("import type { X } from '@scope/pkg/sub';\nexport type { X };\n");
const { onLoadCallbacks } = setupPlugin(dtsPlugin({ configFilepath, entryPoints, outdir: '/out', packages }));
const result: any = await onLoadCallbacks[0]!({ path: importerFilepath } as OnLoadArgs);
expect(result.contents).toContain('from "../@scope/pkg/sub.d.ts"');
expect(result.contents).toContain("from '../@scope/pkg/sub.d.ts'");
});

it('should error when the matched package does not export the imported subpath', async () => {
Expand Down Expand Up @@ -223,29 +273,16 @@ describe('dtsPlugin', () => {
vi.mocked(fs.readFile).mockResolvedValueOnce("import type { X } from 'some-pkg';\nexport type { X };\n");
const { onLoadCallbacks } = setupPlugin(dtsPlugin({ configFilepath, entryPoints, outdir: '/out', packages }));
const result: any = await onLoadCallbacks[0]!({ path: importerFilepath } as OnLoadArgs);
expect(result.contents).toContain('from "../some-pkg/types.ts"');
});

it('should report a non-error thrown during the transform as an unknown error', async () => {
vi.mocked(ts.transform).mockImplementationOnce(() => {
// eslint-disable-next-line @typescript-eslint/only-throw-error -- the plugin must report a thrown value that is not an Error
throw 'not an error';
});
vi.mocked(fs.readFile).mockResolvedValueOnce('export type X = string;\n');
const { onLoadCallbacks } = setupPlugin(
dtsPlugin({ configFilepath, entryPoints: [], outdir: '/out', packages: [] })
);
const result: any = await onLoadCallbacks[0]!({ path: importerFilepath } as OnLoadArgs);
expect(result.errors[0]).toEqual({ location: { file: importerFilepath }, text: 'Unknown Error' });
expect(result.contents).toContain("from '../some-pkg/types.ts'");
});

it('should report a transform failure as a load error rather than throw', async () => {
it('should report a syntax error as a load error on the file rather than emit the declaration', async () => {
vi.mocked(fs.readFile).mockResolvedValueOnce('this is not valid { typescript syntax +++');
const { onLoadCallbacks } = setupPlugin(
dtsPlugin({ configFilepath, entryPoints: [], outdir: '/out', packages: [] })
);
const result: any = await onLoadCallbacks[0]!({ path: importerFilepath } as OnLoadArgs);
expect(Array.isArray(result.errors) || result.contents !== undefined).toBe(true);
expect(result).toEqual({ errors: [expect.objectContaining({ location: { file: importerFilepath } })] });
});
});

Expand Down
94 changes: 46 additions & 48 deletions packages/runtime-bundler/src/plugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import * as module from 'node:module';
import * as path from 'node:path';

import type { Plugin } from 'esbuild';
import ts from 'typescript';
import { parseSync, Visitor } from 'oxc-parser';
import type { ImportDeclaration } from 'oxc-parser';

import { validatePackageName } from './utils.js';

Expand All @@ -16,6 +17,8 @@ type DtsPluginOptions = {
packages: ResolvedPackage[];
};

type DependencyResolution = { issue: string; success: false } | { relativePath: string; success: true };

function parseImportPath(importPath: string) {
const segments = importPath.split('/');
const sliceIndex = importPath.startsWith('@') ? 2 : 1;
Expand All @@ -28,21 +31,23 @@ function parseImportPath(importPath: string) {

export function dtsPlugin(options: DtsPluginOptions): Plugin {
const require = module.createRequire(options.configFilepath);
const printer = ts.createPrinter();

const resolveDependency = (importPath: string, importerFilepath: string) => {
const resolveDependency = (importPath: string, importerFilepath: string): DependencyResolution => {
const { exportKey, packageId } = parseImportPath(importPath);
const validationResult = validatePackageName(packageId);
if (!validationResult.success) {
throw new Error(`Illegal import '${importPath}': ${validationResult.issue}`);
return { issue: `Illegal import '${importPath}': ${validationResult.issue}`, success: false };
}
const packageJsonId = `${packageId}/package.json`;

let packageJsonFilepath: string;
try {
packageJsonFilepath = require.resolve(packageJsonId, { paths: [importerFilepath] });
} catch {
throw new Error(`Failed to resolve '${packageId}' from '${importerFilepath}' via '${packageJsonId}'`);
return {
issue: `Failed to resolve '${packageId}' from '${importerFilepath}' via '${packageJsonId}'`,
success: false
};
}
for (const pkg of options.packages) {
if (pkg.packageJsonPath !== packageJsonFilepath) {
Expand All @@ -51,47 +56,29 @@ export function dtsPlugin(options: DtsPluginOptions): Plugin {
const packageExport = pkg.exports[exportKey];
if (packageExport) {
if ('copy' in packageExport) {
throw new Error(`Found dependency '${importPath}' but export '${exportKey}' is a static asset`);
return {
issue: `Found dependency '${importPath}' but export '${exportKey}' is a static asset`,
success: false
};
}
const types = packageExport.types;
if (types) {
const outfile = options.entryPoints.find((item) => item.in === types)?.out;
if (!outfile) {
throw new Error(`Could not find dependency with absolute input path '${types}'`);
return { issue: `Could not find dependency with absolute input path '${types}'`, success: false };
}
const outpath = path.resolve(options.outdir, outfile + (outfile.endsWith('.d') ? '.ts' : ''));
const importerPath = options.entryPoints.find((item) => item.in === importerFilepath)!.out;
const importer = path.resolve(options.outdir, importerPath + (importerPath.endsWith('.d') ? '.ts' : ''));
return path.relative(path.dirname(importer), outpath);
return { relativePath: path.relative(path.dirname(importer), outpath), success: true };
}
throw new Error(`Found dependency '${importPath}' but export '${exportKey}' does not specify types`);
return {
issue: `Found dependency '${importPath}' but export '${exportKey}' does not specify types`,
success: false
};
}
}
throw new Error(`Could not find dependency '${importPath}' imported by '${importerFilepath}'`);
};

const factory = <T extends ts.SourceFile>(context: ts.TransformationContext) => {
return (rootNode: T): T => {
const visit = (node: ts.Node) => {
if (ts.isImportDeclaration(node)) {
const importPath = (node.moduleSpecifier as ts.StringLiteral).text;
const isRelativeImport = importPath.startsWith('./');
if (isRelativeImport) {
return ts.visitEachChild(node, visit, context);
}
const resolvedImportPath = resolveDependency(importPath, rootNode.fileName);
return ts.factory.updateImportDeclaration(
node,
node.modifiers,
node.importClause,
ts.factory.createStringLiteral(resolvedImportPath),
node.attributes
);
}
return ts.visitEachChild(node, visit, context);
};
return ts.visitNode(rootNode, visit) as T;
};
return { issue: `Could not find dependency '${importPath}' imported by '${importerFilepath}'`, success: false };
};

return {
Expand All @@ -104,23 +91,34 @@ export function dtsPlugin(options: DtsPluginOptions): Plugin {
}));
build.onLoad({ filter: /.*/, namespace }, async (args) => {
const sourceText = await fs.readFile(args.path, 'utf-8');
const sourceFile = ts.createSourceFile(args.path, sourceText, ts.ScriptTarget.ESNext, true, ts.ScriptKind.TS);
let transformationResult: ts.TransformationResult<ts.SourceFile>;
try {
transformationResult = ts.transform(sourceFile, [factory]);
} catch (err) {
const parseResult = parseSync(args.path, sourceText, { lang: 'dts', sourceType: 'module' });
if (parseResult.errors.length) {
return {
errors: [
{
location: {
file: args.path
},
text: err instanceof Error ? err.message : 'Unknown Error'
}
]
errors: parseResult.errors.map((error) => ({ location: { file: args.path }, text: error.message }))
};
}
const contents = printer.printFile(transformationResult.transformed[0]!);
// every import declaration, including those nested in `declare module` blocks, in source order
const importDeclarations: ImportDeclaration[] = [];
new Visitor({
ImportDeclaration(node) {
importDeclarations.push(node);
}
}).visit(parseResult.program);
let contents = '';
let copiedUpTo = 0;
for (const { source } of importDeclarations) {
if (source.value.startsWith('./')) {
continue;
}
const resolution = resolveDependency(source.value, args.path);
if (!resolution.success) {
return { errors: [{ location: { file: args.path }, text: resolution.issue }] };
}
// the specifier's span includes its quotes; splicing between them keeps the source's quote style
contents += sourceText.slice(copiedUpTo, source.start + 1) + resolution.relativePath;
copiedUpTo = source.end - 1;
}
contents += sourceText.slice(copiedUpTo);
return {
contents,
loader: 'copy'
Expand Down
9 changes: 9 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pnpm-workspace.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ catalog:
motion: '13.4.4'
neverthrow: ^8.2.0
nodemailer: ^10.0.11
oxc-parser: '^0.150.0'
conventional-commits-parser: '^7.1.2'
prisma: '^6.9.0'
react-dropzone: ^20.1.2
Expand Down
1 change: 1 addition & 0 deletions testing/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"@opendatacapture/playground-url": "workspace:*",
"@opendatacapture/schemas": "workspace:*",
"@playwright/test": "^1.63.0",
"oxc-parser": "catalog:",
"type-fest": "workspace:type-fest__4.x@*"
}
}
Loading
Loading