Skip to content
This repository was archived by the owner on Aug 4, 2021. It is now read-only.

Commit fab956a

Browse files
committed
Allow specifying tsconfig path
1 parent 48543bd commit fab956a

File tree

3 files changed

+30
-4
lines changed

3 files changed

+30
-4
lines changed

src/index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,9 @@ export default function typescript ( options = {} ) {
2727
delete options.tslib;
2828

2929
// Load options from `tsconfig.json` unless explicitly asked not to.
30-
const tsconfig = options.tsconfig === false ? {} :
31-
getCompilerOptionsFromTsConfig( typescript );
30+
const tsconfig = options.tsconfig === false ?
31+
{} :
32+
getCompilerOptionsFromTsConfig( typescript, options.tsconfig );
3233

3334
delete options.tsconfig;
3435

src/options.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,11 @@ function findFile ( cwd, filename ) {
3737
return null;
3838
}
3939

40-
export function getCompilerOptionsFromTsConfig (typescript ) {
41-
const existingTsConfig = findFile( process.cwd(), 'tsconfig.json' );
40+
export function getCompilerOptionsFromTsConfig (typescript, tsconfigPath) {
41+
if (tsconfigPath && !existsSync(tsconfigPath)) {
42+
throw new Error(`Could not find specified tsconfig.json at ${tsconfigPath}`);
43+
}
44+
const existingTsConfig = tsconfigPath || findFile( process.cwd(), 'tsconfig.json' );
4245
if (!existingTsConfig) {
4346
return {};
4447
}

test/test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ const assert = require( 'assert' );
22
const rollup = require( 'rollup' );
33
const assign = require( 'object-assign' );
44
const typescript = require( '..' );
5+
const path = require('path');
56

67
async function bundle (main, options) {
78
return rollup.rollup({
@@ -180,6 +181,27 @@ describe( 'rollup-plugin-typescript', () => {
180181
assert.notEqual( usage, -1, 'should contain usage' );
181182
});
182183

184+
it( 'allows specifying a path for tsconfig.json', async () => {
185+
const code = await getCode( 'sample/tsconfig-jsx/main.tsx',
186+
{tsconfig: path.resolve(__dirname, 'sample/tsconfig-jsx/tsconfig.json')});
187+
188+
const usage = code.indexOf( 'React.createElement("span", __assign({}, props), "Yo!")' );
189+
assert.notEqual( usage, -1, 'should contain usage' );
190+
});
191+
192+
it( 'throws if tsconfig cannot be found', async () => {
193+
let caughtError = null;
194+
try {
195+
await bundle( 'sample/tsconfig-jsx/main.tsx', {tsconfig: path.resolve(__dirname, 'does-not-exist.json')} );
196+
} catch (error) {
197+
caughtError = error;
198+
}
199+
200+
assert.ok(!!caughtError, 'Throws an error.');
201+
assert.ok(caughtError.message.indexOf( 'Could not find specified tsconfig.json' ) !== -1,
202+
`Unexpected error message: ${caughtError.message}`);
203+
});
204+
183205
it('should throw on bad options', () => {
184206
return bundle('does-not-matter.ts', {
185207
foo: 'bar'

0 commit comments

Comments
 (0)