Skip to content

Commit

Permalink
feat: export transpileIfTypescript
Browse files Browse the repository at this point in the history
  • Loading branch information
joscha committed Mar 14, 2017
1 parent b72f7b8 commit 6f835af
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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;
Expand Down
4 changes: 2 additions & 2 deletions src/transpile-if-ts.ts
Original file line number Diff line number Diff line change
@@ -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
});

Expand Down
19 changes: 19 additions & 0 deletions tests/__tests__/transpileIfTypescript.spec.ts
Original file line number Diff line number Diff line change
@@ -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));
});
});

0 comments on commit 6f835af

Please sign in to comment.