Skip to content

Commit

Permalink
- fix for TS5052
Browse files Browse the repository at this point in the history
- cleanup
  • Loading branch information
ezolenko committed Aug 1, 2017
1 parent c72a610 commit c501e1a
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 37 deletions.
2 changes: 1 addition & 1 deletion dist/get-options-overrides.d.ts
@@ -1,3 +1,3 @@
import { CompilerOptions } from "typescript";
import { IOptions } from "./ioptions";
export declare function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions): CompilerOptions;
export declare function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions, tsConfigJson?: any): CompilerOptions;
23 changes: 14 additions & 9 deletions dist/rollup-plugin-typescript2.cjs.js
Expand Up @@ -449,9 +449,10 @@ function printDiagnostics(context, diagnostics) {
});
}

function getOptionsOverrides(_a) {
function getOptionsOverrides(_a, tsConfigJson) {
var useTsconfigDeclarationDir = _a.useTsconfigDeclarationDir;
return __assign({ module: typescript.ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() }));
var declaration = lodash.get(tsConfigJson, "compilerOptions.declaration", false);
return __assign({ module: typescript.ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() }));
}

function parseTsConfig(tsconfig, context, pluginOptions) {
Expand All @@ -464,7 +465,7 @@ function parseTsConfig(tsconfig, context, pluginOptions) {
printDiagnostics(context, convertDiagnostic("config", [result.error]));
throw new Error("failed to parse " + fileName);
}
return typescript.parseJsonConfigFileContent(result.config, typescript.sys, path.dirname(fileName), getOptionsOverrides(pluginOptions), fileName);
return typescript.parseJsonConfigFileContent(result.config, typescript.sys, path.dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName);
}

// The injected id for helpers.
Expand Down Expand Up @@ -512,6 +513,7 @@ function typescript$1(options) {
abortOnError: true,
rollupCommonJSResolveHack: false,
tsconfig: "tsconfig.json",
useTsconfigDeclarationDir: false,
});
return {
options: function (config) {
Expand Down Expand Up @@ -629,14 +631,17 @@ function typescript$1(options) {
var baseDeclarationDir = parsedConfig.options.outDir;
lodash.each(declarations, function (_a) {
var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark;
var writeToPath;
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
// use the path provided by Typescript's LanguageService.
if (dest == null || pluginOptions.useTsconfigDeclarationDir)
return typescript.sys.writeFile(name, text, writeByteOrderMark);
// Otherwise, take the directory name from the path and make sure it is absolute.
var destDirname = path.dirname(dest);
var destDirectory = path.isAbsolute(dest) ? destDirname : path.join(process.cwd(), destDirname);
var writeToPath = path.join(destDirectory, path.relative(baseDeclarationDir, name));
if (!dest || pluginOptions.useTsconfigDeclarationDir)
writeToPath = name;
else {
// Otherwise, take the directory name from the path and make sure it is absolute.
var destDirname = path.dirname(dest);
var destDirectory = path.isAbsolute(dest) ? destDirname : path.join(process.cwd(), destDirname);
writeToPath = path.join(destDirectory, path.relative(baseDeclarationDir, name));
}
// Write the declaration file to disk.
typescript.sys.writeFile(writeToPath, text, writeByteOrderMark);
});
Expand Down
23 changes: 14 additions & 9 deletions dist/rollup-plugin-typescript2.es.js
Expand Up @@ -448,9 +448,10 @@ function printDiagnostics(context, diagnostics) {
});
}

function getOptionsOverrides(_a) {
function getOptionsOverrides(_a, tsConfigJson) {
var useTsconfigDeclarationDir = _a.useTsconfigDeclarationDir;
return __assign({ module: ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() }));
var declaration = get(tsConfigJson, "compilerOptions.declaration", false);
return __assign({ module: ModuleKind.ES2015, noEmitHelpers: true, importHelpers: true, noResolve: false, outDir: process.cwd() }, (!declaration || useTsconfigDeclarationDir ? {} : { declarationDir: process.cwd() }));
}

function parseTsConfig(tsconfig, context, pluginOptions) {
Expand All @@ -463,7 +464,7 @@ function parseTsConfig(tsconfig, context, pluginOptions) {
printDiagnostics(context, convertDiagnostic("config", [result.error]));
throw new Error("failed to parse " + fileName);
}
return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions), fileName);
return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName);
}

// The injected id for helpers.
Expand Down Expand Up @@ -511,6 +512,7 @@ function typescript$1(options) {
abortOnError: true,
rollupCommonJSResolveHack: false,
tsconfig: "tsconfig.json",
useTsconfigDeclarationDir: false,
});
return {
options: function (config) {
Expand Down Expand Up @@ -628,14 +630,17 @@ function typescript$1(options) {
var baseDeclarationDir = parsedConfig.options.outDir;
each(declarations, function (_a) {
var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark;
var writeToPath;
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
// use the path provided by Typescript's LanguageService.
if (dest == null || pluginOptions.useTsconfigDeclarationDir)
return sys.writeFile(name, text, writeByteOrderMark);
// Otherwise, take the directory name from the path and make sure it is absolute.
var destDirname = dirname(dest);
var destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname);
var writeToPath = join(destDirectory, relative(baseDeclarationDir, name));
if (!dest || pluginOptions.useTsconfigDeclarationDir)
writeToPath = name;
else {
// Otherwise, take the directory name from the path and make sure it is absolute.
var destDirname = dirname(dest);
var destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname);
writeToPath = join(destDirectory, relative(baseDeclarationDir, name));
}
// Write the declaration file to disk.
sys.writeFile(writeToPath, text, writeByteOrderMark);
});
Expand Down
1 change: 1 addition & 0 deletions rollup.config.js
Expand Up @@ -7,6 +7,7 @@ export default {

external: [
'path',
'fs',
'fs-extra',
'object-assign',
'rollup-pluginutils',
Expand Down
11 changes: 7 additions & 4 deletions src/get-options-overrides.ts
@@ -1,13 +1,16 @@
import {CompilerOptions, ModuleKind} from "typescript";
import {IOptions} from "./ioptions";
import { CompilerOptions, ModuleKind } from "typescript";
import { IOptions } from "./ioptions";
import { get } from "lodash";

export function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions): CompilerOptions {
export function getOptionsOverrides({ useTsconfigDeclarationDir }: IOptions, tsConfigJson?: any): CompilerOptions
{
const declaration = get(tsConfigJson, "compilerOptions.declaration", false);
return {
module: ModuleKind.ES2015,
noEmitHelpers: true,
importHelpers: true,
noResolve: false,
outDir: process.cwd(),
...(useTsconfigDeclarationDir ? {} : {declarationDir: process.cwd()}),
...(!declaration || useTsconfigDeclarationDir ? {} : {declarationDir: process.cwd()}),
};
}
18 changes: 12 additions & 6 deletions src/index.ts
Expand Up @@ -51,6 +51,7 @@ export default function typescript(options?: Partial<IOptions>)
abortOnError: true,
rollupCommonJSResolveHack: false,
tsconfig: "tsconfig.json",
useTsconfigDeclarationDir: false,
});

return {
Expand Down Expand Up @@ -235,17 +236,22 @@ export default function typescript(options?: Partial<IOptions>)

onwrite({dest}: IRollupOptions)
{
const baseDeclarationDir = parsedConfig.options.outDir as string;
const baseDeclarationDir = parsedConfig.options.outDir;
each(declarations, ({ name, text, writeByteOrderMark }) =>
{
let writeToPath: string;
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
// use the path provided by Typescript's LanguageService.
if (dest == null || pluginOptions.useTsconfigDeclarationDir) return sys.writeFile(name, text, writeByteOrderMark);
if (!dest || pluginOptions.useTsconfigDeclarationDir)
writeToPath = name;
else
{
// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = dirname(dest);
const destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname);
writeToPath = join(destDirectory, relative(baseDeclarationDir!, name));
}

// Otherwise, take the directory name from the path and make sure it is absolute.
const destDirname = dirname(dest);
const destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname);
const writeToPath = join(destDirectory, relative(baseDeclarationDir, name));
// Write the declaration file to disk.
sys.writeFile(writeToPath, text, writeByteOrderMark);
});
Expand Down
3 changes: 2 additions & 1 deletion src/irollup-options.ts
@@ -1,3 +1,4 @@
export interface IRollupOptions {
export interface IRollupOptions
{
dest?: string;
}
5 changes: 3 additions & 2 deletions src/parse-ts-config.ts
Expand Up @@ -6,7 +6,8 @@ import {convertDiagnostic} from "./tscache";
import {getOptionsOverrides} from "./get-options-overrides";
import {IOptions} from "./ioptions";

export function parseTsConfig(tsconfig: string, context: IContext, pluginOptions: IOptions): ParsedCommandLine {
export function parseTsConfig(tsconfig: string, context: IContext, pluginOptions: IOptions): ParsedCommandLine
{
const fileName = findConfigFile(process.cwd(), sys.fileExists, tsconfig);

if (!fileName)
Expand All @@ -20,5 +21,5 @@ export function parseTsConfig(tsconfig: string, context: IContext, pluginOptions
throw new Error(`failed to parse ${fileName}`);
}

return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions), fileName);
return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName);
}
4 changes: 1 addition & 3 deletions src/partial.ts
@@ -1,3 +1 @@
export declare type Partial<T> = {
[P in keyof T]?: T[P];
};
export declare type Partial<T> = { [P in keyof T]?: T[P]; };
3 changes: 1 addition & 2 deletions tsconfig.json
Expand Up @@ -15,8 +15,7 @@
"strictNullChecks": true,
"forceConsistentCasingInFileNames": true,
"noImplicitReturns": true,
"declaration": true,
"declarationDir": "./dist"
"declaration": true
},
"include": [
"src/**/*.ts"
Expand Down

0 comments on commit c501e1a

Please sign in to comment.