Skip to content

Commit c501e1a

Browse files
committed
- fix for TS5052
- cleanup
1 parent c72a610 commit c501e1a

10 files changed

+56
-37
lines changed

dist/get-options-overrides.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
import { CompilerOptions } from "typescript";
22
import { IOptions } from "./ioptions";
3-
export declare function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions): CompilerOptions;
3+
export declare function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions, tsConfigJson?: any): CompilerOptions;

dist/rollup-plugin-typescript2.cjs.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -449,9 +449,10 @@ function printDiagnostics(context, diagnostics) {
449449
});
450450
}
451451

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

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

470471
// The injected id for helpers.
@@ -512,6 +513,7 @@ function typescript$1(options) {
512513
abortOnError: true,
513514
rollupCommonJSResolveHack: false,
514515
tsconfig: "tsconfig.json",
516+
useTsconfigDeclarationDir: false,
515517
});
516518
return {
517519
options: function (config) {
@@ -629,14 +631,17 @@ function typescript$1(options) {
629631
var baseDeclarationDir = parsedConfig.options.outDir;
630632
lodash.each(declarations, function (_a) {
631633
var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark;
634+
var writeToPath;
632635
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
633636
// use the path provided by Typescript's LanguageService.
634-
if (dest == null || pluginOptions.useTsconfigDeclarationDir)
635-
return typescript.sys.writeFile(name, text, writeByteOrderMark);
636-
// Otherwise, take the directory name from the path and make sure it is absolute.
637-
var destDirname = path.dirname(dest);
638-
var destDirectory = path.isAbsolute(dest) ? destDirname : path.join(process.cwd(), destDirname);
639-
var writeToPath = path.join(destDirectory, path.relative(baseDeclarationDir, name));
637+
if (!dest || pluginOptions.useTsconfigDeclarationDir)
638+
writeToPath = name;
639+
else {
640+
// Otherwise, take the directory name from the path and make sure it is absolute.
641+
var destDirname = path.dirname(dest);
642+
var destDirectory = path.isAbsolute(dest) ? destDirname : path.join(process.cwd(), destDirname);
643+
writeToPath = path.join(destDirectory, path.relative(baseDeclarationDir, name));
644+
}
640645
// Write the declaration file to disk.
641646
typescript.sys.writeFile(writeToPath, text, writeByteOrderMark);
642647
});

dist/rollup-plugin-typescript2.es.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,10 @@ function printDiagnostics(context, diagnostics) {
448448
});
449449
}
450450

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

456457
function parseTsConfig(tsconfig, context, pluginOptions) {
@@ -463,7 +464,7 @@ function parseTsConfig(tsconfig, context, pluginOptions) {
463464
printDiagnostics(context, convertDiagnostic("config", [result.error]));
464465
throw new Error("failed to parse " + fileName);
465466
}
466-
return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions), fileName);
467+
return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName);
467468
}
468469

469470
// The injected id for helpers.
@@ -511,6 +512,7 @@ function typescript$1(options) {
511512
abortOnError: true,
512513
rollupCommonJSResolveHack: false,
513514
tsconfig: "tsconfig.json",
515+
useTsconfigDeclarationDir: false,
514516
});
515517
return {
516518
options: function (config) {
@@ -628,14 +630,17 @@ function typescript$1(options) {
628630
var baseDeclarationDir = parsedConfig.options.outDir;
629631
each(declarations, function (_a) {
630632
var name = _a.name, text = _a.text, writeByteOrderMark = _a.writeByteOrderMark;
633+
var writeToPath;
631634
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
632635
// use the path provided by Typescript's LanguageService.
633-
if (dest == null || pluginOptions.useTsconfigDeclarationDir)
634-
return sys.writeFile(name, text, writeByteOrderMark);
635-
// Otherwise, take the directory name from the path and make sure it is absolute.
636-
var destDirname = dirname(dest);
637-
var destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname);
638-
var writeToPath = join(destDirectory, relative(baseDeclarationDir, name));
636+
if (!dest || pluginOptions.useTsconfigDeclarationDir)
637+
writeToPath = name;
638+
else {
639+
// Otherwise, take the directory name from the path and make sure it is absolute.
640+
var destDirname = dirname(dest);
641+
var destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname);
642+
writeToPath = join(destDirectory, relative(baseDeclarationDir, name));
643+
}
639644
// Write the declaration file to disk.
640645
sys.writeFile(writeToPath, text, writeByteOrderMark);
641646
});

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ export default {
77

88
external: [
99
'path',
10+
'fs',
1011
'fs-extra',
1112
'object-assign',
1213
'rollup-pluginutils',

src/get-options-overrides.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
1-
import {CompilerOptions, ModuleKind} from "typescript";
2-
import {IOptions} from "./ioptions";
1+
import { CompilerOptions, ModuleKind } from "typescript";
2+
import { IOptions } from "./ioptions";
3+
import { get } from "lodash";
34

4-
export function getOptionsOverrides({useTsconfigDeclarationDir}: IOptions): CompilerOptions {
5+
export function getOptionsOverrides({ useTsconfigDeclarationDir }: IOptions, tsConfigJson?: any): CompilerOptions
6+
{
7+
const declaration = get(tsConfigJson, "compilerOptions.declaration", false);
58
return {
69
module: ModuleKind.ES2015,
710
noEmitHelpers: true,
811
importHelpers: true,
912
noResolve: false,
1013
outDir: process.cwd(),
11-
...(useTsconfigDeclarationDir ? {} : {declarationDir: process.cwd()}),
14+
...(!declaration || useTsconfigDeclarationDir ? {} : {declarationDir: process.cwd()}),
1215
};
1316
}

src/index.ts

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ export default function typescript(options?: Partial<IOptions>)
5151
abortOnError: true,
5252
rollupCommonJSResolveHack: false,
5353
tsconfig: "tsconfig.json",
54+
useTsconfigDeclarationDir: false,
5455
});
5556

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

236237
onwrite({dest}: IRollupOptions)
237238
{
238-
const baseDeclarationDir = parsedConfig.options.outDir as string;
239+
const baseDeclarationDir = parsedConfig.options.outDir;
239240
each(declarations, ({ name, text, writeByteOrderMark }) =>
240241
{
242+
let writeToPath: string;
241243
// If for some reason no 'dest' property exists or if 'useTsconfigDeclarationDir' is given in the plugin options,
242244
// use the path provided by Typescript's LanguageService.
243-
if (dest == null || pluginOptions.useTsconfigDeclarationDir) return sys.writeFile(name, text, writeByteOrderMark);
245+
if (!dest || pluginOptions.useTsconfigDeclarationDir)
246+
writeToPath = name;
247+
else
248+
{
249+
// Otherwise, take the directory name from the path and make sure it is absolute.
250+
const destDirname = dirname(dest);
251+
const destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname);
252+
writeToPath = join(destDirectory, relative(baseDeclarationDir!, name));
253+
}
244254

245-
// Otherwise, take the directory name from the path and make sure it is absolute.
246-
const destDirname = dirname(dest);
247-
const destDirectory = isAbsolute(dest) ? destDirname : join(process.cwd(), destDirname);
248-
const writeToPath = join(destDirectory, relative(baseDeclarationDir, name));
249255
// Write the declaration file to disk.
250256
sys.writeFile(writeToPath, text, writeByteOrderMark);
251257
});

src/irollup-options.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1-
export interface IRollupOptions {
1+
export interface IRollupOptions
2+
{
23
dest?: string;
34
}

src/parse-ts-config.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@ import {convertDiagnostic} from "./tscache";
66
import {getOptionsOverrides} from "./get-options-overrides";
77
import {IOptions} from "./ioptions";
88

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

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

23-
return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions), fileName);
24+
return parseJsonConfigFileContent(result.config, sys, dirname(fileName), getOptionsOverrides(pluginOptions, result.config), fileName);
2425
}

src/partial.ts

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1 @@
1-
export declare type Partial<T> = {
2-
[P in keyof T]?: T[P];
3-
};
1+
export declare type Partial<T> = { [P in keyof T]?: T[P]; };

tsconfig.json

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@
1515
"strictNullChecks": true,
1616
"forceConsistentCasingInFileNames": true,
1717
"noImplicitReturns": true,
18-
"declaration": true,
19-
"declarationDir": "./dist"
18+
"declaration": true
2019
},
2120
"include": [
2221
"src/**/*.ts"

0 commit comments

Comments
 (0)