Skip to content

Commit

Permalink
Support user's typescript
Browse files Browse the repository at this point in the history
  • Loading branch information
Will Binns-Smith committed Jul 10, 2019
1 parent 702b80f commit 7e1aa50
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 17 deletions.
1 change: 1 addition & 0 deletions packages/transformers/typescript-tsc/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
},
"main": "src/TSCTransformer",
"dependencies": {
"@parcel/local-require": "^2.0.0-alpha.0",
"@parcel/plugin": "^2.0.0-alpha.0",
"typescript": "^3.4.5"
}
Expand Down
41 changes: 24 additions & 17 deletions packages/transformers/typescript-tsc/src/TSCTransformer.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
// @flow strict-local

import {Transformer} from '@parcel/plugin';
// $FlowFixMe
import typescript from 'typescript';
import localRequire from '@parcel/local-require';

type TypescriptCompilerOptions = {
module?: mixed,
Expand All @@ -24,22 +23,30 @@ export default new Transformer({
async transform({asset, config}) {
asset.type = 'js';

// Transpile Module using TypeScript
let typescript =
config == null
? // $FlowFixMe
require('typescript')
: await localRequire('typescript', asset.filePath);

let code = await asset.getCode();
let transpiled = typescript.transpileModule(code, {
compilerOptions: {
// React is the default. Users can override this by supplying their own tsconfig,
// which many TypeScript users will already have for typechecking, etc.
jsx: 'React',
...config?.compilerOptions,
// Always emit output
noEmit: false,
// Don't compile ES `import`s -- scope hoisting prefers them and they will
// otherwise compiled to CJS via babel in the js transformer
module: typescript.ModuleKind.ESNext
},
fileName: asset.filePath // Should be relativePath?
});
let transpiled = typescript.transpileModule(
code,
({
compilerOptions: {
// React is the default. Users can override this by supplying their own tsconfig,
// which many TypeScript users will already have for typechecking, etc.
jsx: 'React',
...config?.compilerOptions,
// Always emit output
noEmit: false,
// Don't compile ES `import`s -- scope hoisting prefers them and they will
// otherwise compiled to CJS via babel in the js transformer
module: typescript.ModuleKind.ESNext
},
fileName: asset.filePath // Should be relativePath?
}: TypescriptTranspilerOptions)
);

return [
{
Expand Down

0 comments on commit 7e1aa50

Please sign in to comment.