Merge pull request #28655 from backstage/rugvip/static

cli: treat static file assets as free from side-effects
This commit is contained in:
Patrik Oldsberg
2025-01-29 15:42:16 +01:00
committed by GitHub
2 changed files with 43 additions and 2 deletions
+5
View File
@@ -0,0 +1,5 @@
---
'@backstage/cli': patch
---
Treat static file assets as always being free from side effects in package builds.
+38 -2
View File
@@ -21,7 +21,12 @@ import {
relative as relativePath,
} from 'path';
import { createFilter } from 'rollup-pluginutils';
import { Plugin, InputOptions, OutputChunk } from 'rollup';
import {
Plugin,
InputOptions,
OutputChunk,
HasModuleSideEffects,
} from 'rollup';
type ForwardFileImportsOptions = {
include: Array<string | RegExp> | string | RegExp | null;
@@ -91,6 +96,33 @@ export function forwardFileImports(options: ForwardFileImportsOptions) {
}
},
options(inputOptions) {
// We're in control of the config ourselves, so these are just checks to
// make sure we don't update the config but forget about the config
// overrides here
const treeshake = inputOptions.treeshake;
if (treeshake !== undefined && typeof treeshake !== 'object') {
throw new Error(
'Expected treeshake input config to be an object or not set',
);
}
if (treeshake?.moduleSideEffects) {
throw new Error('treeshake.moduleSideEffects must not be set');
}
// All external assets are treated as being side-effect free.
//
// This also works around an apparent bug in rollup where the
// `makeAbsoluteExternalsRelative: false` option sometimes caused relative
// asset paths to be rewritten with an incorrect path. They are rewritten
// in the first place because they are being treated as external by this
// plugin, but that seems to be the best way to handle asset files.
const moduleSideEffects: HasModuleSideEffects = id => {
if (filter(id)) {
return false;
}
return true;
};
const origExternal = inputOptions.external;
// We decorate any existing `external` option with our own way of determining
@@ -129,7 +161,11 @@ export function forwardFileImports(options: ForwardFileImportsOptions) {
return true;
};
return { ...inputOptions, external };
return {
...inputOptions,
external,
treeshake: { ...treeshake, moduleSideEffects },
};
},
} satisfies Plugin;
}