Clean up browserify task, remove unused 'debugMode.js' script

This commit is contained in:
Ron Buckton
2018-06-25 16:21:28 -07:00
parent 6cd27a3217
commit 5ed96ec072
10 changed files with 213 additions and 208 deletions

120
scripts/build/browserify.js Normal file
View File

@@ -0,0 +1,120 @@
// @ts-check
const Browserify = require("browserify");
const Vinyl = require("vinyl");
const fs = require("fs");
const path = require("path");
const convertMap = require("convert-source-map");
const applySourceMap = require("vinyl-sourcemaps-apply");
const { Transform, Readable } = require("stream");
module.exports = browserify;
/**
* @param {import("browserify").Options} [opts]
*/
function browserify(opts) {
return new Transform({
objectMode: true,
/**
* @param {string | Buffer | File} input
*/
transform(input, _, cb) {
if (typeof input === "string" || Buffer.isBuffer(input)) return cb(new Error("Only Vinyl files are supported."));
try {
const sourceMap = input.sourceMap;
const cwd = input.cwd || process.cwd();
const base = input.base || cwd;
const output = /**@type {File}*/(new Vinyl({ path: input.path, base: input.base }));
const stream = streamFromFile(input);
const b = new Browserify(Object.assign({}, opts, { debug: !!sourceMap, basedir: input.base }));
b.add(stream, { file: input.path, basedir: input.base });
b.bundle((err, contents) => {
if (err) return cb(err);
output.contents = contents;
if (sourceMap) {
output.sourceMap = typeof sourceMap === "string" ? JSON.parse(sourceMap) : sourceMap;
const sourceRoot = output.sourceMap.sourceRoot;
makeAbsoluteSourceMap(cwd, base, output.sourceMap);
const stringContents = contents.toString("utf8");
const newSourceMapConverter = convertMap.fromSource(stringContents);
if (newSourceMapConverter) {
const newSourceMap = newSourceMapConverter.toObject();
makeAbsoluteSourceMap(cwd, base, newSourceMap);
applySourceMap(output, newSourceMap);
makeRelativeSourceMap(cwd, base, sourceRoot, output.sourceMap);
output.contents = new Buffer(convertMap.removeComments(stringContents), "utf8");
}
}
cb(null, output);
});
}
catch (e) {
cb(e);
}
}
});
}
/**
* @param {string | undefined} cwd
* @param {string | undefined} base
* @param {RawSourceMap} sourceMap
*
* @typedef RawSourceMap
* @property {string} version
* @property {string} file
* @property {string} [sourceRoot]
* @property {string[]} sources
* @property {string[]} [sourcesContents]
* @property {string} mappings
* @property {string[]} [names]
*/
function makeAbsoluteSourceMap(cwd = process.cwd(), base = "", sourceMap) {
const sourceRoot = sourceMap.sourceRoot || "";
const resolvedBase = path.resolve(cwd, base);
const resolvedSourceRoot = path.resolve(resolvedBase, sourceRoot);
sourceMap.file = path.resolve(resolvedBase, sourceMap.file).replace(/\\/g, "/");
sourceMap.sources = sourceMap.sources.map(source => path.resolve(resolvedSourceRoot, source).replace(/\\/g, "/"));
sourceMap.sourceRoot = "";
}
/**
* @param {string | undefined} cwd
* @param {string | undefined} base
* @param {string} sourceRoot
* @param {RawSourceMap} sourceMap
*/
function makeRelativeSourceMap(cwd = process.cwd(), base = "", sourceRoot, sourceMap) {
makeAbsoluteSourceMap(cwd, base, sourceMap);
const resolvedBase = path.resolve(cwd, base);
const resolvedSourceRoot = path.resolve(resolvedBase, sourceRoot);
sourceMap.file = path.relative(resolvedBase, sourceMap.file).replace(/\\/g, "/");
sourceMap.sources = sourceMap.sources.map(source => path.relative(resolvedSourceRoot, source).replace(/\\/g, "/"));
sourceMap.sourceRoot = sourceRoot;
}
/**
* @param {File} file
*/
function streamFromFile(file) {
return file.isBuffer() ? streamFromBuffer(file.contents) :
file.isStream() ? file.contents :
fs.createReadStream(file.path, { autoClose: true });
}
/**
* @param {Buffer} buffer
*/
function streamFromBuffer(buffer) {
return new Readable({
read() {
this.push(buffer);
this.push(null);
}
});
}
/**
* @typedef {import("vinyl") & { sourceMap?: any }} File
*/
void 0;

View File

@@ -1,7 +0,0 @@
// @ts-check
const gulp = require("./gulp");
exports.useDebugMode = true;
gulp.task("useDebugMode", /*help*/ false, [], (done) => { exports["useDebugMode"] = true; done(); });
gulp.task("dontUseDebugMode", /*help*/ false, [], (done) => { exports["useDebugMode"] = false; done(); });

View File

@@ -35,32 +35,28 @@ function rm(dest, opts) {
*/
write(file, _, cb) {
if (failed) return;
if (Vinyl.isVinyl(file)) {
const basePath = typeof dest === "string" ? path.resolve(cwd, dest) :
typeof dest === "function" ? path.resolve(cwd, dest(file)) :
file.base;
const filePath = path.resolve(basePath, file.relative);
file.cwd = cwd;
file.base = basePath;
file.path = filePath;
const entry = {
file,
deleted: false,
cb,
promise: del(file.path).then(() => {
entry.deleted = true;
processDeleted();
}, err => {
failed = true;
pending.length = 0;
cb(err);
})
};
pending.push(entry);
}
else {
cb(new Error("Only Vinyl files are supported."));
}
if (typeof file === "string" || Buffer.isBuffer(file)) return cb(new Error("Only Vinyl files are supported."));
const basePath = typeof dest === "string" ? path.resolve(cwd, dest) :
typeof dest === "function" ? path.resolve(cwd, dest(file)) :
file.base;
const filePath = path.resolve(basePath, file.relative);
file.cwd = cwd;
file.base = basePath;
file.path = filePath;
const entry = {
file,
deleted: false,
cb,
promise: del(file.path).then(() => {
entry.deleted = true;
processDeleted();
}, err => {
failed = true;
pending.length = 0;
cb(err);
})
};
pending.push(entry);
},
final(cb) {
processDeleted();

View File

@@ -39,10 +39,9 @@ function upToDate(parsedProject, options) {
* @param {string|Buffer|File} file
*/
write(file, _, cb) {
if (Vinyl.isVinyl(file)) {
inputs.push(file);
inputMap.set(path.resolve(file.path), file);
}
if (typeof file === "string" || Buffer.isBuffer(file)) return cb(new Error("Only Vinyl files are supported."));
inputs.push(file);
inputMap.set(path.resolve(file.path), file);
cb();
},
final(cb) {

View File

@@ -17,7 +17,6 @@ async function produceLKG() {
await copyLocalizedDiagnostics();
await buildProtocol();
await copyScriptOutputs();
await buildTsc();
await copyDeclarationOutputs();
await writeGitAttributes();
}
@@ -53,7 +52,7 @@ async function buildProtocol() {
async function copyScriptOutputs() {
await copyWithCopyright("cancellationToken.js");
await copyWithCopyright("tsc.js");
await copyWithCopyright("tsc.release.js", "tsc.js");
await copyWithCopyright("tsserver.js");
await copyWithCopyright("typescript.js");
await copyWithCopyright("typescriptServices.js");
@@ -61,10 +60,6 @@ async function copyScriptOutputs() {
await copyWithCopyright("watchGuard.js");
}
async function buildTsc() {
await exec(path.join(source, "tsc.js"), [`-b -f ${path.join(root, "src/tsc/tsconfig.release.json")}`]);
}
async function copyDeclarationOutputs() {
await copyWithCopyright("tsserverlibrary.d.ts");
await copyWithCopyright("typescript.d.ts");
@@ -75,9 +70,9 @@ async function writeGitAttributes() {
await fs.writeFile(path.join(dest, ".gitattributes"), `* text eol=lf`, "utf-8");
}
async function copyWithCopyright(fileName: string) {
async function copyWithCopyright(fileName: string, destName = fileName) {
const content = await fs.readFile(path.join(source, fileName), "utf-8");
await fs.writeFile(path.join(dest, fileName), copyright + "\n" + content);
await fs.writeFile(path.join(dest, destName), copyright + "\n" + content);
}
async function copyFromBuiltLocal(fileName: string) {