-
-
Notifications
You must be signed in to change notification settings - Fork 536
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 transpiler and new --transpiler option to use third-party transpilers #1160
Merged
Merged
Changes from 10 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
19990d4
WIP experimental swc compiler
cspotcode 1551509
Fix optional peer dep
cspotcode fcd27cc
wip
cspotcode 77436c3
properly merge all of ts onto exports object
cspotcode f77e1b1
fix clobbering of code because swc does not append a //# sourcemap co…
cspotcode c82c820
More changes:
cspotcode b13a631
Merge remote-tracking branch 'origin/master' into ab/swc-compiler
cspotcode 78b49e3
Fix bug in swc loading to allow swc API instance to be passed to factory
cspotcode 230f638
lint fixes
cspotcode f1b1be0
Fix typo in createTypescriptCompiler function name
cspotcode ab67165
Switch from hacky overloading the "compiler" config to implementing a…
cspotcode c1548cf
Merge remote-tracking branch 'origin/master' into ab/swc-compiler
cspotcode 369d43a
fix package.json files array and add --transpiler CLI flag
cspotcode 7103b68
make --transpiler imply --transpile-only and add tests
cspotcode 026b4c1
fixes
cspotcode d72dbd0
add missing test files
cspotcode 52082fd
add @swc/core dep to tests
cspotcode 7a855ed
add some jsdoc to new transpiler api surface
cspotcode 6fe8249
change transpiler options to be specified as "transpiler: [name, {/*o…
cspotcode 14671a3
Merge remote-tracking branch 'origin/master' into ab/swc-compiler
cspotcode c3c5a3e
fix
cspotcode 1a06089
cleanup comments
cspotcode File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
module.exports = require('../dist/compilers/swc') |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
import type * as ts from 'typescript' | ||
import type * as swcWasm from '@swc/wasm' | ||
import type * as swcTypes from '@swc/core' | ||
import type { TSCommon } from '..' | ||
export interface Options { | ||
/** TypeScript compiler to wrap */ | ||
compiler?: string | TSCommon | ||
/** | ||
* swc compiler to use for compilation | ||
* Set to '@swc/wasm' to use swc's WASM compiler | ||
* Default: '@swc/core' | ||
*/ | ||
swc?: string | typeof swcWasm | ||
} | ||
|
||
export function createTypescriptCompiler (options: Options = {}) { | ||
const { swc, compiler = 'typescript' } = options | ||
const compilerInstance = typeof compiler === 'string' ? require(compiler) as TSCommon : compiler | ||
let swcInstance: typeof swcWasm | ||
if (typeof swc === 'string') { | ||
swcInstance = require(swc) as typeof swcWasm | ||
} else if (swc == null) { // tslint:disable-line | ||
let swcResolved | ||
try { | ||
swcResolved = require.resolve('@swc/core') | ||
} catch (e) { | ||
try { | ||
swcResolved = require.resolve('@swc/wasm') | ||
} catch (e) { | ||
throw new Error('swc compiler requires either @swc/core or @swc/wasm to be installed as dependencies') | ||
} | ||
} | ||
swcInstance = require(swcResolved) as typeof swcWasm | ||
} else { | ||
swcInstance = swc | ||
} | ||
|
||
const version = `${ compilerInstance.version }-tsnode-${ require('../../package').version }-swc` | ||
|
||
const transpileModule: TSCommon['transpileModule'] = (input: string, transpileOptions: ts.TranspileOptions): ts.TranspileOutput => { | ||
const compilerOptions = transpileOptions.compilerOptions! | ||
const { fileName } = transpileOptions | ||
const { esModuleInterop, sourceMap, importHelpers, experimentalDecorators, emitDecoratorMetadata, target, jsxFactory, jsxFragmentFactory } = compilerOptions | ||
const options: swcTypes.Options = { | ||
filename: fileName, | ||
sourceMaps: sourceMap, | ||
// isModule: true, | ||
module: { | ||
type: 'commonjs', | ||
noInterop: !esModuleInterop | ||
}, | ||
swcrc: false, | ||
jsc: { | ||
externalHelpers: importHelpers, | ||
parser: { | ||
syntax: 'typescript', | ||
tsx: fileName!.endsWith('.tsx') || fileName!.endsWith('.jsx'), | ||
decorators: experimentalDecorators, | ||
dynamicImport: true | ||
}, | ||
target: targetMapping.get(target!) ?? 'es3', | ||
transform: { | ||
decoratorMetadata: emitDecoratorMetadata, | ||
legacyDecorator: true, | ||
react: { | ||
throwIfNamespace: false, | ||
development: false, | ||
useBuiltins: false, | ||
pragma: jsxFactory!, | ||
pragmaFrag: jsxFragmentFactory! | ||
} | ||
} | ||
} | ||
} | ||
const { code, map } = swcInstance.transformSync(input, options) | ||
return { outputText: code, sourceMapText: map } | ||
} | ||
return { | ||
...compilerInstance, | ||
version, | ||
transpileModule | ||
} | ||
} | ||
|
||
const targetMapping = new Map<ts.ScriptTarget, swcTypes.JscTarget>() | ||
targetMapping.set(/* ts.ScriptTarget.ES3 */ 0, 'es3') | ||
targetMapping.set(/* ts.ScriptTarget.ES5 */ 1, 'es5') | ||
targetMapping.set(/* ts.ScriptTarget.ES2015 */ 2, 'es2015') | ||
targetMapping.set(/* ts.ScriptTarget.ES2016 */ 3, 'es2016') | ||
targetMapping.set(/* ts.ScriptTarget.ES2017 */ 4, 'es2017') | ||
targetMapping.set(/* ts.ScriptTarget.ES2018 */ 5, 'es2018') | ||
targetMapping.set(/* ts.ScriptTarget.ES2019 */ 6, 'es2019') | ||
targetMapping.set(/* ts.ScriptTarget.ES2020 */ 7, 'es2019') | ||
targetMapping.set(/* ts.ScriptTarget.ESNext */ 99, 'es2019') | ||
targetMapping.set(/* ts.ScriptTarget.Latest */ 99, 'es2019') | ||
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
99 is set twice - intentional?