Skip to content

Commit

Permalink
- fix for #24. forcing declarations for project files even if they ar…
Browse files Browse the repository at this point in the history
…e ignored by rollup
  • Loading branch information
ezolenko committed Aug 15, 2017
1 parent 8122820 commit 448913d
Show file tree
Hide file tree
Showing 12 changed files with 181 additions and 129 deletions.
1 change: 0 additions & 1 deletion dist/host.d.ts
Expand Up @@ -20,5 +20,4 @@ export declare class LanguageServiceHost implements tsTypes.LanguageServiceHost
getTypeRootsVersion(): number;
directoryExists(directoryName: string): boolean;
getDirectories(directoryName: string): string[];
private normalize(fileName);
}
1 change: 1 addition & 0 deletions dist/normalize.d.ts
@@ -0,0 +1 @@
export declare function normalize(fileName: string): string;
112 changes: 64 additions & 48 deletions dist/rollup-plugin-typescript2.cjs.js

Large diffs are not rendered by default.

65 changes: 41 additions & 24 deletions dist/rollup-plugin-typescript2.es.js
@@ -1,5 +1,6 @@
/* eslint-disable */
import { concat, defaults, each, endsWith, filter, find, get, has, isEqual, isFunction, map, some } from 'lodash';
import * as _ from 'lodash';
import { existsSync, readFileSync, readdirSync, renameSync } from 'fs';
import { Graph, alg } from 'graphlib';
import { sha1 } from 'object-hash';
Expand Down Expand Up @@ -119,6 +120,10 @@ function setTypescriptModule(override) {
tsModule = override;
}

function normalize(fileName) {
return fileName.split("\\").join("/");
}

var LanguageServiceHost = (function () {
function LanguageServiceHost(parsedConfig) {
this.parsedConfig = parsedConfig;
Expand All @@ -131,14 +136,14 @@ var LanguageServiceHost = (function () {
this.versions = {};
};
LanguageServiceHost.prototype.setSnapshot = function (fileName, data) {
fileName = this.normalize(fileName);
fileName = normalize(fileName);
var snapshot = tsModule.ScriptSnapshot.fromString(data);
this.snapshots[fileName] = snapshot;
this.versions[fileName] = (this.versions[fileName] || 0) + 1;
return snapshot;
};
LanguageServiceHost.prototype.getScriptSnapshot = function (fileName) {
fileName = this.normalize(fileName);
fileName = normalize(fileName);
if (has(this.snapshots, fileName))
return this.snapshots[fileName];
if (existsSync(fileName)) {
Expand All @@ -152,7 +157,7 @@ var LanguageServiceHost = (function () {
return this.cwd;
};
LanguageServiceHost.prototype.getScriptVersion = function (fileName) {
fileName = this.normalize(fileName);
fileName = normalize(fileName);
return (this.versions[fileName] || 0).toString();
};
LanguageServiceHost.prototype.getScriptFileNames = function () {
Expand Down Expand Up @@ -185,9 +190,6 @@ var LanguageServiceHost = (function () {
LanguageServiceHost.prototype.getDirectories = function (directoryName) {
return tsModule.sys.getDirectories(directoryName);
};
LanguageServiceHost.prototype.normalize = function (fileName) {
return fileName.split("\\").join("/");
};
return LanguageServiceHost;
}());

Expand Down Expand Up @@ -607,7 +609,9 @@ function typescript(options) {
printDiagnostics(contextWrapper, diagnostics);
}
if (result && result.dts) {
declarations[result.dts.name] = result.dts;
var key = normalize(id);
declarations[key] = result.dts;
context.debug(blue("generated declarations") + " for '" + key + "'");
result.dts = undefined;
}
return result;
Expand All @@ -633,23 +637,36 @@ function typescript(options) {
},
onwrite: function (_a) {
var dest = _a.dest;
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 || 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.
tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark);
});
if (parsedConfig.options.declaration) {
each(parsedConfig.fileNames, function (name) {
var key = normalize(name);
if (has(declarations, key) || !filter$$1(key))
return;
context.debug("generating missed declarations for '" + key + "'");
var output = service.getEmitOutput(key, true);
var dts = find(output.outputFiles, function (entry) { return endsWith(entry.name, ".d.ts"); });
if (dts)
declarations[key] = dts;
});
var baseDeclarationDir_1 = parsedConfig.options.outDir;
each(declarations, function (_a, key) {
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 || 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_1, name));
}
context.debug(blue("writing declarations") + " for '" + key + "' to '" + writeToPath + "'");
// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark);
});
}
},
};
}
Expand Down
4 changes: 2 additions & 2 deletions src/get-options-overrides.ts
@@ -1,11 +1,11 @@
import { tsModule } from "./tsproxy";
import * as tsTypes from "typescript";
import { IOptions } from "./ioptions";
import { get } from "lodash";
import * as _ from "lodash";

export function getOptionsOverrides({ useTsconfigDeclarationDir }: IOptions, tsConfigJson?: any): tsTypes.CompilerOptions
{
const declaration = get(tsConfigJson, "compilerOptions.declaration", false);
const declaration = _.get(tsConfigJson, "compilerOptions.declaration", false);
return {
module: tsModule.ModuleKind.ES2015,
noEmitHelpers: true,
Expand Down
17 changes: 6 additions & 11 deletions src/host.ts
@@ -1,8 +1,8 @@

import { tsModule } from "./tsproxy";
import * as tsTypes from "typescript";
import { existsSync } from "fs";
import { has } from "lodash";
import * as _ from "lodash";
import { normalize } from "./normalize";

export class LanguageServiceHost implements tsTypes.LanguageServiceHost
{
Expand All @@ -22,7 +22,7 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost

public setSnapshot(fileName: string, data: string): tsTypes.IScriptSnapshot
{
fileName = this.normalize(fileName);
fileName = normalize(fileName);

const snapshot = tsModule.ScriptSnapshot.fromString(data);
this.snapshots[fileName] = snapshot;
Expand All @@ -32,9 +32,9 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost

public getScriptSnapshot(fileName: string): tsTypes.IScriptSnapshot | undefined
{
fileName = this.normalize(fileName);
fileName = normalize(fileName);

if (has(this.snapshots, fileName))
if (_.has(this.snapshots, fileName))
return this.snapshots[fileName];

if (existsSync(fileName))
Expand All @@ -54,7 +54,7 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost

public getScriptVersion(fileName: string)
{
fileName = this.normalize(fileName);
fileName = normalize(fileName);

return (this.versions[fileName] || 0).toString();
}
Expand Down Expand Up @@ -108,9 +108,4 @@ export class LanguageServiceHost implements tsTypes.LanguageServiceHost
{
return tsModule.sys.getDirectories(directoryName);
}

private normalize(fileName: string)
{
return fileName.split("\\").join("/");
}
}
76 changes: 48 additions & 28 deletions src/index.ts
Expand Up @@ -5,7 +5,7 @@ import { TsCache, convertDiagnostic, ICode } from "./tscache";
import { tsModule, setTypescriptModule } from "./tsproxy";
import * as tsTypes from "typescript";
import * as resolve from "resolve";
import { defaults, endsWith, concat, find, isFunction, get, each } from "lodash";
import * as _ from "lodash";
import { IRollupOptions } from "./irollup-options";
import { IOptions } from "./ioptions";
import { Partial } from "./partial";
Expand All @@ -14,6 +14,7 @@ import { printDiagnostics } from "./print-diagnostics";
import { TSLIB, tslibSource } from "./tslib";
import { blue, red, yellow } from "colors/safe";
import { join, relative, dirname, isAbsolute } from "path";
import { normalize } from "./normalize";

export default function typescript(options?: Partial<IOptions>)
{
Expand Down Expand Up @@ -42,7 +43,7 @@ export default function typescript(options?: Partial<IOptions>)

const pluginOptions = { ...options } as IOptions;

defaults(pluginOptions,
_.defaults(pluginOptions,
{
check: true,
verbosity: VerbosityLevel.Warning,
Expand Down Expand Up @@ -105,7 +106,7 @@ export default function typescript(options?: Partial<IOptions>)
if (filter(result.resolvedModule.resolvedFileName))
cache().setDependency(result.resolvedModule.resolvedFileName, importer);

if (endsWith(result.resolvedModule.resolvedFileName, ".d.ts"))
if (_.endsWith(result.resolvedModule.resolvedFileName, ".d.ts"))
return null;

const resolved = pluginOptions.rollupCommonJSResolveHack
Expand Down Expand Up @@ -148,7 +149,7 @@ export default function typescript(options?: Partial<IOptions>)
noErrors = false;

// always checking on fatal errors, even if options.check is set to false
const diagnostics = concat(
const diagnostics = _.concat(
cache().getSyntacticDiagnostics(id, snapshot, () =>
{
return service.getSyntacticDiagnostics(id);
Expand All @@ -162,13 +163,13 @@ export default function typescript(options?: Partial<IOptions>)

// since no output was generated, aborting compilation
cache().done();
if (isFunction(this.error))
if (_.isFunction(this.error))
this.error(red(`failed to transpile '${id}'`));
}

const transpiled = find(output.outputFiles, (entry) => endsWith(entry.name, ".js") || endsWith(entry.name, ".jsx"));
const map = find(output.outputFiles, (entry) => endsWith(entry.name, ".map"));
const dts = find(output.outputFiles, (entry) => endsWith(entry.name, ".d.ts"));
const transpiled = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".js") || _.endsWith(entry.name, ".jsx"));
const map = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".map"));
const dts = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".d.ts"));

return {
code: transpiled ? transpiled.text : undefined,
Expand All @@ -179,7 +180,7 @@ export default function typescript(options?: Partial<IOptions>)

if (pluginOptions.check)
{
const diagnostics = concat(
const diagnostics = _.concat(
cache().getSyntacticDiagnostics(id, snapshot, () =>
{
return service.getSyntacticDiagnostics(id);
Expand All @@ -198,7 +199,9 @@ export default function typescript(options?: Partial<IOptions>)

if (result && result.dts)
{
declarations[result.dts.name] = result.dts;
const key = normalize(id);
declarations[key] = result.dts;
context.debug(`${blue("generated declarations")} for '${key}'`);
result.dts = undefined;
}

Expand All @@ -207,7 +210,7 @@ export default function typescript(options?: Partial<IOptions>)

ongenerate(bundleOptions: any): void
{
targetCount = get(bundleOptions, "targets.length", 1);
targetCount = _.get(bundleOptions, "targets.length", 1);

if (round >= targetCount) // ongenerate() is called for each target
{
Expand All @@ -222,7 +225,7 @@ export default function typescript(options?: Partial<IOptions>)

cache().walkTree((id) =>
{
const diagnostics = concat(
const diagnostics = _.concat(
convertDiagnostic("syntax", service.getSyntacticDiagnostics(id)),
convertDiagnostic("semantic", service.getSemanticDiagnostics(id)),
);
Expand All @@ -241,25 +244,42 @@ export default function typescript(options?: Partial<IOptions>)

onwrite({ dest }: IRollupOptions)
{
const baseDeclarationDir = parsedConfig.options.outDir;
each(declarations, ({ name, text, writeByteOrderMark }) =>
if (parsedConfig.options.declaration)
{
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 || pluginOptions.useTsconfigDeclarationDir)
writeToPath = name;
else
_.each(parsedConfig.fileNames, (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);
writeToPath = join(destDirectory, relative(baseDeclarationDir!, name));
}
const key = normalize(name);
if (_.has(declarations, key) || !filter(key))
return;
context.debug(`generating missed declarations for '${key}'`);
const output = service.getEmitOutput(key, true);
const dts = _.find(output.outputFiles, (entry) => _.endsWith(entry.name, ".d.ts"));
if (dts)
declarations[key] = dts;
});

// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark);
});
const baseDeclarationDir = parsedConfig.options.outDir;
_.each(declarations, ({ name, text, writeByteOrderMark }, key) =>
{
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 || 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));
}

context.debug(`${blue("writing declarations")} for '${key}' to '${writeToPath}'`);

// Write the declaration file to disk.
tsModule.sys.writeFile(writeToPath, text, writeByteOrderMark);
});
}
},
};
}
4 changes: 4 additions & 0 deletions src/normalize.ts
@@ -0,0 +1,4 @@
export function normalize(fileName: string)
{
return fileName.split("\\").join("/");
}
4 changes: 2 additions & 2 deletions src/print-diagnostics.ts
@@ -1,12 +1,12 @@
import { tsModule } from "./tsproxy";
import { red, white, yellow } from "colors/safe";
import { each } from "lodash";
import { IContext } from "./context";
import { IDiagnostics } from "./tscache";
import * as _ from "lodash";

export function printDiagnostics(context: IContext, diagnostics: IDiagnostics[]): void
{
each(diagnostics, (diagnostic) =>
_.each(diagnostics, (diagnostic) =>
{
let print;
let color;
Expand Down
4 changes: 2 additions & 2 deletions src/rollingcache.ts
@@ -1,7 +1,7 @@
import { ICache } from "./icache";
import { emptyDirSync, ensureFileSync, readJsonSync, removeSync, writeJsonSync } from "fs-extra";
import { existsSync, readdirSync, renameSync } from "fs";
import { isEqual } from "lodash";
import * as _ from "lodash";

/**
* Saves data in new cache folder or reads it from old one.
Expand Down Expand Up @@ -56,7 +56,7 @@ export class RollingCache<DataType> implements ICache<DataType>
if (!existsSync(this.oldCacheRoot))
return names.length === 0; // empty folder matches

return isEqual(readdirSync(this.oldCacheRoot).sort(), names.sort());
return _.isEqual(readdirSync(this.oldCacheRoot).sort(), names.sort());
}

/**
Expand Down

0 comments on commit 448913d

Please sign in to comment.