@wrench/roll-typescript
Version:
plugin for bundling TypeScript with support of modular output and declaration bundle
205 lines (174 loc) • 6.5 kB
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
const lodash_1 = require("lodash");
const path_1 = require("path");
const host_1 = require("../host");
const util_1 = require("../util");
const NULL = {
version: "0",
};
class Project {
constructor(props) {
this.scripts = {};
util_1.rebind(Object.assign(this, props), props);
initialize.call(this, Project.prototype, util_1.bindToSelf);
}
fork(props) {
const host = Object.assign(Object.create(this), props);
host.options = Object.assign(Object.assign({}, this.options), props.options);
initialize.call(host, this, util_1.rebind);
host.dirty = true;
return host;
}
setFileNames(fileNames, canonical) {
if (!canonical)
fileNames = fileNames.map(this.getCanonicalFileName);
this.fileNames = fileNames;
this.commonSourceDirectory = host_1.computeCommonSourceDirectoryOfFilenames(this, this.fileNames);
this.dirty = true;
}
getProgram(checkDirty) {
if (checkDirty)
this.checkProgramDirty();
if (this.dirty || !this.program) {
this.program = this.ts.createProgram({
host: this,
options: this.options,
rootNames: this.fileNames,
oldProgram: this.program,
});
this.transform = toCustomTransformers(this.transformerFactory, this.program);
this.dirty = false;
}
return this.program;
}
checkProgramDirty() {
if (!this.dirty && this.program)
this.dirty = this.program.getSourceFiles().some(isVersionDiff, this.scripts);
}
updateScript(fileName, text) {
text = text || "";
fileName = this.getCanonicalFileName(fileName);
let script = this.scripts[fileName];
if (script) {
if (script.text !== text) {
script.text = text;
script.version = String(+script.version + 1);
}
}
else {
script = this.ts.ScriptSnapshot.fromString(text);
script.text = text;
script.kind = this.getScriptKind(fileName);
script.version = NULL.version;
this.scripts[fileName] = script;
}
if (!this.dirty && this.program) {
const file = this.program.getSourceFile(fileName);
this.dirty = file && file.version !== script.version;
}
return script;
}
reportDiagnostic(diagnostic) {
util_1.reportDiagnosticByConsole(diagnostic, this);
}
getSourceFile(fileName, target, onError = throwError) {
return this.getSourceFileByPath(fileName, fileName, target, onError);
}
getCommonSourceDirectory() {
return this.commonSourceDirectory;
}
getSourceFileByPath(fileName, path, target, onError = throwError) {
const script = this.getScriptSnapshot(fileName);
if (script) {
const key = host_1.getKeyForCompilationSettings(this, target);
return this.documentRegistry.updateDocumentWithKey(fileName, path, this.options, key, script, script.version, script.kind);
}
}
getScriptKind(fileName) {
return host_1.getScriptKind(this, fileName);
}
getNewLine() { return this.newLine; }
getCurrentDirectory() { return this.currentDirectory; }
getCanonicalFileName(fileName) {
return util_1.canonical(fileName, this.caseSensitiveFileNames);
}
;
getCompilationSettings() {
return this.options;
}
getScriptFileNames() {
return Object.keys(this.scripts);
}
getScriptVersion(fileName) {
return (this.scripts[fileName] || NULL).version;
}
getScriptSnapshot(fileName) {
return this.scripts[fileName] || this.updateScript(fileName, this.readFile(fileName));
}
getDefaultLibFileName(options) {
return this.ts.getDefaultLibFileName(options);
}
getDefaultLibLocation() {
return this.defaultLibLocation;
}
useCaseSensitiveFileNames() {
return this.caseSensitiveFileNames;
}
fileExists(fileName) {
return this.ts.sys.fileExists(fileName);
}
readFile(fileName) {
return this.ts.sys.readFile(fileName);
}
;
directoryExists(directoryName) {
return this.ts.sys.directoryExists(directoryName);
}
getDirectories(path) {
return this.ts.sys.getDirectories(path);
}
readDirectory(path, extensions, exclude, include, depth) {
return this.ts.sys.readDirectory(path, extensions, exclude, include, depth);
}
realpath(path) {
return this.ts.sys.realpath(path);
}
writeFile(path, data, writeByteOrderMark) {
this.ts.sys.writeFile(path, data, writeByteOrderMark);
}
trace(s) {
return this.ts.sys.write(s + this.newLine);
}
}
exports.Project = Project;
function throwError(message) {
throw new Error(message);
}
function initialize(prototype, binder) {
if (binder && prototype)
binder(this, prototype);
this.scripts = this.scripts || {};
this.fileNames = this.fileNames || [];
this.newLine = this.ts.getNewLineCharacter(this.options);
this.documentRegistry = this.documentRegistry || this.ts.createDocumentRegistry();
this.currentDirectory = this.currentDirectory || this.ts.sys.getCurrentDirectory();
this.defaultLibLocation = this.defaultLibLocation || path_1.dirname(this.ts.sys.getExecutingFilePath());
this.caseSensitiveFileNames = this.caseSensitiveFileNames || this.ts.sys.useCaseSensitiveFileNames;
this.commonSourceDirectory = this.commonSourceDirectory || host_1.computeCommonSourceDirectoryOfFilenames(this, this.fileNames);
this.moduleResolutionCache = this.ts.createModuleResolutionCache(this.currentDirectory, this.getCanonicalFileName, this.options);
}
function isVersionDiff(file) {
return file.version !== (this[file.path] || NULL).version;
}
function toCustomTransformers(factory, program) {
if (factory) {
const transforms = lodash_1.castArray(factory(program));
const conf = lodash_1.omitBy({
before: lodash_1.map(transforms, "before"),
after: lodash_1.map(transforms, "after"),
afterDeclarations: lodash_1.map(transforms, "afterDeclarations"),
});
if (!lodash_1.isEmpty(conf))
return conf;
}
}