feat: support options.cwd (#197)

This commit is contained in:
chencheng (云谦) 2019-12-04 01:20:41 +08:00 committed by Eugene Zolenko
parent d6359af2b0
commit 4217346054
6 changed files with 17 additions and 8 deletions

View File

@ -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).

View File

@ -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;

View File

@ -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<string>;
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()

View File

@ -60,6 +60,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
transformers: [],
tsconfigDefaults: {},
objectHashIgnoreUnknownHack: false,
cwd: process.cwd(),
});
if (!pluginOptions.typescript) {
@ -103,7 +104,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (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<Partial<IOptions>> = (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<Partial<IOptions>> = (options) =>
}
};
console.log('>>> DEBUG', 'declarations', declarations);
_.each(declarations, ({ type, map }, key) =>
{
emitDeclaration(key, ".d.ts", type);

View File

@ -12,6 +12,7 @@ export type TransformerFactoryCreator = (ls: tsTypes.LanguageService) => tsTypes
export interface IOptions
{
cwd: string;
include: string|string[];
exclude: string|string[];
check: boolean;

View File

@ -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)