0
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2026-08-04 10:39:43 +08:00
discourse/frontend/asset-processor/build.mjs
Sérgio Saquetim a8b1606ff8
DEV: Derive styleguide code samples from the rendered example (#42100)
DEV: Keep styleguide samples synchronized with source

Styleguide code samples were hand-written strings that could silently drift
from the markup they described. This caused the char counter sample to
advertise a nonexistent `CharCounter` module while rendering
`DCharCounter`.

Add a `discourse-source-imports` bundler plugin that imports another module's
untranspiled source as a string. `?source=file` returns the complete file,
while `?source=template` extracts the contents of the module's single
`<template>` block. Imports use normal module resolution and pass the result
to `StyleguideExample` through its `@code` argument.

Move the spinner and char counter examples into `components/examples/` and
source their displayed code through the new plugin.

Register the plugin with both the core Rolldown build and the asset processor.
Each pipeline injects its own `readSource(id)` implementation: the asset
processor reads from its in-memory module map, while core reads from disk and
rejects paths outside the project root.

---------

Co-authored-by: David Taylor <david@taylorhq.com>
2026-07-29 13:48:35 -03:00

55 lines
1.5 KiB
JavaScript
Vendored

import { fileURLToPath } from "node:url";
import { rolldown } from "rolldown";
const local = (p) => fileURLToPath(import.meta.resolve(p));
const bundle = await rolldown({
input: local("./transpiler.js"),
tsconfig: false,
platform: "browser",
moduleTypes: { ".wasm": "binary" },
transform: {
define: {
"import.meta.url": "'http://example.com'",
},
},
resolve: {
extensions: [".mjs", ".js", ".cjs", ".json"],
alias: {
"content-tag": local("./content-tag.js"),
path: "path-browserify",
url: local("./url-polyfill.js"),
// Absolute path so postcss's `browser` field (`source-map-js: false`) can't stub it out and disable CSS sourcemaps.
"source-map-js": local("source-map-js"),
assert: local("./noop.js"),
fs: local("./noop.js"),
os: local("./os-shim.js"),
workerpool: local("./workerpool-shim.js"),
},
},
plugins: [
{
name: "text-encoder-umd",
transform: {
filter: { id: /EncoderDecoderTogether\.min\.js$/ },
// Give this UMD a local `self` to attach its exports to, then re-export them.
handler(code) {
return {
code: `var self = {};\n${code}\nexport const { TextEncoder, TextDecoder } = self;\n`,
moduleType: "js",
};
},
},
},
],
});
const { output } = await bundle.generate({
format: "iife",
minify: false,
banner: `var process = { "env": { "EMBER_ENV": "production" }, "cwd": () => "/" };`,
});
await bundle.close();
process.stdout.write(output[0].code);