Skip to content

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

Give every bundler the side-effect-free declarations (#80956)

Fraser28f667591b27
diff --git a/.storybook/main.ts b/.storybook/main.ts
index cbee25678e5..f3c6cc3037e 100644
--- a/.storybook/main.ts
+++ b/.storybook/main.ts
@@ -6,6 +6,9 @@ const webpack = require("webpack");
const MiniCssExtractPlugin = require("mini-css-extract-plugin");
const { CSS_CONFIG } = require("../frontend/build/shared/rspack/css-config");
+const {
+ SIDE_EFFECT_FREE_RULE,
+} = require("../frontend/build/shared/rspack/side-effect-free-modules");
const mainAppStories = [
"../frontend/**/*.mdx",
@@ -80,6 +83,7 @@ const config: StorybookConfig = {
module: {
...config.module,
rules: [
+ SIDE_EFFECT_FREE_RULE,
...(config.module?.rules ?? []).filter(
(rule) => !isCSSRule(rule) && !isSvgRule(rule),
),
diff --git a/e2e/support/config.js b/e2e/support/config.js
index 7f47b89edb7..c9b8db8cd72 100644
--- a/e2e/support/config.js
+++ b/e2e/support/config.js
@@ -40,6 +40,10 @@ const {
} = require("@esbuild-plugins/node-modules-polyfill");
const cypressSplit = require("cypress-split");
+const {
+ sideEffectFreeModulesPlugin,
+} = require("../../frontend/build/shared/esbuild/side-effect-free-modules-plugin");
+
const isInstrumented = process.env.INSTRUMENT_COVERAGE === "true";
// The Cypress config process runs with cwd = this file's directory
// (e2e/support), so @cypress/code-coverage writes .nyc_output/out.json here.
@@ -238,7 +242,11 @@ const defaultConfig = {
loader: {
".svg": "text",
},
- plugins: [NodeModulesPolyfillPlugin(), assetsResolverPlugin],
+ plugins: [
+ NodeModulesPolyfillPlugin(),
+ assetsResolverPlugin,
+ sideEffectFreeModulesPlugin,
+ ],
sourcemap: "inline",
}),
);
diff --git a/frontend/build/shared/esbuild/side-effect-free-modules-plugin.js b/frontend/build/shared/esbuild/side-effect-free-modules-plugin.js
new file mode 100644
index 00000000000..2f26d133c7c
--- /dev/null
+++ b/frontend/build/shared/esbuild/side-effect-free-modules-plugin.js
@@ -0,0 +1,63 @@
+/* eslint-env node */
+
+const path = require("path");
+
+const {
+ SCRIPT_FILE_PATTERN,
+ SIDE_EFFECT_FREE_PATHS,
+ SIDE_EFFECT_PATHS,
+} = require("../rspack/side-effect-free-modules");
+
+const PLUGIN_NAME = "side-effect-free-modules";
+
+// A rough check for a relative import, made before the specifier is resolved.
+// The candidate has no extension yet, so it can say "definitely outside" but never "inside".
+const mayResolveIntoDeclaredDir = (candidate) =>
+ SIDE_EFFECT_FREE_PATHS.some((dir) => (candidate + path.sep).startsWith(dir));
+
+const isDeclaredSideEffectFree = (file) =>
+ SCRIPT_FILE_PATTERN.test(file) &&
+ SIDE_EFFECT_FREE_PATHS.some((dir) => file.startsWith(dir)) &&
+ !SIDE_EFFECT_PATHS.some((exception) => file.startsWith(exception));
+
+// esbuild has no module.rules, so this plugin applies the declarations at resolve time:
+// an import that lands in a declared directory comes back marked sideEffects: false,
+// and esbuild drops the module when nothing uses its bindings.
+const sideEffectFreeModulesPlugin = {
+ name: PLUGIN_NAME,
+ setup(build) {
+ // Every declared directory lives under frontend/src/metabase,
+ // so the only specifiers that can reach one are the metabase/ alias and relative paths.
+ build.onResolve({ filter: /^(?:metabase\/|\.{1,2}\/)/ }, async (args) => {
+ // build.resolve runs every onResolve callback again, this one included.
+ // The marker makes the inner pass fall through to esbuild's own resolution.
+ if (args.pluginData === PLUGIN_NAME) {
+ return null;
+ }
+ // Nearly every import in a bundle is relative, and nearly none lands in a declared directory.
+ // The rough check turns those away without paying for a resolve.
+ if (args.path.startsWith(".")) {
+ const candidate = path.resolve(args.resolveDir, args.path);
+ if (!mayResolveIntoDeclaredDir(candidate)) {
+ return null;
+ }
+ }
+ const resolved = await build.resolve(args.path, {
+ kind: args.kind,
+ importer: args.importer,
+ resolveDir: args.resolveDir,
+ pluginData: PLUGIN_NAME,
+ });
+ // A specifier the resolver rejects is left for the normal pass, which reports the error.
+ if (
+ resolved.errors.length > 0 ||
+ !isDeclaredSideEffectFree(resolved.path)
+ ) {
+ return null;
+ }
+ return { ...resolved, sideEffects: false };
+ });
+ },
+};
+
+module.exports = { sideEffectFreeModulesPlugin };
diff --git a/frontend/build/shared/rspack/side-effect-free-modules.js b/frontend/build/shared/rspack/side-effect-free-modules.js
index a3fec9cf4ec..e8f6f2887e9 100644
--- a/frontend/build/shared/rspack/side-effect-free-modules.js
+++ b/frontend/build/shared/rspack/side-effect-free-modules.js
@@ -14,6 +14,7 @@ const SIDE_EFFECT_FREE_PATHS = [
].map((dir) => path.join(REPO_ROOT, dir) + path.sep);
// Exceptions to the above rule, which are known to have side effects and must be included in the bundle.
+// An entry can name a single file or a whole directory, so consumers match entries as prefixes.
const SIDE_EFFECT_PATHS = [
// Module-scope calls fill in the exported NODE_TYPE objects.
"frontend/src/metabase/querying/expressions/pratt/syntax.ts",
@@ -21,15 +22,19 @@ const SIDE_EFFECT_PATHS = [
"frontend/src/metabase/ui/components/overlays/Popover/register-popover-dropdown.ts",
].map((file) => path.join(REPO_ROOT, file));
+// Only script files are ever marked side-effect free.
+// CSS and other asset imports exist for their effect, so they keep the bundler's default.
+const SCRIPT_FILE_PATTERN = /\.[jt]sx?$/;
+
const SIDE_EFFECT_FREE_RULE = {
- // Scope to JS/TS files, since we don't want to mark CSS or other assets as side-effect-free.
- test: /\.[jt]sx?$/,
+ test: SCRIPT_FILE_PATTERN,
include: SIDE_EFFECT_FREE_PATHS,
exclude: SIDE_EFFECT_PATHS,
sideEffects: false,
};
module.exports = {
+ SCRIPT_FILE_PATTERN,
SIDE_EFFECT_FREE_PATHS,
SIDE_EFFECT_PATHS,
SIDE_EFFECT_FREE_RULE,
diff --git a/rspack.embedding-sdk-node.config.js b/rspack.embedding-sdk-node.config.js
index 55ff7ad00cf..82434a2b329 100644
--- a/rspack.embedding-sdk-node.config.js
+++ b/rspack.embedding-sdk-node.config.js
@@ -7,6 +7,10 @@ const path = require("path");
const rspack = require("@rspack/core");
+const {
+ SIDE_EFFECT_FREE_RULE,
+} = require("./frontend/build/shared/rspack/side-effect-free-modules");
+
const SDK_DIST_PATH = path.join(__dirname, "/resources/embedding-sdk/dist");
const SDK_PACKAGE_SRC_PATH =
__dirname + "/enterprise/frontend/src/embedding-sdk-package";
@@ -37,6 +41,7 @@ const sharedResolve = {
const sharedModule = {
rules: [
+ SIDE_EFFECT_FREE_RULE,
{
test: /\.(tsx?|js)$/,
exclude: /node_modules/,
diff --git a/rspack.embedding-sdk-package.config.js b/rspack.embedding-sdk-package.config.js
index 7fc7b861e03..67dbacac28f 100644
--- a/rspack.embedding-sdk-package.config.js
+++ b/rspack.embedding-sdk-package.config.js
@@ -9,6 +9,9 @@ const {
} = require("./frontend/build/embedding-sdk/rspack/shared");
const { BABEL_CONFIG } = require("./frontend/build/shared/rspack/babel-config");
const { CSS_CONFIG } = require("./frontend/build/shared/rspack/css-config");
+const {
+ SIDE_EFFECT_FREE_RULE,
+} = require("./frontend/build/shared/rspack/side-effect-free-modules");
const {
EXTERNAL_DEPENDENCIES,
} = require("./frontend/build/embedding-sdk/constants/external-dependencies");
@@ -60,6 +63,7 @@ const baseConfig = {
module: {
rules: [
+ SIDE_EFFECT_FREE_RULE,
{
test: /\.(tsx?|jsx?)$/,
exclude: /node_modules|cljs/,
diff --git a/rspack.iframe-sdk-embed.config.js b/rspack.iframe-sdk-embed.config.js
index d1fb26bca38..03dd4f40ad6 100644
--- a/rspack.iframe-sdk-embed.config.js
+++ b/rspack.iframe-sdk-embed.config.js
@@ -11,6 +11,10 @@ const {
COMPRESSION_CONFIG,
} = require("./frontend/build/shared/rspack/compression");
+const {
+ SIDE_EFFECT_FREE_RULE,
+} = require("./frontend/build/shared/rspack/side-effect-free-modules");
+
const SRC_PATH = __dirname + "/frontend/src/metabase";
const ENTERPRISE_SRC_PATH =
__dirname + "/enterprise/frontend/src/metabase-enterprise";
@@ -53,6 +57,7 @@ module.exports = {
devServer: { hot: false },
module: {
rules: [
+ SIDE_EFFECT_FREE_RULE,
{
test: /\.(ts|js)$/,
use: [