Replace minimist, convert build scripts to ES modules + misc refactoring

This commit is contained in:
hensm
2022-08-19 03:09:59 +01:00
parent 170b80283d
commit a186570dc8
17 changed files with 702 additions and 505 deletions

View File

@@ -1,45 +1,50 @@
// @ts-check
"use strict";
const fs = require("fs-extra");
const path = require("path");
import fs from "fs-extra";
import path from "path";
import url from "url";
const esbuild = require("esbuild");
const minimist = require("minimist");
const sveltePlugin = require("esbuild-svelte");
const sveltePreprocess = require("svelte-preprocess");
const webExt = require("web-ext");
import esbuild from "esbuild";
import sveltePlugin from "esbuild-svelte";
import sveltePreprocess from "svelte-preprocess";
import yargs from "yargs";
import webExt from "web-ext";
const { copyFilesPlugin } = require("./lib/copyFilesPlugin.js");
import copyFilesPlugin from "./lib/copyFilesPlugin.js";
const BRIDGE_NAME = "fx_cast_bridge";
const BRIDGE_VERSION = "0.2.0";
const MIRRORING_APP_ID = "19A6F4AE";
const argv = minimist(process.argv.slice(2), {
boolean: ["package", "watch"],
string: ["mirroringAppId", "mode"],
default: {
package: false,
watch: false,
mirroringAppId: MIRRORING_APP_ID,
mode: "development"
}
});
if (argv.package && argv.watch) {
console.error("Cannot package whilst watching files.");
process.exit(1);
}
const argv = yargs()
.help()
.version(false)
.option("watch", {
describe: "Rebuild on changes",
type: "boolean"
})
.option("package", {
describe: "Package with web-ext",
type: "boolean",
conflicts: "watch"
})
.option("mode", {
describe: "Set build mode",
choices: ["development", "production"],
default: "development"
})
.parseSync(process.argv);
// If packaging, use production mode
if (argv.package) {
argv.mode = "production";
}
const __dirname = path.dirname(url.fileURLToPath(import.meta.url));
// Paths
const rootPath = path.resolve(__dirname, "../");
const rootPath = path.join(__dirname, "../");
const srcPath = path.join(rootPath, "src");
const distPath = path.join(rootPath, "../dist/ext/");
@@ -75,7 +80,7 @@ const buildOpts = {
define: {
BRIDGE_NAME: `"${BRIDGE_NAME}"`,
BRIDGE_VERSION: `"${BRIDGE_VERSION}"`,
MIRRORING_APP_ID: `"${argv.mirroringAppId}"`
MIRRORING_APP_ID: `"${MIRRORING_APP_ID}"`
},
plugins: [
// @ts-ignore

View File

@@ -1,11 +1,11 @@
// @ts-check
"use strict";
const path = require("path");
const fs = require("fs");
import path from "path";
import fs from "fs";
// eslint-disable-next-line no-unused-vars
const esbuild = require("esbuild");
import esbuild from "esbuild";
/**
* Walks file tree from a given root path.
@@ -37,17 +37,14 @@ function* walk(rootPath) {
*
* @type {(opts: CopyFilesPluginOpts) => esbuild.Plugin}
*/
exports.copyFilesPlugin = opts => {
export default opts => {
if (!fs.existsSync(opts.src)) {
throw new Error("copyFilesPlugin: src path not found!");
}
const matchingPaths = [];
for (const path of walk(opts.src)) {
if (!opts.excludePattern?.test(path)) {
matchingPaths.push(path);
}
}
const matchingPaths = [...walk(opts.src)].filter(
path => !opts.excludePattern?.test(path)
);
return {
name: "copy-files",