diff options
Diffstat (limited to 'scripts/build/common.mjs')
-rw-r--r-- | scripts/build/common.mjs | 42 |
1 files changed, 34 insertions, 8 deletions
diff --git a/scripts/build/common.mjs b/scripts/build/common.mjs index c3afc7f..db87134 100644 --- a/scripts/build/common.mjs +++ b/scripts/build/common.mjs @@ -16,13 +16,15 @@ * along with this program. If not, see <https://www.gnu.org/licenses/>. */ -import { execSync } from "child_process"; +import { exec, execSync } from "child_process"; import esbuild from "esbuild"; import { existsSync } from "fs"; import { readdir, readFile } from "fs/promises"; import { join } from "path"; +import { promisify } from "util"; -const watch = process.argv.includes("--watch"); +export const watch = process.argv.includes("--watch"); +export const isStandalone = JSON.stringify(process.argv.includes("--standalone")); // https://github.com/evanw/esbuild/issues/619#issuecomment-751995294 /** @@ -42,14 +44,15 @@ export const makeAllPackagesExternalPlugin = { export const globPlugins = { name: "glob-plugins", setup: build => { - build.onResolve({ filter: /^plugins$/ }, args => { + const filter = /^~plugins$/; + build.onResolve({ filter }, args => { return { namespace: "import-plugins", path: args.path }; }); - build.onLoad({ filter: /^plugins$/, namespace: "import-plugins" }, async () => { + build.onLoad({ filter, namespace: "import-plugins" }, async () => { const pluginDirs = ["plugins", "userplugins"]; let code = ""; let plugins = "\n"; @@ -76,14 +79,14 @@ export const globPlugins = { } }; -const gitHash = execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim(); +export const gitHash = execSync("git rev-parse --short HEAD", { encoding: "utf-8" }).trim(); /** * @type {esbuild.Plugin} */ export const gitHashPlugin = { name: "git-hash-plugin", setup: build => { - const filter = /^git-hash$/; + const filter = /^~git-hash$/; build.onResolve({ filter }, args => ({ namespace: "git-hash", path: args.path })); @@ -96,10 +99,32 @@ export const gitHashPlugin = { /** * @type {esbuild.Plugin} */ +export const gitRemotePlugin = { + name: "git-remote-plugin", + setup: build => { + const filter = /^~git-remote$/; + build.onResolve({ filter }, args => ({ + namespace: "git-remote", path: args.path + })); + build.onLoad({ filter, namespace: "git-remote" }, async () => { + const res = await promisify(exec)("git remote get-url origin", { encoding: "utf-8" }); + const remote = res.stdout.trim() + .replace("https://github.com/", "") + .replace("git@github.com:", "") + .replace(/.git$/, ""); + + return { contents: `export default "${remote}"` }; + }); + } +}; + +/** + * @type {esbuild.Plugin} + */ export const fileIncludePlugin = { name: "file-include-plugin", setup: build => { - const filter = /^@fileContent\/.+$/; + const filter = /^~fileContent\/.+$/; build.onResolve({ filter }, args => ({ namespace: "include-file", path: args.path, @@ -126,5 +151,6 @@ export const commonOpts = { minify: !watch, sourcemap: watch ? "inline" : "", legalComments: "linked", - plugins: [fileIncludePlugin] + plugins: [fileIncludePlugin, gitHashPlugin, gitRemotePlugin], + external: ["~plugins", "~git-hash", "~git-remote"] }; |