diff --git a/esbuild.config.mjs b/esbuild.config.mjs index 1b1b62953..bbebf4aca 100644 --- a/esbuild.config.mjs +++ b/esbuild.config.mjs @@ -1,14 +1,17 @@ import { build } from "esbuild"; import { copy } from "esbuild-plugin-copy"; -await build({ - entryPoints: ["src/*.ts"], - bundle: false, - outdir: "dist", - outbase: "src", +const common = { platform: "node", format: "cjs", target: "node22", +}; + +await build({ + ...common, + entryPoints: ["src/internal.ts"], + bundle: true, + outfile: "dist/internal.js", plugins: [ copy({ assets: [ @@ -24,3 +27,17 @@ await build({ }), ], }); + +// Everything else is a thin wrapper that requires `./internal.js`. +await build({ + ...common, + entryPoints: [ + "src/index.ts", + "src/errors.ts", + "src/strip-loader.ts", + "src/transform-loader.ts", + ], + bundle: false, + outdir: "dist", + outbase: "src", +}); diff --git a/src/index.ts b/src/index.ts index cec305e59..3d9d18d00 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1 +1,2 @@ -export { transformSync } from "./transform.js"; +// Public API of the `amaro` package (`exports["."]`). +export { transformSync } from "./internal.js"; diff --git a/src/internal.ts b/src/internal.ts new file mode 100644 index 000000000..c866253e8 --- /dev/null +++ b/src/internal.ts @@ -0,0 +1,51 @@ +import type { + ModuleSyntaxTransformOutput, + Options, + TransformOutput, +} from "../lib/wasm"; +import swc from "../lib/wasm.js"; + +const DEFAULT_OPTIONS = { + mode: "strip-only", + deprecatedTsModuleAsError: true, + // default transform will only work when mode is "transform" + transform: { + verbatimModuleSyntax: true, + nativeClassProperties: true, + noEmptyExport: true, + importNotUsedAsValues: "preserve", + }, +} as Options; + +export function transformSync( + source: string, + options?: Options, +): TransformOutput { + // Ensure that the source code is a string + const input = `${source ?? ""}`; + return swc.transformSync(input, { + ...DEFAULT_OPTIONS, + ...options, + }); +} + +export function transformModuleSyntax( + source: string, +): ModuleSyntaxTransformOutput { + return swc.transformModuleSyntax(`${source ?? ""}`); +} + +export function getFirstExpression( + source: string, + startColumn: number, +): string { + return swc.getFirstExpression(`${source ?? ""}`, startColumn); +} + +export function isValidSyntax(source: string): boolean { + return swc.isValidSyntax(`${source ?? ""}`); +} + +export function isRecoverableError(source: string): boolean { + return swc.isRecoverableError(`${source ?? ""}`); +} diff --git a/src/nodejs.ts b/src/nodejs.ts deleted file mode 100644 index 90ce82e0a..000000000 --- a/src/nodejs.ts +++ /dev/null @@ -1,23 +0,0 @@ -import type { ModuleSyntaxTransformOutput } from "../lib/wasm"; -import swc from "../lib/wasm.js"; - -export function transformModuleSyntax( - source: string, -): ModuleSyntaxTransformOutput { - return swc.transformModuleSyntax(`${source ?? ""}`); -} - -export function getFirstExpression( - source: string, - startColumn: number, -): string { - return swc.getFirstExpression(`${source ?? ""}`, startColumn); -} - -export function isValidSyntax(source: string): boolean { - return swc.isValidSyntax(`${source ?? ""}`); -} - -export function isRecoverableError(source: string): boolean { - return swc.isRecoverableError(`${source ?? ""}`); -} diff --git a/src/transform.ts b/src/transform.ts deleted file mode 100644 index 877831a61..000000000 --- a/src/transform.ts +++ /dev/null @@ -1,26 +0,0 @@ -import type { Options, TransformOutput } from "../lib/wasm"; -import swc from "../lib/wasm.js"; - -const DEFAULT_OPTIONS = { - mode: "strip-only", - deprecatedTsModuleAsError: true, - // default transform will only work when mode is "transform" - transform: { - verbatimModuleSyntax: true, - nativeClassProperties: true, - noEmptyExport: true, - importNotUsedAsValues: "preserve", - }, -} as Options; - -export function transformSync( - source: string, - options?: Options, -): TransformOutput { - // Ensure that the source code is a string - const input = `${source ?? ""}`; - return swc.transformSync(input, { - ...DEFAULT_OPTIONS, - ...options, - }); -} diff --git a/test/nodejs.test.js b/test/nodejs.test.js index 0df5cb0bd..31cb5c133 100644 --- a/test/nodejs.test.js +++ b/test/nodejs.test.js @@ -5,7 +5,7 @@ const { isRecoverableError, isValidSyntax, transformModuleSyntax, -} = require("../dist/nodejs.js"); +} = require("../dist/internal.js"); test("should transform module syntax for the Node.js REPL", () => { const result = transformModuleSyntax(