discourse/app/assets/javascripts/theme-transpiler/add-theme-globals.test.mjs
David Taylor aa2fb29fa6
DEV: Use rollup for theme JS compilation (#33103)
This commit is a complete reimplementation of our theme JS compilation
system.

Previously, we compiled theme JS into AMD `define` statements on a
per-source-file basis, and then concatenated them together for the
client. These AMD modules would integrate with those in Discourse core,
allowing two way access between core/theme modules. Going forward, we'll
be moving away from AMD, and towards native ES modules in core. Before
we can do that, we need to stop relying on AMD as the 'glue' between
core and themes/plugins.

This change introduces Rollup (running in mini-racer) as a compiler for
theme JS. This is configured to generate a single ES Module which
exports a list of 'compat modules'. Core `import()`s the modules for
each active theme, and adds them all to AMD. In future, this consumption
can be updated to avoid AMD entirely.

All module resolution within a theme is handled by Rollup, and does not
use AMD.

Import of core/plugin modules from themes are automatically transformed
into calls to a new `window.moduleBroker` interface. For now, this is a
direct interface to AMD. In future, this can be updated to point to real
ES Modules in core.

Despite the complete overhaul of the internals, this is not a breaking
change, and should have no impact on existing themes. If any
incompatibilities are found, please report them on
https://meta.discourse.org.

---------

Co-authored-by: Jarek Radosz <jarek@cvx.dev>
Co-authored-by: Chris Manson <chris@manson.ie>
2025-07-25 12:02:29 +01:00

74 lines
1.9 KiB
JavaScript

/* eslint-disable qunit/require-expect */
import { transformSync } from "@babel/core";
import { expect, test } from "vitest";
import AddThemeGlobals from "./add-theme-globals.js";
function compile(input) {
return transformSync(input, {
configFile: false,
plugins: [AddThemeGlobals],
}).code;
}
test("adds imports automatically", () => {
expect(
compile(`
console.log(settings, themePrefix);
`)
).toMatchInlineSnapshot(`
"import { themePrefix, settings } from "virtual:theme";
console.log(settings, themePrefix);"
`);
});
test("throws error if settings or themePrefix are defined locally", () => {
expect(() =>
compile(`
const settings = {};
console.log(settings);
`)
).toThrowErrorMatchingInlineSnapshot(
`[Error: unknown file: \`settings\` is already defined. Unable to add import.]`
);
expect(() =>
compile(`
const themePrefix = "foo";
console.log(themePrefix);
`)
).toThrowErrorMatchingInlineSnapshot(
`[Error: unknown file: \`themePrefix\` is already defined. Unable to add import.]`
);
});
test("works if settings and themePrefix are already imported", () => {
expect(
compile(`
import { themePrefix, settings } from "virtual:theme";
console.log(settings, themePrefix);
`)
).toMatchInlineSnapshot(`
"import { themePrefix, settings } from "virtual:theme";
console.log(settings, themePrefix);"
`);
});
test("throws error if settings or themePrefix are already imported from the wrong place", () => {
expect(() =>
compile(`
import { themePrefix } from "foo";
console.log(themePrefix);
`)
).toThrowErrorMatchingInlineSnapshot(
`[Error: unknown file: \`themePrefix\` is already imported. Unable to add import from \`virtual:theme\`.]`
);
expect(() =>
compile(`
import { settings } from "foo";
console.log(settings);
`)
).toThrowErrorMatchingInlineSnapshot(
`[Error: unknown file: \`settings\` is already imported. Unable to add import from \`virtual:theme\`.]`
);
});