File tree Expand file tree Collapse file tree 12 files changed +182
-41
lines changed Expand file tree Collapse file tree 12 files changed +182
-41
lines changed Original file line number Diff line number Diff line change @@ -34,6 +34,21 @@ This allows the installed Amaro to override the Amaro version used by Node.js.
3434node --experimental-strip-types --import=" amaro/register" script.ts
3535```
3636
37+ Or with the alias:
38+
39+ ``` bash
40+ node --experimental-strip-types --import=" amaro/strip" script.ts
41+ ```
42+
43+ Enabling TypeScript feature transformation:
44+
45+ ``` bash
46+ node --experimental-transform-types --import=" amaro/transform" script.ts
47+ ```
48+
49+ > Note that the "amaro/transform" loader should be used with ` --experimental-transform-types ` flag, or
50+ > at least with ` --enable-source-maps ` flag, to preserve the original source maps.
51+
3752### How to update SWC
3853
3954To update the SWC version, run:
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 1+ import { build } from "esbuild" ;
2+ import { copy } from "esbuild-plugin-copy" ;
3+
4+ const copyPlugin = copy ( {
5+ assets : [
6+ {
7+ from : [ "./src/register/register-strip.mjs" ] ,
8+ to : [ "." ] ,
9+ } ,
10+ {
11+ from : [ "./src/register/register-transform.mjs" ] ,
12+ to : [ "." ] ,
13+ } ,
14+ {
15+ from : [ "./lib/LICENSE" , "./lib/package.json" ] ,
16+ to : [ "." ] ,
17+ } ,
18+ ] ,
19+ } ) ;
20+
21+ await build ( {
22+ entryPoints : [ "src/index.ts" ] ,
23+ bundle : true ,
24+ platform : "node" ,
25+ target : "node22" ,
26+ outfile : "dist/index.js" ,
27+ plugins : [ copyPlugin ] ,
28+ } ) ;
29+
30+ await build ( {
31+ entryPoints : [ "src/strip-loader.ts" ] ,
32+ bundle : false ,
33+ outfile : "dist/strip-loader.js" ,
34+ platform : "node" ,
35+ target : "node22" ,
36+ } ) ;
37+
38+ await build ( {
39+ entryPoints : [ "src/transform-loader.ts" ] ,
40+ bundle : false ,
41+ outfile : "dist/transform-loader.js" ,
42+ platform : "node" ,
43+ target : "node22" ,
44+ } ) ;
Original file line number Diff line number Diff line change 2121 "ci:fix" : " biome check --write" ,
2222 "prepack" : " npm run build" ,
2323 "postpack" : " npm run clean" ,
24- "build" : " node esbuild.config.js " ,
24+ "build" : " node esbuild.config.mjs " ,
2525 "typecheck" : " tsc --noEmit" ,
2626 "test" : " node --test --experimental-test-snapshots \" **/*.test.js\" " ,
2727 "test:regenerate" : " node --test --experimental-test-snapshots --test-update-snapshots \" **/*.test.js\" "
2828 },
2929 "devDependencies" : {
3030 "@biomejs/biome" : " 1.8.3" ,
31- "@types/node" : " ^20.14.11 " ,
31+ "@types/node" : " ^22.0.0 " ,
3232 "esbuild" : " ^0.23.0" ,
3333 "esbuild-plugin-copy" : " ^2.1.1" ,
3434 "rimraf" : " ^6.0.1" ,
3535 "typescript" : " ^5.5.3"
3636 },
3737 "exports" : {
3838 "." : " ./dist/index.js" ,
39- "./register" : " ./dist/register.mjs"
39+ "./register" : " ./dist/register-strip.mjs" ,
40+ "./strip" : " ./dist/register-strip.mjs" ,
41+ "./transform" : " ./dist/register-transform.mjs"
4042 },
41- "files" : [" dist" , " LICENSE.md" ]
43+ "files" : [" dist" , " LICENSE.md" ],
44+ "engines" : {
45+ "node" : " >=22"
46+ }
4247}
Original file line number Diff line number Diff line change 11export { transformSync } from "./transform.ts" ;
2- export { load } from "./loader.ts" ;
Original file line number Diff line number Diff line change 1+ import { register } from "node:module" ;
2+
3+ register ( "./strip-loader.js" , import . meta. url ) ;
Original file line number Diff line number Diff line change 1+ import { register } from "node:module" ;
2+ import { emitWarning , env , execArgv } from "node:process" ;
3+
4+ const hasSourceMaps =
5+ execArgv . includes ( "--enable-source-maps" ) ||
6+ env . NODE_OPTIONS ?. includes ( "--enable-source-maps" ) ;
7+
8+ if ( ! hasSourceMaps ) {
9+ emitWarning ( "Source maps are disabled, stack traces will not accurate" ) ;
10+ }
11+
12+ register ( "./transform-loader.js" , import . meta. url ) ;
Load Diff This file was deleted.
Original file line number Diff line number Diff line change 11import type { LoadFnOutput , LoadHookContext } from "node:module" ;
22import type { Options } from "../lib/wasm" ;
3- import { transformSync } from "./index.ts " ;
3+ import { transformSync } from "./index.js " ;
44
55type NextLoad = (
66 url : string ,
@@ -20,14 +20,16 @@ export async function load(
2020 ...context ,
2121 format : "module" ,
2222 } ) ;
23- if ( source == null )
24- throw new Error ( "Source code cannot be null or undefined" ) ;
25- const { code } = transformSync ( source . toString ( ) , {
23+ // biome-ignore lint/style/noNonNullAssertion: If module exists, it will have a source
24+ const { code } = transformSync ( source ! . toString ( ) , {
2625 mode : "strip-only" ,
2726 } as Options ) ;
2827 return {
2928 format : format . replace ( "-typescript" , "" ) ,
30- source : code ,
29+ // Source map is not necessary in strip-only mode. However, to map the source
30+ // file in debuggers to the original TypeScript source, add a sourceURL magic
31+ // comment to hint that it is a generated source.
32+ source : `${ code } \n\n//# sourceURL=${ url } ` ,
3133 } ;
3234 }
3335 return nextLoad ( url , context ) ;
Original file line number Diff line number Diff line change 1+ import type { LoadFnOutput , LoadHookContext } from "node:module" ;
2+ import type { Options } from "../lib/wasm" ;
3+ import { transformSync } from "./index.js" ;
4+
5+ type NextLoad = (
6+ url : string ,
7+ context ?: LoadHookContext ,
8+ ) => LoadFnOutput | Promise < LoadFnOutput > ;
9+
10+ export async function load (
11+ url : string ,
12+ context : LoadHookContext ,
13+ nextLoad : NextLoad ,
14+ ) {
15+ const { format } = context ;
16+ if ( format . endsWith ( "-typescript" ) ) {
17+ // Use format 'module' so it returns the source as-is, without stripping the types.
18+ // Format 'commonjs' would not return the source for historical reasons.
19+ const { source } = await nextLoad ( url , {
20+ ...context ,
21+ format : "module" ,
22+ } ) ;
23+
24+ // biome-ignore lint/style/noNonNullAssertion: If module exists, it will have a source
25+ const { code, map } = transformSync ( source ! . toString ( ) , {
26+ mode : "transform" ,
27+ sourceMap : true ,
28+ filename : url ,
29+ } as Options ) ;
30+
31+ let output = code ;
32+
33+ if ( map ) {
34+ const base64SourceMap = Buffer . from ( map ) . toString ( "base64" ) ;
35+ output = `${ code } \n\n//# sourceMappingURL=data:application/json;base64,${ base64SourceMap } ` ;
36+ }
37+
38+ return {
39+ format : format . replace ( "-typescript" , "" ) ,
40+ source : output ,
41+ } ;
42+ }
43+ return nextLoad ( url , context ) ;
44+ }
You can’t perform that action at this time.
0 commit comments