Skip to content

Commit

Permalink
Set swc option keepClassNames whenever possible (#1344)
Browse files Browse the repository at this point in the history
* set swc option keepClassNames whenever possible

* increase minimum swc version to 1.2.50 to support keepClassNames

* update package-lock.json

* add test

* lint-fix

* Fix tests so they actually fail without the bugfix

* remove log statement from tests

* compat with ts 2.7
  • Loading branch information
cspotcode committed May 25, 2021
1 parent 34ce778 commit aba4ae6
Show file tree
Hide file tree
Showing 8 changed files with 6,550 additions and 73 deletions.
6,580 changes: 6,520 additions & 60 deletions package-lock.json

Large diffs are not rendered by default.

8 changes: 4 additions & 4 deletions package.json
Expand Up @@ -112,8 +112,8 @@
},
"devDependencies": {
"@microsoft/api-extractor": "^7.15.2",
"@swc/core": ">=1.2.45",
"@swc/wasm": ">=1.2.45",
"@swc/core": ">=1.2.50",
"@swc/wasm": ">=1.2.50",
"@types/chai": "^4.0.4",
"@types/diff": "^4.0.2",
"@types/lodash": "^4.14.151",
Expand Down Expand Up @@ -143,8 +143,8 @@
"util.promisify": "^1.0.1"
},
"peerDependencies": {
"@swc/core": ">=1.2.45",
"@swc/wasm": ">=1.2.45",
"@swc/core": ">=1.2.50",
"@swc/wasm": ">=1.2.50",
"@types/node": "*",
"typescript": ">=2.7"
},
Expand Down
4 changes: 2 additions & 2 deletions src/test/index.spec.ts
Expand Up @@ -345,15 +345,15 @@ test.suite('ts-node', (test) => {
`${cmdNoProject} --transpiler ts-node/transpilers/swc-experimental transpile-only-swc`
);
expect(err).to.equal(null);
expect(stdout).to.contain('hello world');
expect(stdout).to.contain('Hello World!');
});

test('should support third-party transpilers via tsconfig', async () => {
const { err, stdout } = await exec(
`${cmdNoProject} transpile-only-swc-via-tsconfig`
);
expect(err).to.equal(null);
expect(stdout).to.contain('hello world');
expect(stdout).to.contain('Hello World!');
});

test('should pipe into `ts-node` and evaluate', async () => {
Expand Down
9 changes: 6 additions & 3 deletions src/transpilers/swc.ts
Expand Up @@ -55,6 +55,8 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
const nonTsxOptions = createSwcOptions(false);
const tsxOptions = createSwcOptions(true);
function createSwcOptions(isTsx: boolean): swcTypes.Options {
const swcTarget = targetMapping.get(target!) ?? 'es3';
const keepClassNames = target! >= /* ts.ScriptTarget.ES2016 */ 3;
return {
sourceMaps: sourceMap,
// isModule: true,
Expand All @@ -71,7 +73,7 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
decorators: experimentalDecorators,
dynamicImport: true,
},
target: targetMapping.get(target!) ?? 'es3',
target: swcTarget,
transform: {
decoratorMetadata: emitDecoratorMetadata,
legacyDecorator: true,
Expand All @@ -81,9 +83,10 @@ export function create(createOptions: SwcTranspilerOptions): Transpiler {
useBuiltins: false,
pragma: jsxFactory!,
pragmaFrag: jsxFragmentFactory!,
},
} as swcTypes.ReactConfig,
},
},
keepClassNames,
} as swcTypes.JscConfig,
};
}

Expand Down
7 changes: 6 additions & 1 deletion tests/transpile-only-swc-via-tsconfig/index.ts
@@ -1,4 +1,9 @@
// Test for #1343
const Decorator = function () {};
@Decorator
class World {}

// intentional type errors to check transpile-only ESM loader skips type checking
parseInt(1101, 2);
const x: number = 'hello world';
const x: number = `Hello ${World.name}!`;
console.log(x);
4 changes: 3 additions & 1 deletion tests/transpile-only-swc-via-tsconfig/tsconfig.json
Expand Up @@ -4,8 +4,10 @@
"transpiler": "ts-node/transpilers/swc-experimental"
},
"compilerOptions": {
"target": "ES2018",
"module": "ESNext",
"allowJs": true,
"jsx": "react"
"jsx": "react",
"experimentalDecorators": true
}
}
7 changes: 6 additions & 1 deletion tests/transpile-only-swc/index.ts
@@ -1,4 +1,9 @@
// Test for #1343
const Decorator = function () {};
@Decorator
class World {}

// intentional type errors to check transpile-only ESM loader skips type checking
parseInt(1101, 2);
const x: number = 'hello world';
const x: number = `Hello ${World.name}!`;
console.log(x);
4 changes: 3 additions & 1 deletion tests/transpile-only-swc/tsconfig.json
@@ -1,7 +1,9 @@
{
"compilerOptions": {
"target": "ES2018",
"module": "ESNext",
"allowJs": true,
"jsx": "react"
"jsx": "react",
"experimentalDecorators": true
}
}

0 comments on commit aba4ae6

Please sign in to comment.