Skip to content

Commit

Permalink
feat: example bundler + minifier + brotli compressor script
Browse files Browse the repository at this point in the history
  • Loading branch information
bhouston committed Jul 2, 2020
1 parent d94c2cd commit 7d56798
Show file tree
Hide file tree
Showing 7 changed files with 10,498 additions and 102 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,4 @@ bundle*.js
bundle*.js.zip
bundle.terserjs.zip
*.zip
www
85 changes: 85 additions & 0 deletions build.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import { exec } from "child_process";
import program from "commander";
import fs from "fs";
import glob from "glob";
import makeDir from "make-dir";
import path from "path";
import process from "process";

program
.name("build")
.option("-r, --rootDir <dirpath>", "the root of the source directory tree")
.option("-g, --glob <glob>", "the search within the source directory")
.option("-o, --outDir <dirpath>", "the root of the output directory tree")
.option("-m, --minify", "minify the code")
.option("-c, --compress", "compress the code using brotli")
.option("-v, --verboseLevel <level>", "higher numbers means more output", parseInt);
program.parse(process.argv);

async function asyncCommandLine(commandLine) {
return new Promise(function (resolve, reject) {
exec(commandLine, (error, stdout, stderr) => {
if (error) {
return reject(error);
}
return resolve();
});
});
}
async function bundle(inputFilePath, outputFilePath) {
return asyncCommandLine(`yarn rollup ${inputFilePath} --file ${outputFilePath} --format iife --minifyInternalExport`);
}

async function minify(inputFilePath, outputFilePath) {
return asyncCommandLine(
`yarn terser --compress --mangle --mangle-props -- ${inputFilePath} --output ${outputFilePath}`,
);
}

async function compress(inputFilePath) {
return asyncCommandLine(`brotli ${inputFilePath}`);
}

async function transpile() {
return asyncCommandLine(`yarn tgt ${program.minify ? "--minify" : ""}`);
}

async function main() {
await transpile();
const globRegex = `${program.rootDir}/${program.glob}`;
glob(globRegex, {}, function (er, sourceFileNames) {
sourceFileNames.forEach(async (sourceFileName) => {
const sourceDirectory = path.dirname(sourceFileName);
const outputDirectory = sourceDirectory.replace(program.rootDir, program.outDir);

const sourceExtension = path.extname(sourceFileName);
const sourceBaseName = path.basename(sourceFileName, sourceExtension);
const bundledFileName = `${outputDirectory}/${sourceBaseName}.rollup${sourceExtension}`;
const minifiedFileName = `${outputDirectory}/${sourceBaseName}${sourceExtension}`;
//const outputFileName = `${outputDirectory}/${sourceBaseName}.${sourceExtension}`;
if (!fs.existsSync(outputDirectory)) {
makeDir.sync(outputDirectory);
}
await bundle(sourceFileName, bundledFileName);
sourceFileName = bundledFileName;
if (program.minify) {
await minify(sourceFileName, minifiedFileName);
fs.unlinkSync(sourceFileName);
sourceFileName = minifiedFileName;
} else {
fs.renameSync(sourceFileName, minifiedFileName);
}

const compressedFileName = sourceFileName + ".br";
if (fs.existsSync(compressedFileName)) {
fs.unlinkSync(sourceFileName + ".br");
}

if (program.compress) {
await compress(sourceFileName);
}
});
});
}

main();
3 changes: 0 additions & 3 deletions bundle.sh

This file was deleted.

8 changes: 8 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"scripts": {
"build": "tgt & tsc",
"build-examples": "node --experimental-modules build.mjs -g **/index.js -r ./dist -o ./www -m -c -v 0",
"dev": "npm run watch & npm start",
"lint": "eslint ./src/",
"start": "es-dev-server --node-resolve --watch",
Expand All @@ -34,12 +35,17 @@
"devDependencies": {
"@commitlint/cli": "^8.3.5",
"@commitlint/config-conventional": "^8.3.4",
"@rollup/plugin-multi-entry": "^3.0.1",
"@semantic-release/changelog": "^5.0.1",
"@semantic-release/git": "^9.0.0",
"@semantic-release/release-notes-generator": "^9.0.1",
"@types/glob": "^7.1.2",
"@types/jest": "^26.0.0",
"@types/rollup": "^0.54.0",
"@typescript-eslint/eslint-plugin": "^3.1.0",
"@typescript-eslint/parser": "^3.1.0",
"brotli": "^1.3.2",
"commander": "^5.1.0",
"commitizen": "^4.1.2",
"cz-conventional-changelog": "^3.2.0",
"es-dev-server": "^1.53.0",
Expand All @@ -49,7 +55,9 @@
"eslint-plugin-cflint": "^1.0.0",
"eslint-plugin-import": "^2.20.2",
"eslint-plugin-prettier": "^3.1.3",
"glob": "^7.1.6",
"jest": "^26.0.1",
"make-dir": "^3.1.0",
"prettier": "^2.0.5",
"rollup": "^2.18.0",
"semantic-release": "^17.0.8",
Expand Down
97 changes: 0 additions & 97 deletions src/examples/testing/testrig1/index.ts

This file was deleted.

Loading

0 comments on commit 7d56798

Please sign in to comment.