Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swc: Support paths from tsconfig #1793

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
28 changes: 26 additions & 2 deletions src/test/transpilers.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,23 @@
// Should consolidate them here.

import { context } from './testlib';
import { ctxTsNode, testsDirRequire } from './helpers';
import {
CMD_TS_NODE_WITHOUT_PROJECT_FLAG,
ctxTsNode,
TEST_DIR,
testsDirRequire,
} from './helpers';
import * as expect from 'expect';
import { createExec } from './exec-helpers';

const exec = createExec({
cwd: TEST_DIR,
});

const test = context(ctxTsNode);

test.suite('swc', (test) => {
test('verify that TS->SWC target mappings suppport all possible values from both TS and SWC', async (t) => {
test('verify that TS->SWC target mappings support all possible values from both TS and SWC', async (t) => {
const swcTranspiler = testsDirRequire(
'ts-node/transpilers/swc-experimental'
) as typeof import('../transpilers/swc');
Expand Down Expand Up @@ -44,4 +54,18 @@ test.suite('swc', (test) => {
expect([...swcTranspiler.targetMapping.values()]).toContain(target);
}
});

test('verify paths are mapped correctly', async (t) => {
const { err, stdout } = await exec(
`${CMD_TS_NODE_WITHOUT_PROJECT_FLAG} swc-with-paths`,
{
env: {
...process.env,
NODE_OPTIONS: `${process.env.NODE_OPTIONS || ''}}`,
},
}
);
expect(err).toBe(null);
expect(stdout).toMatch('Hello, world!\n');
});
});
16 changes: 16 additions & 0 deletions src/transpilers/swc.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import * as path from 'path';
import type * as ts from 'typescript';
import type * as swcWasm from '@swc/wasm';
import type * as swcTypes from '@swc/core';
Expand Down Expand Up @@ -60,6 +61,8 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
strict,
alwaysStrict,
noImplicitUseStrict,
baseUrl,
paths,
} = compilerOptions;
const nonTsxOptions = createSwcOptions(false);
const tsxOptions = createSwcOptions(true);
Expand Down Expand Up @@ -110,6 +113,16 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
noImplicitUseStrict === true
? false
: true;

const absolutePaths = Object.fromEntries(
Object.entries(paths || {})
.filter(([key, values]) => values.length > 0)
.map(([key, values]) => [
key,
values.map((value) => path.resolve(baseUrl || './', value)),
])
) as { [from: string]: [string] };

return {
sourceMaps: sourceMap,
// isModule: true,
Expand Down Expand Up @@ -144,6 +157,9 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
} as swcTypes.ReactConfig,
},
keepClassNames,
baseUrl,
paths:
Object.keys(absolutePaths).length > 0 ? absolutePaths : undefined,
} as swcTypes.JscConfig,
};
}
Expand Down
1 change: 1 addition & 0 deletions tests/swc-with-paths/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import '@parent-dir/hello-world';
12 changes: 12 additions & 0 deletions tests/swc-with-paths/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
{
"ts-node": {
"swc": true
},
"compilerOptions": {
"module": "CommonJS",
"baseUrl": "./",
"paths": {
"@parent-dir/*": ["../*"]
}
}
}