fix(rest): Bundle path-to-regexp into ESM output for StackBlitz compat#3866
fix(rest): Bundle path-to-regexp into ESM output for StackBlitz compat#3866
Conversation
🦋 Changeset detectedLatest commit: 77d9d53 The changes in this PR will be included in the next version bump. This PR includes changesets to release 4 packages
Not sure what this means? Click here to learn what changesets are. Click here if you're a maintainer who wants to add another changeset to this PR |
|
The latest updates on your projects. Learn more about Vercel for GitHub. |
StackBlitz WebContainers fundamentally cannot resolve CJS named exports from path-to-regexp, regardless of import style (named, namespace, etc). Add a rollup ESM build (dist/esm/index.js) that bundles path-to-regexp directly, eliminating the CJS boundary. Point the "module" and "browser" export conditions to this bundled ESM output. CJS and React Native paths are unchanged. Reverts the namespace import workaround since it's no longer needed. Made-with: Cursor
ca0bc87 to
3d3378a
Compare
|
Size Change: -524 B (-0.65%) Total Size: 80.4 kB 📦 View Changed
ℹ️ View Unchanged
|
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #3866 +/- ##
=======================================
Coverage 98.08% 98.08%
=======================================
Files 152 153 +1
Lines 2877 2877
Branches 564 564
=======================================
Hits 2822 2822
Misses 11 11
Partials 44 44 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
…athToRegexpESM The regex that strips CJS `exports.X = void 0` lines only matched exactly two chained assignments. TypeScript CJS output can chain all exports on one line (e.g. 7+ assignments). The old regex would leave the line intact, causing a ReferenceError in ESM where `exports` is undefined. Use `/^(?:exports\.\w+\s*=\s*)+void 0;\s*\n/gm` which matches one or more chained `exports.X =` followed by `void 0`. Co-authored-by: Nathaniel Tucker <me@ntucker.me>
There was a problem hiding this comment.
Cursor Bugbot has reviewed your changes and found 1 potential issue.
❌ Bugbot Autofix is OFF. To automatically fix reported issues with cloud agents, enable autofix in the Cursor dashboard.
Reviewed by Cursor Bugbot for commit 77d9d53. Configure here.
| ], | ||
| "scripts": { | ||
| "build:lib": "NODE_ENV=production BROWSERSLIST_ENV='2020' POLYFILL_TARGETS='chrome>88,safari>14' yarn g:babel --out-dir lib", | ||
| "build:lib": "NODE_ENV=production BROWSERSLIST_ENV='2020' POLYFILL_TARGETS='chrome>88,safari>14' yarn g:babel --out-dir lib && NODE_ENV=production BROWSERSLIST_ENV='2020' yarn g:rollup", |
There was a problem hiding this comment.
Dev watch mode broken by chained build commands
Low Severity
The build:lib script was changed from a single Babel command to yarn g:babel --out-dir lib && ... yarn g:rollup. The dev script calls run build:lib -w, where -w is meant to enable Babel's watch mode. With shell && chaining, the -w flag is appended to the last command (yarn g:rollup), so Babel runs once without watching and rollup gets the -w flag instead. This breaks the dev workflow — source file changes are no longer automatically recompiled.
Reviewed by Cursor Bugbot for commit 77d9d53. Configure here.


Motivation
StackBlitz WebContainers fundamentally cannot resolve CJS named exports from
path-to-regexp, regardless of import style. Previous attempts (#3860 named import consolidation, #3862 namespaceimport *) all fail with:This was confirmed to be unreproducible locally — webpack with
importExportsPresence: 'error'passes fine, and Node 18/20/24 ESM loaders all detect the exports correctly. The issue is specific to WebContainers' CJS module analysis.Solution
The only fix is to eliminate the CJS boundary entirely. This PR adds a rollup ESM build (
dist/esm/index.js) that bundlespath-to-regexpdirectly into the output, converting it from CJS to ESM at build time.dist/esm/index.js(ESM format,path-to-regexpinlined, other deps external)"module"and"browser"export conditions now point todist/esm/index.js"types"condition added to exports map for correct TS resolutiondist/index.js), React Native (lib/), and UMD (dist/index.umd.min.js) paths are unchangedRestHelpers.tsreverted to simple named imports (the bundled ESM has properexportstatements, no CJS interop needed)The bundled ESM output is 962 lines (vs 430 for
path-to-regexpalone + the rest of the package) — minimal size increase since path-to-regexp was already a required dependency.Open questions
N/A
Made with Cursor
Note
Medium Risk
Changes the
@data-client/restbuild pipeline and module wiring by inlining a dependency into the ESM/lib output, which could affect bundling behavior and runtime compatibility across environments.Overview
Resolves StackBlitz/WebContainer CJS/ESM interop issues by bundling
path-to-regexpinto the@data-client/restESM/lib output and tree-shaking it down to justcompile,parse, andpathToRegexp.This adds a custom Rollup plugin/config path for
BROWSERSLIST_ENV=2020, updatesbuild:libto run Rollup after Babel, and switchesRestHelpersto import the path helpers via a new localsrc/pathToRegexp.tsre-export (compiled tolib/pathToRegexp.js).Reviewed by Cursor Bugbot for commit 77d9d53. Bugbot is set up for automated code reviews on this repo. Configure here.