Skip to content

Commit

Permalink
fix(react): ensure Accordion works when consumers have enabled tree-s…
Browse files Browse the repository at this point in the history
…haking

We incorrectly caused `import "@u-elements/u-details"` to be tree-shaked
when `@digdir/designsystemet-react` was used in an application with tree-
shaking enabled.

This happened because we bundled several dependencies (including this one)
with our library, and our library has `"sideEffects": false`, implying that
any side-effect imports that happened within our library folder was safe to remove.

This was fixed by correctly marking all our dependencies as external, in
which case the application's build will correctly identify `"@u-elements/u-details"``
as having side-effects and thus not remove the import.

Closes #2477
  • Loading branch information
unekinn committed Sep 20, 2024
1 parent 14f5284 commit e987c92
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 19 deletions.
5 changes: 5 additions & 0 deletions .changeset/brave-months-shop.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@digdir/designsystemet-react': patch
---

Ensure Accordion works when consumers have enabled tree-shaking
1 change: 0 additions & 1 deletion packages/react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@
"react-dom": "^18.3.1",
"rimraf": "^6.0.1",
"rollup": "^4.20.0",
"rollup-plugin-peer-deps-external": "^2.2.4",
"typescript": "^5.5.4"
}
}
31 changes: 23 additions & 8 deletions packages/react/rollup.config.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,26 @@
import commonjs from '@rollup/plugin-commonjs';
import resolve from '@rollup/plugin-node-resolve';
import peerDepsExternal from 'rollup-plugin-peer-deps-external';
import pkg from './package.json';

// These are dependencies, but are not in our package.json
const implicitDependencies = [
'@digdir/designsystemet-theme',
'@digdir/designsystemet-css',
'@digdir/design-system-tokens',
];

const dependencies = Object.keys({
...implicitDependencies,
...pkg.dependencies,
...pkg.peerDependencies,
});

/*
Regexes to correctly mark submodules from dependencies as being external
*/
const dependenciesSubmodules = dependencies.map(
(dep) => new RegExp(`^${dep}/`),
);

export default [
{
Expand All @@ -21,12 +41,7 @@ export default [
preserveModulesRoot: 'tsc-build',
},
],
external: [
/@digdir\/designsystemet-theme/,
/@digdir\/designsystemet-css/,
/@digdir\/design-system-tokens/,
/@navikt\/aksel-icons/,
],
plugins: [peerDepsExternal(), resolve(), commonjs()],
external: [...dependencies, ...dependenciesSubmodules],
plugins: [resolve(), commonjs()],
},
];
10 changes: 0 additions & 10 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2983,7 +2983,6 @@ __metadata:
react-dom: "npm:^18.3.1"
rimraf: "npm:^6.0.1"
rollup: "npm:^4.20.0"
rollup-plugin-peer-deps-external: "npm:^2.2.4"
typescript: "npm:^5.5.4"
peerDependencies:
react: ">=18.3.1"
Expand Down Expand Up @@ -14984,15 +14983,6 @@ __metadata:
languageName: node
linkType: hard

"rollup-plugin-peer-deps-external@npm:^2.2.4":
version: 2.2.4
resolution: "rollup-plugin-peer-deps-external@npm:2.2.4"
peerDependencies:
rollup: "*"
checksum: 10/49b7b3f5ca14550146249b5fd86043bffb82a2c40750c80a1845c3b694fadde58435bae0c912ad8cb1c1ef6c1f8ad64914153793391cb13d52e2bc3aa2ac61e2
languageName: node
linkType: hard

"rollup@npm:^4.13.0":
version: 4.14.0
resolution: "rollup@npm:4.14.0"
Expand Down

0 comments on commit e987c92

Please sign in to comment.