diff --git a/packages/transformers/typescript-tsc/package.json b/packages/transformers/typescript-tsc/package.json index e872155d063..8266d8f5ec0 100644 --- a/packages/transformers/typescript-tsc/package.json +++ b/packages/transformers/typescript-tsc/package.json @@ -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" } diff --git a/packages/transformers/typescript-tsc/src/TSCTransformer.js b/packages/transformers/typescript-tsc/src/TSCTransformer.js index d3343cf803d..a32ae945688 100644 --- a/packages/transformers/typescript-tsc/src/TSCTransformer.js +++ b/packages/transformers/typescript-tsc/src/TSCTransformer.js @@ -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, @@ -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 [ {