Upgrade rspack2#47
Merged
Merged
Conversation
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.
|
Review the following changes in direct dependencies. Learn more about Socket for GitHub.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Upgrade rspack from v1 to v2 (2.0.0-rc.1 / rc.2) and fix the breaking
import.meta.dirnameissue caused by Cypress's bundled tsx.The problem
Rspack v2 is a pure ESM package (
"type": "module") that usesimport.meta.dirnameinternally. Cypress uses tsx to load config files, and tsx/esbuild transformsimport.metaintoconst import_meta = {}— an empty object — when converting ESM to CJS. This causes Rspack to crash with:This affects both the library's internal loading of rspack AND user config files that
require('@rspack/core').Related issues:
import.meta.dirnameis undefined in CJS context web-infra-dev/rspack#13420import.meta.dirnameandimport.meta.filenameprivatenumber/tsx#781The fix
patchImportMeta.ts— WrapsModule._extensions['.js']to detect the emptyimport_meta={}in tsx-transformed code and inject correctdirname/filename/urlvalues before the module executes. Imported first inindex.tsso it covers all downstreamrequire()calls.dynamicAbsoluteImport— Usesnew Function('specifier', 'return import(specifier)')to preserve nativeimport()at runtime, preventing tsc from converting torequire().devServer.createto avoid exposing a private type from@rspack/dev-serverv2.@rspack/coresince Rspack v2's ESM cannot be loaded by Jest's CJS runtime.Changes
@rspack/coreand@rspack/dev-serverto 2.0.0-rcimport.meta.dirnamedevServer.createreturn type