- partial tsconfig override from plugin options (#33)

This commit is contained in:
Eugene Zolenko 2017-09-27 17:31:18 -06:00
parent 094df8992d
commit fa1bbf8882
11 changed files with 91 additions and 60 deletions

View File

@ -21,41 +21,44 @@ export default {
entry: './main.ts',
plugins: [
typescript()
typescript(/*{ plugin options }*/)
]
}
```
The plugin inherits all compiler options and file lists from your `tsconfig.json` file.
If your tsconfig has another name or another relative path from the root directory, you can pass in a custom path:
```js
// ...
plugins: [
typescript({
tsconfig: "other_dir/tsconfig.json"
})
]
```
This also allows for passing in different tsconfig files depending on your build target.
The plugin inherits all compiler options and file lists from your `tsconfig.json` file. If your tsconfig has another name or another relative path from the root directory, see `tsconfig` and `tsconfigOverride` options below. This also allows for passing in different tsconfig files depending on your build target.
The following compiler options are forced though:
* `module`: es2015
* `module`: `es2015`
* `noEmitHelpers`: true
* `importHelpers`: true
* `noResolve`: false
* `outDir`: `process.cwd()`,
* (`declarationDir`: `process.cwd()`) (*only if `useTsconfigDeclarationDir` is false in the plugin options*)
* `outDir`: `process.cwd()`
* `declarationDir`: `process.cwd()` (*only if `useTsconfigDeclarationDir` is false in the plugin options*)
* `moduleResolution`: `node` (*`classic` is [depreciated](https://www.typescriptlang.org/docs/handbook/module-resolution.html). It also breaks this plugin, see [#12](https://github.com/ezolenko/rollup-plugin-typescript2/issues/12) and [#14](https://github.com/ezolenko/rollup-plugin-typescript2/issues/14)*)
You will need to set `"moduleResolution": "node"` in `tsconfig.json` if typescript complains about missing `tslib`. See [#12](https://github.com/ezolenko/rollup-plugin-typescript2/issues/12) and [#14](https://github.com/ezolenko/rollup-plugin-typescript2/issues/14).
Plugin takes following options:
### Plugin options
* `tsconfig`: "tsconfig.json"
Override this if your tsconfig has another name or relative location from the project directory.
* `tsconfigOverride`: `{}`
The object passed as `tsconfigOverride` will be merged with loaded tsconfig before parsing. Hard overrides (see above) will be applied on top of that. Theoretically you can put everything you would put in tsconfig proper.
```js
let override = { compilerOptions: { declaration: true } };
// ...
plugins: [
typescript({ tsconfigOverride: override })
]
```
This is a [deep merge](https://lodash.com/docs/4.17.4#merge) (objects are merged, arrays are concatenated, primitives are replaced, etc), increase verbosity to 3 and look for `parsed tsconfig` if you get something unexpected.
* `check`: true
Set to false to avoid doing any diagnostic checks on the code.
@ -68,16 +71,16 @@ Plugin takes following options:
- 3 -- Debug
* `clean`: false
Set to true for clean build (wipes out cache on every build).
* `cacheRoot`: ".rts2_cache"
Path to cache.
* `include`: `[ "*.ts+(|x)", "**/*.ts+(|x)" ]`
By default passes all .ts files through typescript compiler.
By default passes all .ts files through typescript compiler.
* `exclude`: `[ "*.d.ts", "**/*.d.ts" ]`
@ -90,13 +93,13 @@ Plugin takes following options:
* `rollupCommonJSResolveHack`: false
On windows typescript resolver favors POSIX path, while commonjs plugin (and maybe others?) uses native path as module id. This can result in `namedExports` being ignored if rollup happened to use typescript's resolution. Set to true to pass resolved module path through `resolve()` to match up with `rollup-plugin-commonjs`.
* `useTsconfigDeclarationDir`: false
If true, declaration files will be emitted in the directory given in the tsconfig. If false, the declaration files will be placed inside the destination directory given in the Rollup configuration.
* `typescript`: typescript module installed with the plugin
When typescript version installed by the plugin (latest 2.x) is unacceptable, you can import your own typescript module and pass it in as `typescript: require("typescript")`. Must be 2.0+, things might break if transpiler interfaces changed enough from what the plugin was built against.
### Declarations
@ -108,7 +111,7 @@ By default, the declaration files will be located in the same directory as the g
The way typescript handles type-only imports and ambient types effectively hides them from rollup watch, because import statements are not generated and changing them doesn't trigger a rebuild.
Otherwise the plugin should work in watch mode. Make sure to run a normal build after watch session to catch any type errors.
Otherwise the plugin should work in watch mode. Make sure to run a normal build after watch session to catch any type errors.
### Version

1
dist/ioptions.d.ts vendored
View File

@ -11,4 +11,5 @@ export interface IOptions {
tsconfig: string;
useTsconfigDeclarationDir: boolean;
typescript: typeof tsModule;
tsconfigOverride: any;
}

View File

@ -17152,7 +17152,9 @@ var lodash = createCommonjsModule(function (module, exports) {
// Define as an anonymous module so, through path mapping, it can be
// referenced as the "underscore" module.
undefined(function() {
return _;
});
}
// Check for `exports` after `define` in case a build optimizer adds it.
else if (freeModule) {
@ -17180,6 +17182,7 @@ var lodash_9 = lodash.isFunction;
var lodash_10 = lodash.concat;
var lodash_11 = lodash.find;
var lodash_12 = lodash.defaults;
var lodash_14 = lodash.merge;
var RollupContext = /** @class */ (function () {
function RollupContext(verbosity, bail, context, prefix) {
@ -17318,10 +17321,6 @@ if (!lodash$2) {
var lodash_1$1 = lodash$2;
"use strict";
var graph = Graph;
var DEFAULT_EDGE_NAME = "\x00";
@ -19196,7 +19195,8 @@ module['exports'] = function zalgo(text, options) {
'̷', '͡', ' ҉'
]
},
all = [].concat(soul.up, soul.down, soul.mid);
all = [].concat(soul.up, soul.down, soul.mid),
zalgo = {};
function randomNumber(range) {
var r = Math.floor(Math.random() * range);
@ -19465,7 +19465,7 @@ function init() {
}
var sequencer = function sequencer (map, str) {
var exploded = str.split("");
var exploded = str.split(""), i = 0;
exploded = exploded.map(map);
return exploded.join("");
};
@ -19701,7 +19701,8 @@ function printDiagnostics(context, diagnostics) {
function getOptionsOverrides(_a, tsConfigJson) {
var useTsconfigDeclarationDir = _a.useTsconfigDeclarationDir;
var declaration = lodash_1(tsConfigJson, "compilerOptions.declaration", false);
return __assign({ module: tsModule.ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() }));
var overrides = __assign({ module: tsModule.ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd(), moduleResolution: tsModule.ModuleResolutionKind.NodeJs }, (!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() }));
return overrides;
}
function parseTsConfig(tsconfig, context, pluginOptions) {
@ -19714,7 +19715,12 @@ function parseTsConfig(tsconfig, context, pluginOptions) {
printDiagnostics(context, convertDiagnostic("config", [result.error]));
throw new Error("failed to parse " + fileName);
}
return tsModule.parseJsonConfigFileContent(result.config, tsModule.sys, path.dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName);
lodash_14(result.config, pluginOptions.tsconfigOverride);
var compilerOptionsOverride = getOptionsOverrides(pluginOptions, result.config);
var parsedTsConfig = tsModule.parseJsonConfigFileContent(result.config, tsModule.sys, path.dirname(fileName), compilerOptionsOverride, fileName);
context.debug("built-in options overrides: " + JSON.stringify(compilerOptionsOverride, undefined, 4));
context.debug("parsed tsconfig: " + JSON.stringify(parsedTsConfig, undefined, 4));
return parsedTsConfig;
}
// The injected id for helpers.
@ -19764,22 +19770,23 @@ function typescript(options) {
tsconfig: "tsconfig.json",
useTsconfigDeclarationDir: false,
typescript: require("typescript"),
tsconfigOverride: {},
});
setTypescriptModule(pluginOptions.typescript);
return {
options: function (config) {
rollupOptions = config;
context = new ConsoleContext(pluginOptions.verbosity, "rpt2: ");
context.info("Typescript version: " + tsModule.version);
context.debug("Plugin Options: " + JSON.stringify(pluginOptions, function (key, value) { return key === "typescript" ? "version " + value.version : value; }, 4));
context.info("typescript version: " + tsModule.version);
context.debug("plugin options: " + JSON.stringify(pluginOptions, function (key, value) { return key === "typescript" ? "version " + value.version : value; }, 4));
filter = createFilter(pluginOptions.include, pluginOptions.exclude);
context.debug("rollup config: " + JSON.stringify(rollupOptions, undefined, 4));
parsedConfig = parseTsConfig(pluginOptions.tsconfig, context, pluginOptions);
servicesHost = new LanguageServiceHost(parsedConfig);
service = tsModule.createLanguageService(servicesHost, tsModule.createDocumentRegistry());
// printing compiler option errors
if (pluginOptions.check)
printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics()));
context.debug("rollupConfig: " + JSON.stringify(rollupOptions, undefined, 4));
if (pluginOptions.clean)
cache().clean();
},

View File

@ -17148,7 +17148,9 @@ var lodash = createCommonjsModule(function (module, exports) {
// Define as an anonymous module so, through path mapping, it can be
// referenced as the "underscore" module.
undefined(function() {
return _;
});
}
// Check for `exports` after `define` in case a build optimizer adds it.
else if (freeModule) {
@ -17176,6 +17178,7 @@ var lodash_9 = lodash.isFunction;
var lodash_10 = lodash.concat;
var lodash_11 = lodash.find;
var lodash_12 = lodash.defaults;
var lodash_14 = lodash.merge;
var RollupContext = /** @class */ (function () {
function RollupContext(verbosity, bail, context, prefix) {
@ -17314,10 +17317,6 @@ if (!lodash$2) {
var lodash_1$1 = lodash$2;
"use strict";
var graph = Graph;
var DEFAULT_EDGE_NAME = "\x00";
@ -19192,7 +19191,8 @@ module['exports'] = function zalgo(text, options) {
'̷', '͡', ' ҉'
]
},
all = [].concat(soul.up, soul.down, soul.mid);
all = [].concat(soul.up, soul.down, soul.mid),
zalgo = {};
function randomNumber(range) {
var r = Math.floor(Math.random() * range);
@ -19461,7 +19461,7 @@ function init() {
}
var sequencer = function sequencer (map, str) {
var exploded = str.split("");
var exploded = str.split(""), i = 0;
exploded = exploded.map(map);
return exploded.join("");
};
@ -19697,7 +19697,8 @@ function printDiagnostics(context, diagnostics) {
function getOptionsOverrides(_a, tsConfigJson) {
var useTsconfigDeclarationDir = _a.useTsconfigDeclarationDir;
var declaration = lodash_1(tsConfigJson, "compilerOptions.declaration", false);
return __assign({ module: tsModule.ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() }));
var overrides = __assign({ module: tsModule.ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd(), moduleResolution: tsModule.ModuleResolutionKind.NodeJs }, (!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() }));
return overrides;
}
function parseTsConfig(tsconfig, context, pluginOptions) {
@ -19710,7 +19711,12 @@ function parseTsConfig(tsconfig, context, pluginOptions) {
printDiagnostics(context, convertDiagnostic("config", [result.error]));
throw new Error("failed to parse " + fileName);
}
return tsModule.parseJsonConfigFileContent(result.config, tsModule.sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName);
lodash_14(result.config, pluginOptions.tsconfigOverride);
var compilerOptionsOverride = getOptionsOverrides(pluginOptions, result.config);
var parsedTsConfig = tsModule.parseJsonConfigFileContent(result.config, tsModule.sys, dirname(fileName), compilerOptionsOverride, fileName);
context.debug("built-in options overrides: " + JSON.stringify(compilerOptionsOverride, undefined, 4));
context.debug("parsed tsconfig: " + JSON.stringify(parsedTsConfig, undefined, 4));
return parsedTsConfig;
}
// The injected id for helpers.
@ -19760,22 +19766,23 @@ function typescript(options) {
tsconfig: "tsconfig.json",
useTsconfigDeclarationDir: false,
typescript: require("typescript"),
tsconfigOverride: {},
});
setTypescriptModule(pluginOptions.typescript);
return {
options: function (config) {
rollupOptions = config;
context = new ConsoleContext(pluginOptions.verbosity, "rpt2: ");
context.info("Typescript version: " + tsModule.version);
context.debug("Plugin Options: " + JSON.stringify(pluginOptions, function (key, value) { return key === "typescript" ? "version " + value.version : value; }, 4));
context.info("typescript version: " + tsModule.version);
context.debug("plugin options: " + JSON.stringify(pluginOptions, function (key, value) { return key === "typescript" ? "version " + value.version : value; }, 4));
filter = createFilter(pluginOptions.include, pluginOptions.exclude);
context.debug("rollup config: " + JSON.stringify(rollupOptions, undefined, 4));
parsedConfig = parseTsConfig(pluginOptions.tsconfig, context, pluginOptions);
servicesHost = new LanguageServiceHost(parsedConfig);
service = tsModule.createLanguageService(servicesHost, tsModule.createDocumentRegistry());
// printing compiler option errors
if (pluginOptions.check)
printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics()));
context.debug("rollupConfig: " + JSON.stringify(rollupOptions, undefined, 4));
if (pluginOptions.clean)
cache().clean();
},

View File

@ -1,6 +1,6 @@
{
"name": "rollup-plugin-typescript2",
"version": "0.6.1",
"version": "0.7.0",
"description": "Seamless integration between Rollup and TypeScript. Now with errors.",
"main": "dist/rollup-plugin-typescript2.cjs.js",
"module": "dist/rollup-plugin-typescript2.es.js",

View File

@ -29,12 +29,12 @@ export default {
{
"graphlib": [ "alg", "Graph" ],
"colors/safe": [ "green", "white", "red", "yellow", "blue" ],
"lodash": [ "get", "each", "isEqual", "some", "filter", "endsWith", "map", "has", "isFunction", "concat", "find", "defaults" ],
"lodash": [ "get", "each", "isEqual", "some", "filter", "endsWith", "map", "has", "isFunction", "concat", "find", "defaults", "assign", "merge" ],
// "fs-extra": [ "renameSync", "removeSync", "ensureFileSync", "writeJsonSync", "readJsonSync", "existsSync", "readdirSync", "emptyDirSync" ],
},
}
),
ts({ verbosity: 3, abortOnError: false }),
ts({ verbosity: 2, abortOnError: false }),
],
banner: '/* eslint-disable */',

View File

@ -6,12 +6,14 @@ import * as _ from "lodash";
export function getOptionsOverrides({ useTsconfigDeclarationDir }: IOptions, tsConfigJson?: any): tsTypes.CompilerOptions
{
const declaration = _.get(tsConfigJson, "compilerOptions.declaration", false);
return {
const overrides = {
module: tsModule.ModuleKind.ES2015,
noEmitHelpers: true,
importHelpers: true,
noResolve: false,
outDir: process.cwd(),
moduleResolution: tsModule.ModuleResolutionKind.NodeJs,
...(!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() }),
};
return overrides;
}

View File

@ -56,6 +56,7 @@ export default function typescript(options?: Partial<IOptions>)
tsconfig: "tsconfig.json",
useTsconfigDeclarationDir: false,
typescript: require("typescript"),
tsconfigOverride: {},
});
setTypescriptModule(pluginOptions.typescript);
@ -67,11 +68,13 @@ export default function typescript(options?: Partial<IOptions>)
rollupOptions = config;
context = new ConsoleContext(pluginOptions.verbosity, "rpt2: ");
context.info(`Typescript version: ${tsModule.version}`);
context.debug(`Plugin Options: ${JSON.stringify(pluginOptions, (key, value) => key === "typescript" ? `version ${(value as typeof tsModule).version}` : value, 4)}`);
context.info(`typescript version: ${tsModule.version}`);
context.debug(`plugin options: ${JSON.stringify(pluginOptions, (key, value) => key === "typescript" ? `version ${(value as typeof tsModule).version}` : value, 4)}`);
filter = createFilter(pluginOptions.include, pluginOptions.exclude);
context.debug(`rollup config: ${JSON.stringify(rollupOptions, undefined, 4)}`);
parsedConfig = parseTsConfig(pluginOptions.tsconfig, context, pluginOptions);
servicesHost = new LanguageServiceHost(parsedConfig);
@ -82,8 +85,6 @@ export default function typescript(options?: Partial<IOptions>)
if (pluginOptions.check)
printDiagnostics(context, convertDiagnostic("options", service.getCompilerOptionsDiagnostics()));
context.debug(`rollupConfig: ${JSON.stringify(rollupOptions, undefined, 4)}`);
if (pluginOptions.clean)
cache().clean();
},

View File

@ -13,4 +13,5 @@ export interface IOptions
tsconfig: string;
useTsconfigDeclarationDir: boolean;
typescript: typeof tsModule;
tsconfigOverride: any;
}

View File

@ -6,6 +6,7 @@ import { printDiagnostics } from "./print-diagnostics";
import { convertDiagnostic } from "./tscache";
import { getOptionsOverrides } from "./get-options-overrides";
import { IOptions } from "./ioptions";
import * as _ from "lodash";
export function parseTsConfig(tsconfig: string, context: IContext, pluginOptions: IOptions): tsTypes.ParsedCommandLine
{
@ -23,5 +24,13 @@ export function parseTsConfig(tsconfig: string, context: IContext, pluginOptions
throw new Error(`failed to parse ${fileName}`);
}
return tsModule.parseJsonConfigFileContent(result.config, tsModule.sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName);
_.merge(result.config, pluginOptions.tsconfigOverride);
const compilerOptionsOverride = getOptionsOverrides(pluginOptions, result.config);
const parsedTsConfig = tsModule.parseJsonConfigFileContent(result.config, tsModule.sys, dirname(fileName), compilerOptionsOverride, fileName);
context.debug(`built-in options overrides: ${JSON.stringify(compilerOptionsOverride, undefined, 4)}`);
context.debug(`parsed tsconfig: ${JSON.stringify(parsedTsConfig, undefined, 4)}`);
return parsedTsConfig;
}

View File

@ -19,5 +19,5 @@
},
"include": [
"src/**/*.ts"
]
]
}