Skip to content

Commit a23bc47

Browse files
committed
Skip source transform for builtin and commonjs
1 parent 544584a commit a23bc47

File tree

1 file changed

+28
-24
lines changed

1 file changed

+28
-24
lines changed

src/esm.ts

Lines changed: 28 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -106,40 +106,44 @@ export function registerAndCreateEsmHooks(opts?: RegisterOptions) {
106106
url: string,
107107
context: { format: Format | null | undefined },
108108
defaultLoad: typeof load
109-
): Promise<{ format: Format; source: string | Buffer }> {
109+
): Promise<{ format: Format; source: string | Buffer | undefined }> {
110110
// If we get a format hint from resolve() on the context then use it
111111
// otherwise call the old getFormat() hook using node's old built-in defaultGetFormat() that ships with ts-node
112112
const format =
113113
context.format ??
114114
(await getFormat(url, context, defaultGetFormat)).format;
115115

116-
// Call the new defaultLoad() to get the source
117-
const { source: rawSource } = await defaultLoad(
118-
url,
119-
{ format },
120-
defaultLoad
121-
);
116+
let source = undefined;
117+
if (format !== 'builtin' && format !== 'commonjs') {
118+
// Call the new defaultLoad() to get the source
119+
const { source: rawSource } = await defaultLoad(
120+
url,
121+
{ format },
122+
defaultLoad
123+
);
124+
125+
if (rawSource === undefined || rawSource === null) {
126+
throw new Error(
127+
`Failed to load raw source: Format was '${format}' and url was '${url}''.`
128+
);
129+
}
122130

123-
if (rawSource === undefined || rawSource === null) {
124-
throw new Error(
125-
`Failed to load raw source: Format was '${format}' and url was '${url}''.`
131+
// Emulate node's built-in old defaultTransformSource() so we can re-use the old transformSource() hook
132+
const defaultTransformSource: typeof transformSource = async (
133+
source,
134+
_context,
135+
_defaultTransformSource
136+
) => ({ source });
137+
138+
// Call the old hook
139+
const { source: transformedSource } = await transformSource(
140+
rawSource,
141+
{ url, format },
142+
defaultTransformSource
126143
);
144+
source = transformedSource;
127145
}
128146

129-
// Emulate node's built-in old defaultTransformSource() so we can re-use the old transformSource() hook
130-
const defaultTransformSource: typeof transformSource = async (
131-
source,
132-
_context,
133-
_defaultTransformSource
134-
) => ({ source });
135-
136-
// Call the old hook
137-
const { source } = await transformSource(
138-
rawSource,
139-
{ url, format },
140-
defaultTransformSource
141-
);
142-
143147
return { format, source };
144148
}
145149

0 commit comments

Comments
 (0)