-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(typescript-plugins): use babel to transpile
.ts
plugins
- Loading branch information
1 parent
9b89840
commit 79c144a
Showing
16 changed files
with
967 additions
and
619 deletions.
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
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
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,50 @@ | ||
import { GeneratorOptions } from '@babel/generator'; | ||
import * as recast from 'recast'; | ||
|
||
const DEFAULT_OPTIONS = { | ||
sourceType: 'module', | ||
allowImportExportEverywhere: true, | ||
allowReturnOutsideFunction: true, | ||
allowSuperOutsideMethod: true, | ||
plugins: [ | ||
'flow', | ||
'jsx', | ||
'asyncGenerators', | ||
'classProperties', | ||
'doExpressions', | ||
'exportExtensions', | ||
'functionBind', | ||
'functionSent', | ||
'objectRestSpread', | ||
'dynamicImport', | ||
'decorators' | ||
] | ||
}; | ||
|
||
type ParseOptions = object; | ||
type AST = object; | ||
|
||
export default { | ||
parserOverride( | ||
code: string, | ||
options: ParseOptions, | ||
parse: (code: string, options: ParseOptions) => AST | ||
): AST { | ||
return recast.parse(code, { | ||
parser: { | ||
parse(code: string) { | ||
return parse(code, DEFAULT_OPTIONS); | ||
} | ||
} | ||
}); | ||
}, | ||
|
||
generatorOverride( | ||
ast: AST, | ||
options: GeneratorOptions, | ||
code: string, | ||
generate: (ast: AST, options: GeneratorOptions) => string | ||
): string { | ||
return recast.print(ast); | ||
} | ||
}; |
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
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
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,56 @@ | ||
import { transform } from '@babel/core'; | ||
import { extname } from 'path'; | ||
import { addHook } from 'pirates'; | ||
|
||
let useBabelrc = false; | ||
let revert: (() => void) | null = null; | ||
|
||
export const SUPPORTED_EXTENSIONS = new Set([ | ||
'.js', | ||
'.jsx', | ||
'.es', | ||
'.es6', | ||
'.mjs', | ||
'.ts' | ||
]); | ||
|
||
export function hook(code: string, filename: string): string { | ||
let ext = extname(filename); | ||
|
||
if (!SUPPORTED_EXTENSIONS.has(ext)) { | ||
throw new Error(`cannot load file type '${ext}': ${filename}`); | ||
} | ||
|
||
let options = { | ||
filename, | ||
babelrc: useBabelrc, | ||
presets: [] as Array<string>, | ||
sourceMaps: 'inline' | ||
}; | ||
|
||
if (!useBabelrc) { | ||
if (ext === '.ts') { | ||
options.presets.push(require('@babel/preset-typescript').default()); | ||
} | ||
|
||
options.presets.push(require('@babel/preset-env').default()); | ||
} | ||
|
||
return transform(code, options).code as string; | ||
} | ||
|
||
export function enable(babelrc: boolean = false) { | ||
disable(); | ||
useBabelrc = babelrc; | ||
revert = addHook(hook, { | ||
exts: Array.from(SUPPORTED_EXTENSIONS), | ||
ignoreNodeModules: true | ||
}); | ||
} | ||
|
||
export function disable() { | ||
if (revert) { | ||
revert(); | ||
revert = null; | ||
} | ||
} |
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
Oops, something went wrong.