|
| 1 | +#!/usr/bin/env node |
| 2 | +const path = require("node:path"); |
| 3 | +const fs = require("node:fs/promises"); |
| 4 | +const babel = require("@babel/core"); |
| 5 | +const esbuild = require("esbuild"); |
| 6 | +const sass = require("sass"); |
| 7 | +const postcss = require("postcss"); |
| 8 | +const tagLoader = require("html-tag-js/jsx/tag-loader"); |
| 9 | + |
| 10 | +const postcssConfig = require("../postcss.config.js"); |
| 11 | + |
| 12 | +const args = process.argv.slice(2); |
| 13 | +const modeArgIndex = args.indexOf("--mode"); |
| 14 | +const mode = |
| 15 | + modeArgIndex > -1 && args[modeArgIndex + 1] |
| 16 | + ? args[modeArgIndex + 1] |
| 17 | + : "development"; |
| 18 | +const isProd = mode === "production"; |
| 19 | +const watch = args.includes("--watch"); |
| 20 | + |
| 21 | +const root = path.resolve(__dirname, ".."); |
| 22 | +const outdir = path.join(root, "www", "build"); |
| 23 | +const target = ["es5"]; |
| 24 | + |
| 25 | +async function ensureCleanOutdir() { |
| 26 | + await fs.rm(outdir, { recursive: true, force: true }); |
| 27 | + await fs.mkdir(outdir, { recursive: true }); |
| 28 | +} |
| 29 | + |
| 30 | +async function processCssFile(filePath) { |
| 31 | + const isSass = /\.(sa|sc)ss$/.test(filePath); |
| 32 | + let css; |
| 33 | + |
| 34 | + if (isSass) { |
| 35 | + const result = sass.compile(filePath, { |
| 36 | + style: isProd ? "compressed" : "expanded", |
| 37 | + loadPaths: [ |
| 38 | + path.dirname(filePath), |
| 39 | + path.join(root, "src"), |
| 40 | + path.join(root, "node_modules"), |
| 41 | + ], |
| 42 | + }); |
| 43 | + css = result.css; |
| 44 | + } else { |
| 45 | + css = await fs.readFile(filePath, "utf8"); |
| 46 | + } |
| 47 | + |
| 48 | + const postcssPlugins = postcssConfig.plugins || []; |
| 49 | + const processed = await postcss(postcssPlugins).process(css, { |
| 50 | + from: filePath, |
| 51 | + map: false, |
| 52 | + }); |
| 53 | + |
| 54 | + return processed.css; |
| 55 | +} |
| 56 | + |
| 57 | +const babelPlugin = { |
| 58 | + name: "babel-transform", |
| 59 | + setup(build) { |
| 60 | + build.onLoad({ filter: /\.[cm]?js$/ }, async (args) => { |
| 61 | + const source = await fs.readFile(args.path, "utf8"); |
| 62 | + const result = await babel.transformAsync(source, { |
| 63 | + filename: args.path, |
| 64 | + configFile: path.join(root, ".babelrc"), |
| 65 | + babelrc: false, |
| 66 | + sourceType: "unambiguous", |
| 67 | + caller: { |
| 68 | + name: "esbuild", |
| 69 | + supportsStaticESM: true, |
| 70 | + supportsDynamicImport: true, |
| 71 | + }, |
| 72 | + }); |
| 73 | + const transformed = result && result.code ? result.code : source; |
| 74 | + const contents = tagLoader(transformed); |
| 75 | + return { contents, loader: "js" }; |
| 76 | + }); |
| 77 | + }, |
| 78 | +}; |
| 79 | + |
| 80 | +const cssPlugin = { |
| 81 | + name: "css-modules-as-text", |
| 82 | + setup(build) { |
| 83 | + build.onLoad({ filter: /\.(sa|sc|c)ss$/ }, async (args) => { |
| 84 | + const isModule = /\.m\.(sa|sc|c)ss$/.test(args.path); |
| 85 | + const contents = await processCssFile(args.path); |
| 86 | + return { |
| 87 | + contents, |
| 88 | + loader: isModule ? "text" : "css", |
| 89 | + }; |
| 90 | + }); |
| 91 | + }, |
| 92 | +}; |
| 93 | + |
| 94 | +const nodeFallbackPlugin = { |
| 95 | + name: "node-fallbacks", |
| 96 | + setup(build) { |
| 97 | + const emptyNamespace = "empty-module"; |
| 98 | + |
| 99 | + build.onResolve({ filter: /^path$/ }, () => ({ |
| 100 | + path: require.resolve("path-browserify"), |
| 101 | + })); |
| 102 | + |
| 103 | + build.onResolve({ filter: /^crypto$/ }, () => ({ |
| 104 | + path: "crypto", |
| 105 | + namespace: emptyNamespace, |
| 106 | + })); |
| 107 | + |
| 108 | + build.onLoad({ filter: /.*/, namespace: emptyNamespace }, () => ({ |
| 109 | + contents: "export default {};", |
| 110 | + loader: "js", |
| 111 | + })); |
| 112 | + }, |
| 113 | +}; |
| 114 | + |
| 115 | +async function run() { |
| 116 | + await ensureCleanOutdir(); |
| 117 | + |
| 118 | + const buildOptions = { |
| 119 | + absWorkingDir: root, |
| 120 | + entryPoints: { |
| 121 | + main: "./src/main.js", |
| 122 | + console: "./src/lib/console.js", |
| 123 | + searchInFilesWorker: "./src/sidebarApps/searchInFiles/worker.js", |
| 124 | + }, |
| 125 | + outdir, |
| 126 | + entryNames: "[name]", |
| 127 | + chunkNames: "[name].chunk", |
| 128 | + assetNames: "[name][ext]", |
| 129 | + publicPath: "/build/", |
| 130 | + bundle: true, |
| 131 | + format: "iife", |
| 132 | + platform: "browser", |
| 133 | + target, |
| 134 | + minify: isProd, |
| 135 | + define: { |
| 136 | + "process.env.NODE_ENV": JSON.stringify(mode), |
| 137 | + }, |
| 138 | + nodePaths: [path.join(root, "src")], |
| 139 | + loader: { |
| 140 | + ".hbs": "text", |
| 141 | + ".md": "text", |
| 142 | + ".png": "file", |
| 143 | + ".svg": "file", |
| 144 | + ".jpg": "file", |
| 145 | + ".jpeg": "file", |
| 146 | + ".ico": "file", |
| 147 | + ".ttf": "file", |
| 148 | + ".woff2": "file", |
| 149 | + ".webp": "file", |
| 150 | + ".eot": "file", |
| 151 | + ".woff": "file", |
| 152 | + ".webm": "file", |
| 153 | + ".mp4": "file", |
| 154 | + ".wav": "file", |
| 155 | + }, |
| 156 | + plugins: [babelPlugin, cssPlugin, nodeFallbackPlugin], |
| 157 | + }; |
| 158 | + |
| 159 | + if (watch) { |
| 160 | + const ctx = await esbuild.context(buildOptions); |
| 161 | + await ctx.watch(); |
| 162 | + console.log("esbuild is watching for changes..."); |
| 163 | + return; |
| 164 | + } |
| 165 | + |
| 166 | + await esbuild.build(buildOptions); |
| 167 | +} |
| 168 | + |
| 169 | +run().catch((error) => { |
| 170 | + console.error(error); |
| 171 | + process.exit(1); |
| 172 | +}); |
0 commit comments