Skip to content

fix: use bundled css-tree and csso to avoid patch.json resolution in …#2197

Open
hybes wants to merge 2 commits intosvg:mainfrom
hybes:fix/bundler-css-tree-patch-json
Open

fix: use bundled css-tree and csso to avoid patch.json resolution in …#2197
hybes wants to merge 2 commits intosvg:mainfrom
hybes:fix/bundler-css-tree-patch-json

Conversation

@hybes
Copy link
Copy Markdown

@hybes hybes commented Jan 8, 2026

…bundlers

css-tree and csso load patch.json via dynamic require at runtime. Bundlers like Rollup/esbuild (used by Nuxt/Nitro, Next/Vercel) cannot statically analyse this and exclude the JSON file from bundled output, causing runtime errors:

Cannot find module '../data/patch.json'

This fix imports from the pre-bundled dist files (css-tree/dist/csstree.esm and csso/dist/csso.esm) which have all patch data inlined, eliminating the runtime JSON require.

Fixes #2149

…bundlers

css-tree and csso load patch.json via dynamic require at runtime.
Bundlers like Rollup/esbuild (used by Nuxt/Nitro, Next/Vercel) cannot
statically analyse this and exclude the JSON file from bundled output,
causing runtime errors:

  Cannot find module '../data/patch.json'

This fix imports from the pre-bundled dist files (css-tree/dist/csstree.esm
and csso/dist/csso.esm) which have all patch data inlined, eliminating
the runtime JSON require.

Fixes svg#2149
@IlyaSemenov
Copy link
Copy Markdown

What if instead of physically inlining the package, we reconfigure the bundler?

Like here:

svgo/rollup.config.js

Lines 43 to 49 in c06d8f6

external: [
'os',
'fs/promises',
'url',
'path',
...Object.keys(PKG.dependencies),
],

what if this is changed to Object.keys(PKG.dependencies).filter(dep => dep !== 'css-tree')?

@KTibow
Copy link
Copy Markdown
Collaborator

KTibow commented Mar 13, 2026

What if instead of physically inlining the package, we reconfigure the bundler?

what if this is changed to Object.keys(PKG.dependencies).filter(dep => dep !== 'css-tree')?

Actually, we don't inline the package right now and wouldn't inline the package with this PR, but that change would end up partially inlining the package. I'd expect to run into problems due to csso too.

- Medium: The three new smoke tests at lib/svgo.test.js:395,
  lib/svgo.test.js:418, and lib/svgo.test.js:434 are redundant
  with existing coverage. Core stylesheet behavior is already covered
  more thoroughly in lib/style.test.js:28, lib/
  style.test.js:87, and lib/style.test.js:174. Plugin fixture coverage
  is already wired through test/plugins/
  _index.test.js:24, with direct CSS cases in
  test/plugins/inlineStyles.10.svg.txt:1,
  test/plugins/prefixIds.01.svg.txt:1,
  and test/plugins/minifyStyles.01.svg.txt:1. The new tests only assert
  toContain(...), so they add very little signal.
- Medium: They also do not cover the actual regression surface. This
  patch is about bundled css-tree/csso resolution, but
  those tests run against source optimize(), not the built browser
  bundle. The existing browser smoke at test/browser.js:35
  still uses plugins: [] on an SVG with no <style>, so it never touches
  the wrapped imports. If you want new coverage for
  this fix, it should be a CSS-bearing case in test/browser.js /
  test:bundles, not in lib/svgo.test.js.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Cannot find module '../data/patch.json'

3 participants