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

Commit

Permalink
Allow specifying tsconfig path
Browse files Browse the repository at this point in the history
  • Loading branch information
lukastaegert committed Sep 15, 2018
1 parent 48543bd commit a1a31ff
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 5 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
7 changes: 5 additions & 2 deletions src/options.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {};
}
Expand Down
22 changes: 22 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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({
Expand Down Expand Up @@ -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'
Expand Down

0 comments on commit a1a31ff

Please sign in to comment.