Skip to content

Commit 6558cbe

Browse files
authored
Update bundled transpileModule implementation to match latest TS codebase (#1973)
* empty initial commit * Update bundled `transpileModule` implementation to match latest TS 5.0 codebase * Add test; move transpile-only tests to their own file
1 parent fd43821 commit 6558cbe

File tree

4 files changed

+81
-28
lines changed

4 files changed

+81
-28
lines changed

src/test/helpers/version-checks.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,8 @@ export const tsSupportsAllowImportingTsExtensions = semver.gte(
4040
);
4141
// TS 5.0 adds ability for tsconfig to `"extends": []` an array of configs
4242
export const tsSupportsExtendsArray = semver.gte(ts.version, '4.999.999');
43+
// TS 5.0 adds verbatimModuleSyntax
44+
export const tsSupportsVerbatimModuleSyntax = semver.gte(ts.version, '5.0.0');
4345
// Relevant when @tsconfig/bases refers to es2021 and we run tests against
4446
// old TS versions.
4547
export const tsSupportsEs2021 = semver.gte(ts.version, '4.3.0');

src/test/index.spec.ts

Lines changed: 0 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -305,28 +305,6 @@ test.suite('ts-node', (test) => {
305305
);
306306
});
307307

308-
test('should support transpile only mode', async () => {
309-
const r = await exec(
310-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only -pe "x"`
311-
);
312-
if (r.err === null) {
313-
throw new Error('Command was expected to fail, but it succeeded.');
314-
}
315-
316-
expect(r.err.message).toMatch('ReferenceError: x is not defined');
317-
});
318-
319-
test('should throw error even in transpileOnly mode', async () => {
320-
const r = await exec(
321-
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only -pe "console."`
322-
);
323-
if (r.err === null) {
324-
throw new Error('Command was expected to fail, but it succeeded.');
325-
}
326-
327-
expect(r.err.message).toMatch('error TS1003: Identifier expected');
328-
});
329-
330308
for (const flavor of [
331309
'--transpiler ts-node/transpilers/swc transpile-only-swc',
332310
'--transpiler ts-node/transpilers/swc-experimental transpile-only-swc',

src/test/transpile-only.spec.ts

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
import { createExec } from './exec-helpers';
2+
import { ctxTsNode, tsSupportsVerbatimModuleSyntax } from './helpers';
3+
import { CMD_TS_NODE_WITH_PROJECT_FLAG } from './helpers/command-lines';
4+
import { TEST_DIR } from './helpers/paths';
5+
import { expect, context } from './testlib';
6+
7+
const test = context(ctxTsNode);
8+
9+
const exec = createExec({
10+
cwd: TEST_DIR,
11+
});
12+
13+
test('should support transpile only mode', async () => {
14+
const r = await exec(
15+
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only -pe "x"`
16+
);
17+
if (r.err === null) {
18+
throw new Error('Command was expected to fail, but it succeeded.');
19+
}
20+
21+
expect(r.err.message).toMatch('ReferenceError: x is not defined');
22+
});
23+
24+
test('should throw error even in transpileOnly mode', async () => {
25+
const r = await exec(
26+
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --transpile-only -pe "console."`
27+
);
28+
if (r.err === null) {
29+
throw new Error('Command was expected to fail, but it succeeded.');
30+
}
31+
32+
expect(r.err.message).toMatch('error TS1003: Identifier expected');
33+
});
34+
35+
test.suite(
36+
'verbatimModuleSyntax w/transpileOnly should not raise configuration diagnostic',
37+
(test) => {
38+
test.if(tsSupportsVerbatimModuleSyntax);
39+
test('test', async (t) => {
40+
// Mixing verbatimModuleSyntax w/transpileOnly
41+
// https://github.com/TypeStrong/ts-node/issues/1971
42+
// We should *not* get:
43+
// "error TS5104: Option 'isolatedModules' is redundant and cannot be specified with option 'verbatimModuleSyntax'."
44+
const service = t.context.tsNodeUnderTest.create({
45+
transpileOnly: true,
46+
compilerOptions: { verbatimModuleSyntax: true },
47+
});
48+
service.compile('const foo: string = 123', 'module.ts');
49+
});
50+
}
51+
);

src/ts-transpile-module.ts

Lines changed: 28 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
// Derived from
2+
// https://github.com/microsoft/TypeScript/blob/ae1b3db8ceaae7e93bddffa1eed26309068249d7/src/services/transpile.ts
3+
14
import type {
25
CompilerHost,
36
CompilerOptions,
@@ -8,6 +11,12 @@ import type {
811
} from 'typescript';
912
import type { TSCommon } from './ts-compiler-types';
1013

14+
const optionsRedundantWithVerbatimModuleSyntax = new Set([
15+
'isolatedModules',
16+
'preserveValueImports',
17+
'importsNotUsedAsValues',
18+
]);
19+
1120
/** @internal */
1221
export function createTsTranspileModule(
1322
ts: TSCommon,
@@ -29,7 +38,6 @@ export function createTsTranspileModule(
2938
Debug,
3039
toPath,
3140
getSetExternalModuleIndicator,
32-
getEntries,
3341
addRange,
3442
hasProperty,
3543
getEmitScriptTarget,
@@ -54,6 +62,14 @@ export function createTsTranspileModule(
5462
}
5563

5664
for (const option of transpileOptionValueCompilerOptions) {
65+
// Do not set redundant config options if `verbatimModuleSyntax` was supplied.
66+
if (
67+
options.verbatimModuleSyntax &&
68+
optionsRedundantWithVerbatimModuleSyntax.has(option.name)
69+
) {
70+
continue;
71+
}
72+
5773
options[option.name] = option.transpileOptionValue;
5874
}
5975

@@ -109,12 +125,18 @@ export function createTsTranspileModule(
109125

110126
return transpileModule;
111127

128+
/*
129+
* This function will compile source text from 'input' argument using specified compiler options.
130+
* If not options are provided - it will use a set of default compiler options.
131+
* Extra compiler options that will unconditionally be used by this function are:
132+
* - isolatedModules = true
133+
* - allowNonTsExtensions = true
134+
* - noLib = true
135+
* - noResolve = true
136+
*/
112137
function transpileModule(
113138
input: string,
114-
transpileOptions2: Pick<
115-
TranspileOptions,
116-
'fileName' | 'moduleName' | 'renamedDependencies'
117-
>,
139+
transpileOptions2: TranspileOptions,
118140
packageJsonType: 'module' | 'commonjs' = 'commonjs'
119141
): TranspileOutput {
120142
// if jsx is specified then treat file as .tsx
@@ -142,7 +164,7 @@ export function createTsTranspileModule(
142164

143165
if (transpileOptions2.renamedDependencies) {
144166
(sourceFile as any).renamedDependencies = new Map(
145-
getEntries(transpileOptions2.renamedDependencies)
167+
Object.entries(transpileOptions2.renamedDependencies)
146168
);
147169
}
148170

0 commit comments

Comments
 (0)