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
9 changes: 6 additions & 3 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1307,13 +1307,16 @@ export function create(rawOptions: CreateOptions = {}): Service {
config.options.module === ts.ModuleKind.CommonJS
? undefined
: createTranspileOnlyGetOutputFunction(ts.ModuleKind.CommonJS);
// [MUST_UPDATE_FOR_NEW_MODULEKIND]
const getOutputForceESM =
config.options.module === ts.ModuleKind.ES2015 ||
config.options.module === ts.ModuleKind.ES2020 ||
(ts.ModuleKind.ES2020 && config.options.module === ts.ModuleKind.ES2020) ||
(ts.ModuleKind.ES2022 && config.options.module === ts.ModuleKind.ES2022) ||
config.options.module === ts.ModuleKind.ESNext
? undefined
: createTranspileOnlyGetOutputFunction(
ts.ModuleKind.ES2020 || ts.ModuleKind.ES2015
: // [MUST_UPDATE_FOR_NEW_MODULEKIND]
createTranspileOnlyGetOutputFunction(
ts.ModuleKind.ES2022 || ts.ModuleKind.ES2020 || ts.ModuleKind.ES2015
);
const getOutputTranspileOnly = createTranspileOnlyGetOutputFunction();

Expand Down
35 changes: 35 additions & 0 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1267,3 +1267,38 @@ test('Falls back to transpileOnly when ts compiler returns emitSkipped', async (
expect(err).toBe(null);
expect(stdout).toBe('foo\n');
});

test('Detect when typescript adds new ModuleKind values; flag as a failure so we can update our code flagged [MUST_UPDATE_FOR_NEW_MODULEKIND]', async () => {
// We have marked a few places in our code with MUST_UPDATE_FOR_NEW_MODULEKIND to make it easier to update them when TS adds new ModuleKinds
const foundKeys: string[] = [];
function check(value: number, name: string, required: boolean) {
if (required) expect(ts.ModuleKind[name]).toBe(value);
if (ts.ModuleKind[value] === undefined) {
expect(ts.ModuleKind[name]).toBeUndefined();
} else {
expect(ts.ModuleKind[value]).toBe(name);
foundKeys.push(name, `${value}`);
}
}
check(0, 'None', true);
check(1, 'CommonJS', true);
check(2, 'AMD', true);
check(3, 'UMD', true);
check(4, 'System', true);
check(5, 'ES2015', true);
try {
check(6, 'ES2020', false);
check(99, 'ESNext', true);
} catch {
// the value changed: is `99` now, but was `6` in TS 2.7
check(6, 'ESNext', true);
expect(ts.ModuleKind[99]).toBeUndefined();
}
check(7, 'ES2022', false);
check(100, 'Node12', false);
check(199, 'NodeNext', false);
const actualKeys = Object.keys(ts.ModuleKind);
actualKeys.sort();
foundKeys.sort();
expect(actualKeys).toEqual(foundKeys);
});
2 changes: 1 addition & 1 deletion src/transpilers/swc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
}
swcTarget = swcTargets[swcTargetIndex];
const keepClassNames = target! >= /* ts.ScriptTarget.ES2016 */ 3;
// swc only supports these 4x module options
// swc only supports these 4x module options [MUST_UPDATE_FOR_NEW_MODULEKIND]
const moduleType =
module === ModuleKind.CommonJS
? 'commonjs'
Expand Down