derw
Version:
An Elm-inspired language that transpiles to TypeScript
134 lines (133 loc) • 5.19 kB
JavaScript
;
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
var desc = Object.getOwnPropertyDescriptor(m, k);
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
desc = { enumerable: true, get: function() { return m[k]; } };
}
Object.defineProperty(o, k2, desc);
}) : (function(o, m, k, k2) {
if (k2 === undefined) k2 = k;
o[k2] = m[k];
}));
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
Object.defineProperty(o, "default", { enumerable: true, value: v });
}) : function(o, v) {
o["default"] = v;
});
var __importStar = (this && this.__importStar) || function (mod) {
if (mod && mod.__esModule) return mod;
var result = {};
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
__setModuleDefault(result, mod);
return result;
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.bundle = void 0;
const baner_1 = require("@eeue56/baner");
const chokidar = __importStar(require("chokidar"));
const esbuild = __importStar(require("esbuild"));
const path_1 = __importDefault(require("path"));
const compile_1 = require("./compile");
const bundleParser = (0, baner_1.parser)([
(0, baner_1.longFlag)("entry", "Entry point file to bundle up", (0, baner_1.string)()),
(0, baner_1.longFlag)("output", "Output file to generate", (0, baner_1.string)()),
(0, baner_1.longFlag)("quiet", "Don't print any output", (0, baner_1.empty)()),
(0, baner_1.longFlag)("watch", "Watch Derw files for changes", (0, baner_1.empty)()),
(0, baner_1.longFlag)("optimize", "Run generated Javascript through minification", (0, baner_1.empty)()),
(0, baner_1.bothFlag)("h", "help", "This help text", (0, baner_1.empty)()),
]);
function showBundleHelp() {
console.log("To bundle, run `derw build --entry {filename} --output {filename}`");
console.log("To watch use the --watch flag");
console.log("To produce the smallest bundle use the --optimize flag");
console.log((0, baner_1.help)(bundleParser));
}
async function bundle(isInPackageDirectory, argv) {
if (!isInPackageDirectory) {
console.log("No derw-package.json found. Maybe you need to run `derw init` first?");
process.exit(1);
}
const program = (0, baner_1.parse)(bundleParser, argv);
if (program.flags["h/help"].isPresent) {
showBundleHelp();
return;
}
const errors = (0, baner_1.allErrors)(program);
if (errors.length > 0) {
console.log("Errors:");
console.log(errors.join("\n"));
process.exit(1);
}
const output = program.flags.output.isPresent &&
program.flags.output.arguments.kind === "Ok" &&
program.flags.output.arguments.value;
if (!output) {
console.log("You must provide an output filename with --output <filename>");
process.exit(1);
}
let entry = program.flags.entry.isPresent &&
program.flags.entry.arguments.kind === "Ok" &&
program.flags.entry.arguments.value;
if (!entry) {
console.log("You must provide an entry filename with --entry <filename>");
process.exit(1);
}
if (entry.endsWith(".derw")) {
entry = entry.split(".").slice(0, -1).join(".") + ".ts";
}
const args = program.flags.quiet.isPresent ? ["--quiet"] : [];
async function build() {
await (0, compile_1.compileFiles)(isInPackageDirectory, args);
try {
if (program.flags.optimize.isPresent) {
await esbuild.build({
entryPoints: [entry],
logLevel: "error",
bundle: true,
minify: true,
format: "iife",
outfile: output,
});
}
else {
await esbuild.build({
entryPoints: [entry],
logLevel: "error",
bundle: true,
outfile: output,
});
}
}
catch (e) { }
}
if (program.flags.watch.isPresent) {
console.log("Watching src and derw-packages...");
let timer;
chokidar
.watch([
path_1.default.join(process.cwd(), "src"),
path_1.default.join(process.cwd(), "derw-packages"),
])
.on("all", async (event, path) => {
if (path.endsWith(".derw")) {
if (timer !== null) {
clearTimeout(timer);
}
timer = setTimeout(async () => {
await build();
}, 300);
}
});
}
else {
if (!program.flags.quiet.isPresent) {
console.log("Bundling...");
}
await build();
}
}
exports.bundle = bundle;