diff --git a/README.md b/README.md index d99e631..a7612dc 100644 --- a/README.md +++ b/README.md @@ -47,7 +47,7 @@ The following options are unique to `rollup-plugin-typescript`: * `options.include` and `options.exclude` (each a minimatch pattern, or array of minimatch patterns), which determine which files are transpiled by Typescript (all `.ts` and `.tsx` files by default). -* `tsconfig` when set to false, ignores any options specified in the config file +* `tsconfig` when set to false, ignores any options specified in the config file. If set to a string that corresponds to a file path, the specified file will be used as config file. * `typescript` overrides TypeScript used for transpilation: ```js diff --git a/src/index.js b/src/index.js index d1ece02..a3f4e4f 100644 --- a/src/index.js +++ b/src/index.js @@ -27,8 +27,9 @@ export default function typescript ( options = {} ) { delete options.tslib; // Load options from `tsconfig.json` unless explicitly asked not to. - const tsconfig = options.tsconfig === false ? {} : - getCompilerOptionsFromTsConfig( typescript ); + const tsconfig = options.tsconfig === false ? + {} : + getCompilerOptionsFromTsConfig( typescript, options.tsconfig ); delete options.tsconfig; diff --git a/src/options.js b/src/options.js index 0b76549..3cd9556 100644 --- a/src/options.js +++ b/src/options.js @@ -37,8 +37,11 @@ function findFile ( cwd, filename ) { return null; } -export function getCompilerOptionsFromTsConfig (typescript ) { - const existingTsConfig = findFile( process.cwd(), 'tsconfig.json' ); +export function getCompilerOptionsFromTsConfig (typescript, tsconfigPath) { + if (tsconfigPath && !existsSync(tsconfigPath)) { + throw new Error(`Could not find specified tsconfig.json at ${tsconfigPath}`); + } + const existingTsConfig = tsconfigPath || findFile( process.cwd(), 'tsconfig.json' ); if (!existingTsConfig) { return {}; } diff --git a/test/test.js b/test/test.js index 14a373d..615f65f 100644 --- a/test/test.js +++ b/test/test.js @@ -2,6 +2,7 @@ const assert = require( 'assert' ); const rollup = require( 'rollup' ); const assign = require( 'object-assign' ); const typescript = require( '..' ); +const path = require('path'); async function bundle (main, options) { return rollup.rollup({ @@ -180,6 +181,27 @@ describe( 'rollup-plugin-typescript', () => { assert.notEqual( usage, -1, 'should contain usage' ); }); + it( 'allows specifying a path for tsconfig.json', async () => { + const code = await getCode( 'sample/tsconfig-jsx/main.tsx', + {tsconfig: path.resolve(__dirname, 'sample/tsconfig-jsx/tsconfig.json')}); + + const usage = code.indexOf( 'React.createElement("span", __assign({}, props), "Yo!")' ); + assert.notEqual( usage, -1, 'should contain usage' ); + }); + + it( 'throws if tsconfig cannot be found', async () => { + let caughtError = null; + try { + await bundle( 'sample/tsconfig-jsx/main.tsx', {tsconfig: path.resolve(__dirname, 'does-not-exist.json')} ); + } catch (error) { + caughtError = error; + } + + assert.ok(!!caughtError, 'Throws an error.'); + assert.ok(caughtError.message.indexOf( 'Could not find specified tsconfig.json' ) !== -1, + `Unexpected error message: ${caughtError.message}`); + }); + it('should throw on bad options', () => { return bundle('does-not-matter.ts', { foo: 'bar'