Skip to content

Commit

Permalink
Refactoring dist build process.
Browse files Browse the repository at this point in the history
  • Loading branch information
ricmoo committed Nov 14, 2020
1 parent e0b1d0e commit 4809325
Show file tree
Hide file tree
Showing 132 changed files with 1,147 additions and 557 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
node_modules/
packages/*/node_modules
packages/*/lib._esm
.package_node_modules/
obsolete/
.DS_Store
Expand Down
16 changes: 1 addition & 15 deletions misc/admin/lib/build.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,6 @@ function setupConfig(outDir, moduleType, targetType) {
if (info._ethers_nobuild) {
return;
}
if (targetType === "es2015") {
if (info["browser.esm"]) {
info.browser = info["browser.esm"];
}
}
else if (targetType === "es5") {
if (info["browser.umd"]) {
info.browser = info["browser.umd"];
}
}
else {
throw new Error("unsupported target");
}
utils_1.saveJson(filename, info, true);
let path = path_1.resolve("packages", dirname, "tsconfig.json");
let content = utils_1.loadJson(path);
content.compilerOptions.outDir = outDir;
Expand All @@ -39,7 +25,7 @@ function setupConfig(outDir, moduleType, targetType) {
}
function setupBuild(buildModule) {
if (buildModule) {
setupConfig("./lib.esm/", "es2015", "es2015");
setupConfig("./lib._esm/", "es2015", "es2015");
}
else {
setupConfig("./lib/", "commonjs", "es5");
Expand Down
1 change: 1 addition & 0 deletions misc/admin/lib/cmds/bundle-testcases.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
62 changes: 62 additions & 0 deletions misc/admin/lib/cmds/bundle-testcases.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
/** bundle-testcases
*
* This sript converts all the testase data (including needed
* text files) into a single JSON file.
*
* All gzip files (mostly .json.gz) are decompressed and recompressed
* using deflate, so a much more simple deflate library can be used.
*/
const fs_1 = __importDefault(require("fs"));
const path_1 = require("path");
const zlib_1 = __importDefault(require("zlib"));
const log_1 = require("../log");
const path_2 = require("../path");
const config = {
dirs: [
"./input/easyseed-bip39",
"./testcases",
"./input/wordlists"
]
};
(function () {
return __awaiter(this, void 0, void 0, function* () {
console.log(log_1.colorify.bold(`Bundling Testcase Data...`));
const data = { "_": JSON.stringify({ name: "browser-fs", config: config }) };
config.dirs.forEach((dirname) => {
let fulldirname = path_2.resolve("packages/testcases", dirname);
fs_1.default.readdirSync(fulldirname).forEach((filename) => {
const key = path_1.join(dirname, filename);
const content = fs_1.default.readFileSync(path_1.join(fulldirname, filename));
if (filename.split(".").pop() === "gz") {
const contentData = zlib_1.default.gunzipSync(content);
data[key] = String(contentData.length) + "," + zlib_1.default.deflateRawSync(contentData).toString("base64");
}
else {
data[key] = content.toString("base64");
}
console.log(` - Added ${key} (${data[key].length} bytes)`);
});
});
// We write it out to all needed places
fs_1.default.writeFileSync(path_2.resolve("packages/testcases/lib/browser-data.json"), JSON.stringify(data));
fs_1.default.writeFileSync(path_2.resolve("packages/testcases/lib._esm/browser-data.json"), JSON.stringify(data));
fs_1.default.writeFileSync(path_2.resolve("packages/testcases/lib.esm/browser-data.json"), JSON.stringify(data));
// Write it to the TypeScript source last, in case it is running it will
// be regenerated overwriting the above files, but with identical content
fs_1.default.writeFileSync(path_2.resolve("packages/testcases/src.ts/browser-data.json"), JSON.stringify(data));
});
})();
1 change: 1 addition & 0 deletions misc/admin/lib/cmds/esm-alias.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export {};
131 changes: 131 additions & 0 deletions misc/admin/lib/cmds/esm-alias.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
"use strict";
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
const fs_1 = __importDefault(require("fs"));
const path_1 = require("path");
const depgraph_1 = require("../depgraph");
const path_2 = require("../path");
const utils_1 = require("../utils");
const log_1 = require("../log");
function diff(a, b) {
return (Buffer.compare(fs_1.default.readFileSync(a), fs_1.default.readFileSync(b)) !== 0);
}
function alias(name) {
return __awaiter(this, void 0, void 0, function* () {
console.log(` Aliasing: ${name}`);
const baseDir = path_2.resolve("packages", name);
const info = utils_1.loadJson(path_2.resolve(baseDir, "package.json"));
const replacements = info["_ethers.alias"] || {};
const skip = Object.keys(replacements).reduce((accum, key) => {
const replace = replacements[key];
accum[replace] = true;
accum[replace + ".map"] = true;
accum[replace.replace(/\.js$/, ".d.ts")] = true;
return accum;
}, ({}));
const transforms = [];
const recurse = function (input, output) {
fs_1.default.readdirSync(path_1.join(baseDir, input)).forEach((filename) => {
const stat = fs_1.default.statSync(path_1.join(baseDir, input, filename));
if (stat.isDirectory()) {
recurse(path_1.join(input, filename), path_1.join(output, filename));
return;
}
if (skip[filename]) {
return;
}
let inputFilename = filename;
let transform = null;
if (filename.match(/\.js(on)?$/)) {
// JavaScript; swap in any replacement
// e.g. (filename = geturl.js) => (inputFilename = browser-geturl.js)
// + transform (//# sourceMappingURL=browser-geturl.js.map) to
// (//# sourceMappingURL=geturl.js.map)
const replace = replacements[filename];
// Skip!
if (replace === "") {
return;
}
if (replace) {
inputFilename = replace;
transform = function (content) {
content = content.replace(/(\/\/# sourceMappingURL=)(.*)$/g, (all, prefix, mapFilename) => {
return prefix + filename + ".map";
});
return content;
};
}
}
else if (filename.match(/\.d\.ts$/)) {
// TypeScript definietion file
// e.g. (filename = geturl.d.ts) => diff(geturl.d.ts, browser-geturl.d.ts)
// We do not need to swap anything out, but we need to ensure
// the definition of the node and browser are identical
const replace = replacements[filename.replace(/\.d\.ts$/i, ".js")];
// Skip!
if (replace === "") {
return;
}
if (replace) {
inputFilename = replace.replace(/\.js$/i, ".d.ts");
if (diff(path_1.join(baseDir, input, filename), path_1.join(baseDir, input, inputFilename))) {
console.log(`Warning: TypeScript Definition files differ: ${filename} != ${inputFilename}`);
}
}
}
else if (filename.match(/\.map$/)) {
// Map files; swap in the replacement
// e.g. (filename = geturl.js.map) => (inputFilename = browser-geturl.js.map)
// + transform the map JSON to reference "geturl.js"
// We need to swap in the replacement and update its data
const replace = replacements[filename.replace(/\.js.map$/i, ".js")];
// Skip!
if (replace === "") {
return;
}
if (replace) {
inputFilename = replace + ".map";
transform = function (content) {
const data = JSON.parse(content);
data["file"] = filename.replace(/\.js\.map$/, ".js");
return JSON.stringify(data);
};
}
}
transforms.push({ input: path_1.join(input, inputFilename), output: path_1.join(output, filename), transform });
});
};
recurse("lib._esm", "lib.esm");
transforms.forEach(({ input, output, transform }) => {
const sourceFile = path_1.join(baseDir, input);
let content = fs_1.default.readFileSync(sourceFile).toString();
if (transform) {
content = transform(content);
}
const targetFile = path_1.join(baseDir, output);
const targetDir = path_1.dirname(targetFile);
utils_1.mkdir(targetDir);
fs_1.default.writeFileSync(targetFile, content);
});
});
}
(function () {
return __awaiter(this, void 0, void 0, function* () {
console.log(log_1.colorify.bold(`Aliasing Node ESM to Browser ESM...`));
const dirnames = depgraph_1.getOrdered(true);
for (let i = 0; i < dirnames.length; i++) {
yield alias(dirnames[i]);
}
});
})();
15 changes: 1 addition & 14 deletions misc/admin/src.ts/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,19 +18,6 @@ function setupConfig(outDir: string, moduleType: string, targetType: string) {

if (info._ethers_nobuild) { return; }

if (targetType === "es2015") {
if (info["browser.esm"]) {
info.browser = info["browser.esm"];
}
} else if (targetType === "es5") {
if (info["browser.umd"]) {
info.browser = info["browser.umd"];
}
} else {
throw new Error("unsupported target");
}
saveJson(filename, info, true);

let path = resolve("packages", dirname, "tsconfig.json");
let content = loadJson(path);
content.compilerOptions.outDir = outDir;
Expand All @@ -40,7 +27,7 @@ function setupConfig(outDir: string, moduleType: string, targetType: string) {

export function setupBuild(buildModule: boolean): void {
if (buildModule) {
setupConfig("./lib.esm/", "es2015", "es2015");
setupConfig("./lib._esm/", "es2015", "es2015");
} else {
setupConfig("./lib/", "commonjs", "es5");
}
Expand Down
56 changes: 56 additions & 0 deletions misc/admin/src.ts/cmds/bundle-testcases.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
"use strict";

/** bundle-testcases
*
* This sript converts all the testase data (including needed
* text files) into a single JSON file.
*
* All gzip files (mostly .json.gz) are decompressed and recompressed
* using deflate, so a much more simple deflate library can be used.
*/


import fs from "fs";
import { join } from "path";
import zlib from "zlib";

import { colorify } from "../log";
import { resolve } from "../path";

const config = {
dirs: [
"./input/easyseed-bip39",
"./testcases",
"./input/wordlists"
]
};

(async function() {
console.log(colorify.bold(`Bundling Testcase Data...`));

const data: Record<string, string> = { "_": JSON.stringify({ name: "browser-fs", config: config }) };

config.dirs.forEach((dirname) => {
let fulldirname = resolve("packages/testcases", dirname);
fs.readdirSync(fulldirname).forEach((filename) => {
const key = join(dirname, filename);
const content = fs.readFileSync(join(fulldirname, filename));
if (filename.split(".").pop() === "gz") {
const contentData = zlib.gunzipSync(content);
data[key] = String(contentData.length) + "," + zlib.deflateRawSync(contentData).toString("base64");
} else {
data[key] = content.toString("base64");
}
console.log(` - Added ${ key } (${ data[key].length } bytes)`);
});
});

// We write it out to all needed places
fs.writeFileSync(resolve("packages/testcases/lib/browser-data.json"), JSON.stringify(data));
fs.writeFileSync(resolve("packages/testcases/lib._esm/browser-data.json"), JSON.stringify(data));
fs.writeFileSync(resolve("packages/testcases/lib.esm/browser-data.json"), JSON.stringify(data));

// Write it to the TypeScript source last, in case it is running it will
// be regenerated overwriting the above files, but with identical content
fs.writeFileSync(resolve("packages/testcases/src.ts/browser-data.json"), JSON.stringify(data));
})();
Loading

0 comments on commit 4809325

Please sign in to comment.