Skip to content

Comments

fix: resolve shim imports from packages outside workspace#141

Open
Jinjiang wants to merge 2 commits intodavidmyersdev:mainfrom
Jinjiang:fix/resolve-shim-imports-cross-workspace
Open

fix: resolve shim imports from packages outside workspace#141
Jinjiang wants to merge 2 commits intodavidmyersdev:mainfrom
Jinjiang:fix/resolve-shim-imports-cross-workspace

Conversation

@Jinjiang
Copy link

Summary

Fixes an issue where vite-plugin-node-polyfills failed to resolve shim imports (like vite-plugin-node-polyfills/shims/process) from packages outside the workspace during the build phase.

Problem

When building a project that imports packages from outside the main workspace (e.g., from a sibling workspace or monorepo package), Rollup would fail with:

[vite]: Rollup failed to resolve import "vite-plugin-node-polyfills/shims/process" from "..."

This issue was reported in the reproduction case: https://github.com/Jinjiang/reproductions/tree/vite-polyfill-20251125

Root Cause

The @rollup/plugin-inject plugin only provides a transform hook but not a resolveId hook. When external packages reference Node.js globals (like process, Buffer, or global), the inject plugin transforms them into import statements:

import process from 'vite-plugin-node-polyfills/shims/process'

However, without a resolveId hook, Rollup cannot resolve these imports when they're in packages outside the main workspace, causing the build to fail.

Solution

Added a custom Rollup plugin called vite-plugin-node-polyfills:shims-resolver that provides a resolveId hook to resolve shim imports. The plugin:

  1. Creates a map of shim import specifiers to their absolute file paths using require.resolve()
  2. Intercepts resolution requests for shim imports
  3. Returns the absolute path to the shim file, ensuring Rollup can properly resolve and bundle them

The fix applies to all three global shims: buffer, global, and process.

Testing

  • ✅ Reproduction case now builds successfully
  • ✅ All unit tests pass (14/14)
  • ✅ All build tests pass (react, vanilla, vue examples)
  • ✅ No regressions introduced

Changes

  • Added shimsResolverPlugin to handle resolveId for shim imports during build
  • The plugin is only created when shims need to be injected (based on globals configuration)
  • Works for both regular Rollup and Rolldown-Vite configurations

Fixes an issue where Rollup failed to resolve shim imports (like
'vite-plugin-node-polyfills/shims/process') from packages located
outside the main workspace during the build phase.

The root cause was that @rollup/plugin-inject only provides a
transform hook but not a resolveId hook. When external packages
reference Node.js globals, the inject plugin transforms them into
import statements, but Rollup couldn't resolve these imports from
cross-workspace packages.

Added a custom Rollup plugin 'vite-plugin-node-polyfills:shims-resolver'
that provides a resolveId hook to properly resolve shim imports by
mapping them to their absolute file paths using require.resolve().

This fix applies to all three global shims: buffer, global, and process.
@Stanzilla
Copy link

@sapphi-red what do you think? could solve my issue as well?

@pavitra-infocusp
Copy link

I can attest this to be working well for my project. Thanks @Jinjiang for the patch!

afn added a commit to superdoc-dev/superdoc that referenced this pull request Dec 29, 2025
afn added a commit to superdoc-dev/superdoc that referenced this pull request Dec 29, 2025
harbournick pushed a commit to superdoc-dev/superdoc that referenced this pull request Dec 30, 2025
* feat: bump node to v22.21.1 (latest v22) in .nvmrc

* feat: switch to pnpm workspace

Eliminate package-lock.json in favor of pnpm-lock.yaml

* feat: move package versions to pnpm catalog (via codemod)

Run `pnpx codemod pnpm/catalog` to generate initial catalog. Still to
do: update inconsistent versions of dependent packages.

* feat: move more dependencies to pnpm catalog

* feat: add "source" conditional export to super-editor package

Add "source" to conditions in the superdoc vite build definition so that
@harbour-enterprises/super-editor is imported from `src` rather than
from the built package in `dist`.

* feat: move more dependencies to pnpm catalog

* feat: eliminate aliases for drilling into external packages

Avoid drilling into other packages' source with aliases like:

    '@superdoc/common': fileURLToPath(new URL('../../shared/common', import.meta.url)),

and instead use those packages directly. Leverage the "source" condition
so that we can continue importing source files rather than requiring the
dependent package to be built before use.

* feat: eliminate aliases from tsconfig and add "source" condition

For parity with vite config, remove unnecessary aliases for packages
that exist in the workspace, and add the "source" condition via
customConditions so that tsserver can recognize imports from package
source.

* fix: add missing dependency: measuring-dom depends on word-layout

* fix: use pnpm instead of npm in install-rollup-binary.mjs

* feat: approve canvas, esbuild, and vue-demi to run build scripts

* fix: update path printed in ensure-types output

* fix: run superdoc tsc without "source" custom condition

* fix: use pnpm instead of npm in package.json scripts

* fix: import nextTick from vue instead of process

* feat: use pnpm/pnpx in github actions

* feat: use pnpm for building e2e-tests template vue app

* fix: downgrade vitest to v3 due to breaking changes in v4

Also upgrade jsdom and canvas to their latest versions.

* fix: use pinned commit hash for github action

Address CodeQL warning

* feat: remove install-rollup-binary.mjs

This doesn't seem to be needed; according to the [rollup
docs](https://github.com/rollup/rollup/blob/master/docs/migration/index.md#general-changes),
the native code is automatically installed.

* fix: don't overwrite default test exclusions in vite.config.mjs

See https://main.vitest.dev/config/exclude.html#example

* fix: add missing deps (yjs, y-protocols, lib0) to collaboration-yjs

* fix: add missing prosemirror devDependencies to superdoc

* fix: source export from layout-bridge is src/index.js, not ts

* feat: rename @harbour-enterprises/super-editor to @superdoc/super-editor

* feat: eliminate super-editor path aliases

Only use the path aliases in the super-editor and superdoc packages.

Also add `converter/internal/*` export from super-editor, where imports
of specific files are required.

* fix: fix typescript errors when building layout-engine packages

1. Add dependency on @superdoc/word-layout for WordParagraphLayoutOutput
   type
2. Fix several typescript errors in renderer.ts
3. For now, add `--noCheck` to the tsc build in the pdf painter package
   to allow package to be built despite pending TS errors

* fix: build layout-engine/tests with "source" condition set

Note that this requires adding back the super-editor aliases to allow
references within the super-editor source to @core etc to be resolved.

* feat: add shared config for vitest with "source" condition

Include baseConfig in all vitest configuration to resolve using "source"
condition and to set super-editor aliases

* feat: start normalizing tsconfig.json across projects

Still some work to do to eliminate duplication, but:

* Eliminate `references`, since packages are now imported with their
  proper package names rather than importing across package boundaries
* Move common settings into tsconfig.base.json

* fix: various build issues

* Fix layout-bridge export (index.ts, not index.js)
* Add missing dependencies
* Add "./*" export from pm-adapter to allow importing
  @superdoc/pm-adapter/attributes/paragraph.js (note: consider exporting
  this from the top-level index to avoid needing this deep export)

* feat: prevent imports across package boundaries

Add eslint-plugin-import-x to prevent importing across package
boundaries, e.g.
    import { measureBlock } from '../../../measuring/dom/src/index'

* fix: approve postbuild script for unrs-resolver

* fix: address linter errors from eslint-plugin-import-x

* fix: remove "src/index" from imports, add dependencies

Also update the regexp in packages/superdoc/vite.config.js to add
additional known @superdoc/XXX packages

* chore: fix unit tests for pm-adapter (#1550)

* fix: add missing deps to pm-adapter, add fixtures to tsconfig includes

* chore: move more packages to pnpm catalog

* fix: work around bug in vite-plugin-node-polyfills

Apply patch from
davidmyersdev/vite-plugin-node-polyfills#141
which addresses
davidmyersdev/vite-plugin-node-polyfills#140.
Note - this appears to be the same issue as
davidmyersdev/vite-plugin-node-polyfills#81.

* feat: move examples into its own pnpm workspace

Add override for superdoc package to use the locally packed version
instead of pulling it from the npm repository, while still allowing
examples to be used as fully standalone packages independent of the pnpm
workspace.

* fix: ensure that all superdoc exports are included in dist

1. Remove `common` from package.json; this doesn't appear to be present
   in the package prior to this branch.
2. Remove `super-editor/style.css`, since the exported `style.css`
   contains a superset of the super-editor styles
3. Fix source path for copying images from pdfjs

* feat: generate superdoc type declarations using vite-plugin-dts

Avoid separately running tsc to generate types.

Note: there's an issue (present in the main branch as well) which is
that the declaration files in the superdoc package reference imports
from internal packages rather than re-exporting these types. This should
probably be resolved in a separate PR.

* fix: build superdoc prior to running visual tests

* fix: get visual tests to run

1. Change playwright command line to run `pnpm dev` in each package
2. Change format of test-config.js to list package paths, rather than
   full command lines
3. Add missing dependencies

* feat: add semantic-release-pnpm for semantic-release

* feat: add Dockerfile for running visual tests

To run visual tests in Docker and update the snapshots:

    cd examples/tests
    pnpm run docker:build
    pnpm run docker:update-screenshots

* fix: update snapshots from visual tests

* feat: upload visual test results on failure

* fix: update snapshots from visual tests, using actuals from gh action

* fix: increase latency targets for performance tests

The newer version of jsdom (27.3.0) is significantly slower than the
previous version (25.0.1). Relax the target latency metrics in the
performance test to accommodate the slower jsdom package.

* fix: eliminate references to "@/" import alias

Use @core, @utils, etc for imports from super-editor

* fix: add superdoc source aliases to packages/ai/vitest.config.mjs

* fix: increase latency target even further for perf tests

* fix: update visual test snapshots

* fix: issues with react examples

* Add missing styled-jsx dependency
* Remove <React.Strict> which was causing SuperDoc to be rendered twice.
  This resulted in errors, even after adding
  `editorRef.current.destroy()` to the useEffect unmount callback,
  suggesting that SuperDoc#destroy() is not completely reverting the
  state of the DOM.

* fix: update snapshots for react visual tests

* fix: add missing dependency @superdoc/common for layout-bridge

* fix: update visual test snapshots
@kyranjamie
Copy link

Would love to see this merged

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.

4 participants