diff --git a/README.md b/README.md index 2183541..c95b280 100644 --- a/README.md +++ b/README.md @@ -93,6 +93,10 @@ See [#108](https://github.com/ezolenko/rollup-plugin-typescript2/issues/108) ### Plugin options +* `cwd`: `string` + + The current work directory, default `process.cwd()`. + * `tsconfigDefaults`: `{}` The object passed as `tsconfigDefaults` will be merged with loaded `tsconfig.json`. Final config passed to typescript will be the result of values in `tsconfigDefaults` replaced by values in loaded `tsconfig.json`, replaced by values in `tsconfigOverride` and then replaced by hard `compilerOptions` overrides on top of that (see above). diff --git a/src/get-options-overrides.ts b/src/get-options-overrides.ts index 322e2a8..99e7c1f 100644 --- a/src/get-options-overrides.ts +++ b/src/get-options-overrides.ts @@ -8,7 +8,7 @@ import { IContext } from "./context"; // tslint:disable-next-line:no-var-requires const createRollupFilter = require("rollup-pluginutils").createFilter; -export function getOptionsOverrides({ useTsconfigDeclarationDir, cacheRoot }: IOptions, preParsedTsconfig?: tsTypes.ParsedCommandLine): tsTypes.CompilerOptions +export function getOptionsOverrides({ useTsconfigDeclarationDir, cacheRoot, cwd }: IOptions, preParsedTsconfig?: tsTypes.ParsedCommandLine): tsTypes.CompilerOptions { const overrides: tsTypes.CompilerOptions = { noEmitHelpers: false, @@ -30,7 +30,7 @@ export function getOptionsOverrides({ useTsconfigDeclarationDir, cacheRoot }: IO if (!declaration) overrides.declarationDir = undefined; if (declaration && !useTsconfigDeclarationDir) - overrides.declarationDir = process.cwd(); + overrides.declarationDir = cwd; // unsetting sourceRoot if sourceMap is not enabled (in case original tsconfig had inlineSourceMap set that is being unset and would cause TS5051) const sourceMap = preParsedTsconfig.options.sourceMap; diff --git a/src/host.ts b/src/host.ts index 006c939..a6f7f92 100644 --- a/src/host.ts +++ b/src/host.ts @@ -6,15 +6,16 @@ import { TransformerFactoryCreator } from "./ioptions"; export class LanguageServiceHost implements tsTypes.LanguageServiceHost { - private cwd = process.cwd(); + private cwd: string; private snapshots: { [fileName: string]: tsTypes.IScriptSnapshot } = {}; private versions: { [fileName: string]: number } = {}; private service?: tsTypes.LanguageService; private fileNames: Set; - constructor(private parsedConfig: tsTypes.ParsedCommandLine, private transformers: TransformerFactoryCreator[]) + constructor(private parsedConfig: tsTypes.ParsedCommandLine, private transformers: TransformerFactoryCreator[], cwd: string) { this.fileNames = new Set(parsedConfig.fileNames); + this.cwd = cwd; } public reset() diff --git a/src/index.ts b/src/index.ts index a92e475..db026f6 100644 --- a/src/index.ts +++ b/src/index.ts @@ -60,6 +60,7 @@ const typescript: PluginImpl> = (options) => transformers: [], tsconfigDefaults: {}, objectHashIgnoreUnknownHack: false, + cwd: process.cwd(), }); if (!pluginOptions.typescript) { @@ -103,7 +104,7 @@ const typescript: PluginImpl> = (options) => filter = createFilter(context, pluginOptions, parsedConfig); - servicesHost = new LanguageServiceHost(parsedConfig, pluginOptions.transformers); + servicesHost = new LanguageServiceHost(parsedConfig, pluginOptions.transformers, pluginOptions.cwd); service = tsModule.createLanguageService(servicesHost, tsModule.createDocumentRegistry()); servicesHost.setLanguageService(service); @@ -357,7 +358,8 @@ const typescript: PluginImpl> = (options) => } else { - const relativePath = relative(process.cwd(), fileName); + const relativePath = relative(pluginOptions.cwd, fileName); + console.log('>>> DEBUG2', pluginOptions.cwd, relativePath, fileName); context.debug(() => `${blue("emitting declarations")} for '${key}' to '${relativePath}'`); this.emitFile({ type: "asset", @@ -367,6 +369,7 @@ const typescript: PluginImpl> = (options) => } }; + console.log('>>> DEBUG', 'declarations', declarations); _.each(declarations, ({ type, map }, key) => { emitDeclaration(key, ".d.ts", type); diff --git a/src/ioptions.ts b/src/ioptions.ts index d7cfd12..5fe3b6e 100644 --- a/src/ioptions.ts +++ b/src/ioptions.ts @@ -12,6 +12,7 @@ export type TransformerFactoryCreator = (ls: tsTypes.LanguageService) => tsTypes export interface IOptions { + cwd: string; include: string|string[]; exclude: string|string[]; check: boolean; diff --git a/src/parse-tsconfig.ts b/src/parse-tsconfig.ts index 6fc1974..cd5a6b3 100644 --- a/src/parse-tsconfig.ts +++ b/src/parse-tsconfig.ts @@ -10,14 +10,14 @@ import { checkTsConfig } from "./check-tsconfig"; export function parseTsConfig(context: IContext, pluginOptions: IOptions) { - const fileName = tsModule.findConfigFile(process.cwd(), tsModule.sys.fileExists, pluginOptions.tsconfig); + const fileName = tsModule.findConfigFile(pluginOptions.cwd, tsModule.sys.fileExists, pluginOptions.tsconfig); // if the value was provided, but no file, fail hard if (pluginOptions.tsconfig !== undefined && !fileName) throw new Error(`failed to open '${fileName}'`); let loadedConfig: any = {}; - let baseDir = process.cwd(); + let baseDir = pluginOptions.cwd; let configFileName; let pretty = false; if (fileName)