diff --git a/src/index.ts b/src/index.ts index 1f8bc46ab0..338a527f4c 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,6 +1,7 @@ import { defaultRetrieveFileHandler } from './default-retrieve-file-handler'; import * as sourceMapSupport from 'source-map-support'; +export { transpileIfTypescript } from './transpile-if-ts'; export function install() { var options: sourceMapSupport.Options = {}; options.retrieveFile = defaultRetrieveFileHandler; diff --git a/src/transpile-if-ts.ts b/src/transpile-if-ts.ts index 2c9c596ef0..f7cbd2afc7 100644 --- a/src/transpile-if-ts.ts +++ b/src/transpile-if-ts.ts @@ -1,11 +1,11 @@ import * as tsc from 'typescript'; import { getTSConfig } from './utils'; -export function transpileIfTypescript(path, contents) { +export function transpileIfTypescript(path, contents, config?) { if (path && (path.endsWith('.tsx') || path.endsWith('.ts'))) { let transpiled = tsc.transpileModule(contents, { - compilerOptions: getTSConfig({ __TS_CONFIG__: global['__TS_CONFIG__'] }, true), + compilerOptions: getTSConfig(config || { __TS_CONFIG__: global['__TS_CONFIG__'] }, true), fileName: path }); diff --git a/tests/__tests__/transpileIfTypescript.spec.ts b/tests/__tests__/transpileIfTypescript.spec.ts new file mode 100644 index 0000000000..5a6d04b0fb --- /dev/null +++ b/tests/__tests__/transpileIfTypescript.spec.ts @@ -0,0 +1,19 @@ +import { transpileIfTypescript } from '../../src'; + +describe.only('transpileIfTypescript', () => { + it('should ignore anything non-TS', () => { + const contents = 'unaltered'; + expect(transpileIfTypescript('some.js', contents)).toBe(contents); + }); + it('should be able to transpile some TS', () => { + const ts = 'const x:string = "anything";'; + expect(transpileIfTypescript('some.ts', ts)).toMatch('var x = "anything";'); + expect(transpileIfTypescript('some.tsx', ts)).toMatch('var x = "anything";'); + }); + + it('should be possible to pass a custom config', () => { + const customTsConfigFile = 'not-existant.json'; + const customConfig = { __TS_CONFIG__: customTsConfigFile}; + expect(() => transpileIfTypescript('some.ts', '', customConfig)).toThrow(new RegExp(customTsConfigFile)); + }); +}); \ No newline at end of file