|
| 1 | +// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓ |
| 2 | +// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃ |
| 3 | +// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃ |
| 4 | +// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃ |
| 5 | +// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃ |
| 6 | +// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫ |
| 7 | +// ┃ Copyright (c) 2017, the Perspective Authors. ┃ |
| 8 | +// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃ |
| 9 | +// ┃ This file is part of the Perspective library, distributed under the terms ┃ |
| 10 | +// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃ |
| 11 | +// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛ |
| 12 | + |
| 13 | +import * as esbuild from "esbuild"; |
| 14 | +import * as fs from "node:fs"; |
| 15 | +import * as path from "node:path"; |
| 16 | +import { createRequire } from "module"; |
| 17 | +import { bundleAsync as bundleCssAsync, composeVisitors } from "lightningcss"; |
| 18 | +import { fileURLToPath } from "node:url"; |
| 19 | + |
| 20 | +const __dirname = path.dirname(fileURLToPath(import.meta.url)); |
| 21 | +const DIST = path.join(__dirname, "dist"); |
| 22 | + |
| 23 | +function copyRecursive(src, dest) { |
| 24 | + if (!fs.existsSync(src)) return; |
| 25 | + const stat = fs.statSync(src); |
| 26 | + if (stat.isDirectory()) { |
| 27 | + fs.mkdirSync(dest, { recursive: true }); |
| 28 | + for (const child of fs.readdirSync(src)) { |
| 29 | + copyRecursive(path.join(src, child), path.join(dest, child)); |
| 30 | + } |
| 31 | + } else { |
| 32 | + fs.copyFileSync(src, dest); |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// Inline url() asset references as data URIs. |
| 37 | +export function inlineUrlVisitor(fromFile) { |
| 38 | + const dir = path.dirname(fromFile); |
| 39 | + return composeVisitors([ |
| 40 | + { |
| 41 | + Url(url) { |
| 42 | + const ext = path.extname(url.url).toLowerCase(); |
| 43 | + if (![".svg", ".png", ".gif"].includes(ext)) { |
| 44 | + return; |
| 45 | + } |
| 46 | + |
| 47 | + const resolved = path.resolve(dir, url.url); |
| 48 | + if (!fs.existsSync(resolved)) { |
| 49 | + throw new Error(`File not found ${url.url}`); |
| 50 | + // return; |
| 51 | + } |
| 52 | + |
| 53 | + const content = fs.readFileSync(resolved); |
| 54 | + const mime = |
| 55 | + ext === ".svg" |
| 56 | + ? "image/svg+xml" |
| 57 | + : ext === ".png" |
| 58 | + ? "image/png" |
| 59 | + : "image/gif"; |
| 60 | + |
| 61 | + const new_content = content |
| 62 | + .toString("base64") |
| 63 | + .split("\n") |
| 64 | + .map((x) => x.trim()) |
| 65 | + .join(""); |
| 66 | + |
| 67 | + return { |
| 68 | + url: `data:${mime};base64,${new_content}`, |
| 69 | + loc: url.loc, |
| 70 | + }; |
| 71 | + }, |
| 72 | + }, |
| 73 | + ]); |
| 74 | +} |
| 75 | + |
| 76 | +export const resolveNPM = (url) => ({ |
| 77 | + read(filePath) { |
| 78 | + if (filePath.startsWith("http")) { |
| 79 | + return `@import url("${filePath}");`; |
| 80 | + } |
| 81 | + |
| 82 | + return fs.readFileSync(filePath, "utf8"); |
| 83 | + }, |
| 84 | + resolve(specifier, from) { |
| 85 | + if (specifier.startsWith("http")) { |
| 86 | + return { external: specifier }; |
| 87 | + } |
| 88 | + |
| 89 | + const _require = createRequire(url); |
| 90 | + |
| 91 | + if (specifier.startsWith(".") || specifier.startsWith("/")) { |
| 92 | + return path.resolve(path.dirname(from), specifier); |
| 93 | + } |
| 94 | + |
| 95 | + return _require.resolve(specifier); |
| 96 | + }, |
| 97 | +}); |
| 98 | + |
| 99 | +async function build() { |
| 100 | + // Clean and create dist |
| 101 | + fs.mkdirSync(DIST, { recursive: true }); |
| 102 | + |
| 103 | + // Bundle CSS |
| 104 | + const { code: cssCode } = await bundleCssAsync({ |
| 105 | + filename: path.join(__dirname, "./src/css/style.css"), |
| 106 | + minify: true, |
| 107 | + resolver: resolveNPM(import.meta.url), |
| 108 | + visitor: inlineUrlVisitor("./src/css/style.css"), |
| 109 | + }); |
| 110 | + |
| 111 | + fs.mkdirSync(path.join(DIST, "css"), { recursive: true }); |
| 112 | + fs.writeFileSync(path.join(DIST, "style.css"), cssCode); |
| 113 | + |
| 114 | + // Bundle JS entry points |
| 115 | + await esbuild.build({ |
| 116 | + entryPoints: [ |
| 117 | + path.join(__dirname, "src/index.ts"), |
| 118 | + path.join(__dirname, "src/examples.ts"), |
| 119 | + path.join(__dirname, "src/block.ts"), |
| 120 | + ], |
| 121 | + bundle: true, |
| 122 | + splitting: true, |
| 123 | + format: "esm", |
| 124 | + outdir: DIST, |
| 125 | + minify: true, |
| 126 | + sourcemap: true, |
| 127 | + target: ["es2022"], |
| 128 | + define: { |
| 129 | + global: "window", |
| 130 | + }, |
| 131 | + loader: { |
| 132 | + ".wasm": "file", |
| 133 | + ".arrow": "file", |
| 134 | + }, |
| 135 | + }); |
| 136 | + |
| 137 | + // Copy HTML files |
| 138 | + for (const html of ["index.html", "examples.html", "block.html"]) { |
| 139 | + fs.copyFileSync( |
| 140 | + path.join(__dirname, "src", html), |
| 141 | + path.join(DIST, html), |
| 142 | + ); |
| 143 | + } |
| 144 | + |
| 145 | + // Copy static assets |
| 146 | + copyRecursive(path.join(__dirname, "static"), DIST); |
| 147 | + |
| 148 | + // Generate blocks manifest |
| 149 | + const blocksDir = path.join(DIST, "blocks"); |
| 150 | + if (fs.existsSync(blocksDir)) { |
| 151 | + const manifest = {}; |
| 152 | + for (const example of fs.readdirSync(blocksDir)) { |
| 153 | + const exDir = path.join(blocksDir, example); |
| 154 | + if (!fs.statSync(exDir).isDirectory()) continue; |
| 155 | + manifest[example] = fs |
| 156 | + .readdirSync(exDir) |
| 157 | + .filter( |
| 158 | + (f) => |
| 159 | + !f.startsWith(".") && |
| 160 | + !f.endsWith(".png") && |
| 161 | + !f.endsWith(".arrow"), |
| 162 | + ); |
| 163 | + } |
| 164 | + fs.writeFileSync( |
| 165 | + path.join(blocksDir, "manifest.json"), |
| 166 | + JSON.stringify(manifest), |
| 167 | + ); |
| 168 | + } |
| 169 | + |
| 170 | + console.log("Build complete: dist/"); |
| 171 | +} |
| 172 | + |
| 173 | +build().catch((e) => { |
| 174 | + console.error(e); |
| 175 | + process.exit(1); |
| 176 | +}); |
0 commit comments