Skip to content

Upgrade rspack2#47

Merged
xfsnowind merged 11 commits into
mainfrom
upgrade-rspack2
Apr 9, 2026
Merged

Upgrade rspack2#47
xfsnowind merged 11 commits into
mainfrom
upgrade-rspack2

Conversation

@xfsnowind

@xfsnowind xfsnowind commented Apr 9, 2026

Copy link
Copy Markdown
Collaborator

Summary

Upgrade rspack from v1 to v2 (2.0.0-rc.1 / rc.2) and fix the breaking import.meta.dirname issue caused by Cypress's bundled tsx.

The problem

Rspack v2 is a pure ESM package ("type": "module") that uses import.meta.dirname internally. Cypress uses tsx to load config files, and tsx/esbuild transforms import.meta into const import_meta = {} — an empty object — when converting ESM to CJS. This causes Rspack to crash with:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined

This affects both the library's internal loading of rspack AND user config files that require('@rspack/core').

Related issues:

The fix

  • patchImportMeta.ts — Wraps Module._extensions['.js'] to detect the empty import_meta={} in tsx-transformed code and inject correct dirname/filename/url values before the module executes. Imported first in index.ts so it covers all downstream require() calls.
  • dynamicAbsoluteImport — Uses new Function('specifier', 'return import(specifier)') to preserve native import() at runtime, preventing tsc from converting to require().
  • Explicit return type on devServer.create to avoid exposing a private type from @rspack/dev-server v2.
  • Jest mocks for @rspack/core since Rspack v2's ESM cannot be loaded by Jest's CJS runtime.

Note: The tsx workaround should be removed once tsx#782 is merged and Cypress ships with the fixed version.

Changes

  • Upgrade @rspack/core and @rspack/dev-server to 2.0.0-rc
  • Patch tsx's CJS transform to fix import.meta.dirname
  • Fix type error in devServer.create return type
  • Update tests for rspack v2 ESM compatibility

resolve the importing of rspack lib since it turns to ESM
…ype error

RspackDevServer in @rspack/dev-server v2 has a generic default parameter
referencing a private Server interface. Adding an explicit return type
prevents tsc from exposing the private type in declarations.
tsx/esbuild transforms `import.meta` into `const import_meta = {}` when
converting ESM to CJS, leaving import.meta.dirname undefined. Rspack v2
(pure ESM) relies on this property, causing a crash when loaded through
Cypress's tsx-registered child process.

Patch Module._extensions['.js'] to detect the empty import_meta object
in transformed code and inject the correct dirname, filename, and url
values before the module executes.

Also use dynamicAbsoluteImport to preserve native import() calls,
preventing tsc from converting them to require().

Refs:
- privatenumber/tsx#781
- privatenumber/tsx#782
- web-infra-dev/rspack#13420
Rspack v2 is a pure ESM package that cannot be loaded by Jest's CJS
runtime. Mock @rspack/core and use package.json-only resolution in the
test helper to avoid importing rspack modules directly.
@xfsnowind xfsnowind self-assigned this Apr 9, 2026
@xfsnowind xfsnowind added the enhancement New feature or request label Apr 9, 2026
@socket-security

Copy link
Copy Markdown

Review the following changes in direct dependencies. Learn more about Socket for GitHub.

Diff Package Supply Chain
Security
Vulnerability Quality Maintenance License
Updated@​rspack/​core@​1.7.3 ⏵ 2.0.0-rc.1100 +510080 +196 -1100
Updated@​rspack/​dev-server@​1.2.1 ⏵ 2.0.0-rc.2100 +610010096 +1100

View full report

@xfsnowind xfsnowind changed the title Upgrade rspack2 chore: upgrade rspack to version 2 Apr 9, 2026
@xfsnowind xfsnowind changed the title chore: upgrade rspack to version 2 Draft:Upgrade rspack2 Apr 9, 2026
@xfsnowind xfsnowind changed the title Draft:Upgrade rspack2 Draft: Upgrade rspack2 Apr 9, 2026
@xfsnowind xfsnowind merged commit 29fea33 into main Apr 9, 2026
5 checks passed
@xfsnowind xfsnowind deleted the upgrade-rspack2 branch April 9, 2026 10:18
@xfsnowind xfsnowind changed the title Draft: Upgrade rspack2 Upgrade rspack2 Apr 9, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant