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
27 changes: 22 additions & 5 deletions esbuild.config.mjs
Original file line number Diff line number Diff line change
@@ -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: [
Expand All @@ -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",
});
3 changes: 2 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { transformSync } from "./transform.js";
// Public API of the `amaro` package (`exports["."]`).
export { transformSync } from "./internal.js";
51 changes: 51 additions & 0 deletions src/internal.ts
Original file line number Diff line number Diff line change
@@ -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 ?? ""}`);
}
23 changes: 0 additions & 23 deletions src/nodejs.ts

This file was deleted.

26 changes: 0 additions & 26 deletions src/transform.ts

This file was deleted.

2 changes: 1 addition & 1 deletion test/nodejs.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Loading