Skip to content

The easy-to-use open source Business Intelligence and Embedded Analytics tool that lets everyone work with data :bar_chart:

Drop the empty script emitted for the styles entry (#82548)

Romeo Van Snick161f56350b7b
diff --git a/frontend/build/shared/rspack/plugins/DropStylesEntryScriptPlugin.js b/frontend/build/shared/rspack/plugins/DropStylesEntryScriptPlugin.js
new file mode 100644
index 00000000000..85ddc3009a4
--- /dev/null
+++ b/frontend/build/shared/rspack/plugins/DropStylesEntryScriptPlugin.js
@@ -0,0 +1,59 @@
+// @ts-check
+const rspack = require("@rspack/core");
+const HtmlWebpackPlugin = require("html-webpack-plugin");
+
+const PLUGIN_NAME = "DropStylesEntryScriptPlugin";
+const STYLES_ENTRY = "styles";
+
+/**
+ * The `styles` entry is a stylesheet, so the JS file emitted beside it holds one
+ * empty module and does nothing. This drops that file, its script tag and its
+ * preload hint, and leaves the stylesheet alone.
+ *
+ * Only for production builds: in dev the entry also carries the hot-reload runtime.
+ */
+class DropStylesEntryScriptPlugin {
+ apply(/** @type {import("webpack").Compiler} */ compiler) {
+ compiler.hooks.compilation.tap(
+ PLUGIN_NAME,
+ (/** @type {import("webpack").Compilation} */ compilation) => {
+ const scripts = () =>
+ [...(compilation.namedChunks.get(STYLES_ENTRY)?.files ?? [])].filter(
+ (file) => file.endsWith(".js"),
+ );
+
+ // Runs before PreloadAssetTags, which builds its hints from the tags
+ // left here, so the preload goes with the script tag.
+ HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tapAsync(
+ PLUGIN_NAME,
+ (data, cb) => {
+ const files = scripts();
+ data.assetTags.scripts = data.assetTags.scripts.filter((tag) => {
+ const { src } = tag.attributes;
+ return (
+ typeof src !== "string" ||
+ !files.some((file) => src.endsWith(file))
+ );
+ });
+ cb(null, data);
+ },
+ );
+
+ // Early enough that the compression plugins never see the file.
+ compilation.hooks.processAssets.tap(
+ {
+ name: PLUGIN_NAME,
+ stage: rspack.Compilation.PROCESS_ASSETS_STAGE_SUMMARIZE,
+ },
+ () => {
+ for (const file of scripts()) {
+ compilation.deleteAsset(file);
+ }
+ },
+ );
+ },
+ );
+ }
+}
+
+module.exports.DropStylesEntryScriptPlugin = DropStylesEntryScriptPlugin;
diff --git a/rspack.main.config.js b/rspack.main.config.js
index a2dffbf4bea..37fa7fc8aff 100644
--- a/rspack.main.config.js
+++ b/rspack.main.config.js
@@ -27,6 +27,9 @@ const {
const {
CssVarsDeclarationPlugin,
} = require("./frontend/build/shared/rspack/plugins/CssVarsDeclarationPlugin/css-vars-declaration-plugin");
+const {
+ DropStylesEntryScriptPlugin,
+} = require("./frontend/build/shared/rspack/plugins/DropStylesEntryScriptPlugin");
const {
RESOLVE_ALIASES,
} = require("./frontend/build/shared/rspack/resolve-aliases");
@@ -360,6 +363,7 @@ const config = {
ignoreOrder: true,
}),
new OnScriptError(),
+ ...(isDevMode ? [] : [new DropStylesEntryScriptPlugin()]),
new PreloadAssetTags(),
new HtmlWebpackPlugin({
filename: "../../index.html",