Skip to content

Commit 6f835af

Browse files
committed
feat: export transpileIfTypescript
1 parent b72f7b8 commit 6f835af

3 files changed

Lines changed: 22 additions & 2 deletions

File tree

src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { defaultRetrieveFileHandler } from './default-retrieve-file-handler';
22
import * as sourceMapSupport from 'source-map-support';
33

4+
export { transpileIfTypescript } from './transpile-if-ts';
45
export function install() {
56
var options: sourceMapSupport.Options = {};
67
options.retrieveFile = defaultRetrieveFileHandler;

src/transpile-if-ts.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import * as tsc from 'typescript';
22
import { getTSConfig } from './utils';
33

4-
export function transpileIfTypescript(path, contents) {
4+
export function transpileIfTypescript(path, contents, config?) {
55
if (path && (path.endsWith('.tsx') || path.endsWith('.ts'))) {
66

77
let transpiled = tsc.transpileModule(contents, {
8-
compilerOptions: getTSConfig({ __TS_CONFIG__: global['__TS_CONFIG__'] }, true),
8+
compilerOptions: getTSConfig(config || { __TS_CONFIG__: global['__TS_CONFIG__'] }, true),
99
fileName: path
1010
});
1111

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { transpileIfTypescript } from '../../src';
2+
3+
describe.only('transpileIfTypescript', () => {
4+
it('should ignore anything non-TS', () => {
5+
const contents = 'unaltered';
6+
expect(transpileIfTypescript('some.js', contents)).toBe(contents);
7+
});
8+
it('should be able to transpile some TS', () => {
9+
const ts = 'const x:string = "anything";';
10+
expect(transpileIfTypescript('some.ts', ts)).toMatch('var x = "anything";');
11+
expect(transpileIfTypescript('some.tsx', ts)).toMatch('var x = "anything";');
12+
});
13+
14+
it('should be possible to pass a custom config', () => {
15+
const customTsConfigFile = 'not-existant.json';
16+
const customConfig = { __TS_CONFIG__: customTsConfigFile};
17+
expect(() => transpileIfTypescript('some.ts', '', customConfig)).toThrow(new RegExp(customTsConfigFile));
18+
});
19+
});

0 commit comments

Comments
 (0)