Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: support options.cwd #197

Merged
merged 1 commit into from
Dec 3, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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).
Expand Down
4 changes: 2 additions & 2 deletions src/get-options-overrides.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions src/host.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
7 changes: 5 additions & 2 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ const typescript: PluginImpl<Partial<IOptions>> = (options) =>
transformers: [],
tsconfigDefaults: {},
objectHashIgnoreUnknownHack: false,
cwd: process.cwd(),
});

if (!pluginOptions.typescript) {
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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",
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions src/ioptions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ export type TransformerFactoryCreator = (ls: tsTypes.LanguageService) => tsTypes

export interface IOptions
{
cwd: string;
include: string|string[];
exclude: string|string[];
check: boolean;
Expand Down
4 changes: 2 additions & 2 deletions src/parse-tsconfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down