turbo-gulp
Version:
Gulp tasks to boost high-quality projects.
93 lines (92 loc) • 3.77 kB
JavaScript
import gulpRename from "gulp-rename";
import gulpSourceMaps from "gulp-sourcemaps";
import gulpTypescript from "gulp-typescript";
import { Incident } from "incident";
import merge from "merge2";
import { posix as posixPath } from "path";
import { OutModules } from "../options/typescript";
import { deleteUndefinedProperties } from "../utils/utils";
import { resolveTsLocations } from "./_typescript";
function hashTypescriptError(error) {
return JSON.stringify({
fileName: error.fullFilename,
code: error.diagnostic.code,
startPosition: error.startPosition,
endPosition: error.endPosition,
});
}
class TypescriptReporter {
constructor(throwOnError) {
this.baseReporter = gulpTypescript.reporter.defaultReporter();
this.reported = new Set();
this.throwOnError = throwOnError;
}
error(error, typescript) {
const hash = hashTypescriptError(error);
if (this.reported.has(hash)) {
return;
}
this.reported.add(hash);
this.baseReporter.error(error, typescript);
}
finish(compilerResult) {
this.baseReporter.finish(compilerResult);
if (this.throwOnError && this.reported.size > 0) {
throw Incident("TypescriptError");
}
}
}
export function getBuildTypescriptTask(gulp, options, throwOnError = true) {
const resolved = resolveTsLocations(options);
const tscOptions = Object.assign({}, options.tscOptions, { rootDir: resolved.rootDir, outDir: resolved.outDir, typeRoots: resolved.typeRoots });
deleteUndefinedProperties(tscOptions);
const task = function () {
let mjsStream;
let jsStream;
let dtsStream;
const srcStream = gulp
.src(resolved.absScripts, { base: options.srcDir })
.pipe(gulpSourceMaps.init());
const reporter = new TypescriptReporter(throwOnError);
// TODO: update type definitions of `gulp-sourcemaps`
const writeSourceMapsOptions = {
sourceRoot: (file /* VinylFile */) => {
return posixPath.relative(posixPath.dirname(file.relative), "");
},
};
if (options.outModules === OutModules.Js) {
const compiledStream = srcStream.pipe(gulpTypescript(tscOptions, reporter));
jsStream = compiledStream.js
.pipe(gulpSourceMaps.write(writeSourceMapsOptions));
dtsStream = compiledStream.dts;
}
else {
const mjsOptions = Object.assign({}, tscOptions, { module: "es2015" });
const compiledStream = srcStream.pipe(gulpTypescript(mjsOptions, reporter));
mjsStream = compiledStream.js
.pipe(gulpSourceMaps.write(writeSourceMapsOptions))
.pipe(gulpRename({ extname: ".mjs" }));
dtsStream = compiledStream.dts;
if (options.outModules === OutModules.Both) {
jsStream = srcStream
.pipe(gulpTypescript(tscOptions, reporter))
.js
.pipe(gulpSourceMaps.write(writeSourceMapsOptions));
}
}
return merge([dtsStream, jsStream, mjsStream].filter(x => x !== undefined))
.pipe(gulp.dest(options.buildDir));
};
task.displayName = "_build:scripts";
return task;
}
export function getBuildTypescriptWatchTask(gulp, options) {
return () => {
const buildTask = getBuildTypescriptTask(gulp, options, false);
const resolved = resolveTsLocations(options);
return gulp.watch(resolved.absScripts, { cwd: options.srcDir }, buildTask);
};
}
export function getBuildTypescriptWatcher(gulp, options) {
return getBuildTypescriptWatchTask(gulp, options)();
}