Skip to content

Commit ca0bc87

Browse files
committed
fix(rest): Bundle path-to-regexp into ESM output for StackBlitz compat
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
1 parent 7c395d6 commit ca0bc87

4 files changed

Lines changed: 49 additions & 10 deletions

File tree

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
'@data-client/rest': patch
3+
---
4+
5+
Fix StackBlitz WebContainers compatibility with path-to-regexp
6+
7+
Bundle `path-to-regexp` into the ESM output (`dist/esm/index.js`) via rollup,
8+
eliminating the CJS/ESM boundary that StackBlitz WebContainers cannot resolve.
9+
The `"module"` and `"browser"` export conditions now point to this bundled ESM
10+
build, while CJS (`dist/index.js`) and React Native (`lib/`) paths are unchanged.

packages/rest/package.json

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -84,10 +84,11 @@
8484
},
8585
"exports": {
8686
".": {
87-
"module": "./lib/index.js",
87+
"types": "./lib/index.d.ts",
88+
"module": "./dist/esm/index.js",
8889
"import": "./node.mjs",
8990
"require": "./dist/index.js",
90-
"browser": "./lib/index.js",
91+
"browser": "./dist/esm/index.js",
9192
"react-native": "./lib/index.js",
9293
"default": "./lib/index.js"
9394
},

packages/rest/rollup.config.mjs

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,12 @@ function isExternal(id) {
2626
return dependencies.some(dep => dep === id || id.startsWith(dep));
2727
}
2828

29+
// Bundles path-to-regexp but externalizes other deps
30+
function isExternalBundlePTR(id) {
31+
if (id === 'path-to-regexp' || id.startsWith('path-to-regexp/')) return false;
32+
return isExternal(id);
33+
}
34+
2935
const configs = [];
3036
if (process.env.BROWSERSLIST_ENV !== 'node12') {
3137
//TODO: this needs to use src/index.ts so it doesn't include the wrong polyfills
@@ -54,6 +60,28 @@ if (process.env.BROWSERSLIST_ENV !== 'node12') {
5460
filesize({ showBrotliSize: true }),
5561
],
5662
});
63+
// ESM build that bundles path-to-regexp (CJS) into ESM output.
64+
// StackBlitz WebContainers cannot resolve CJS named exports, so we must
65+
// eliminate the CJS boundary by inlining path-to-regexp here.
66+
configs.push({
67+
input: 'src/index.ts',
68+
external: isExternalBundlePTR,
69+
output: [{ file: 'dist/esm/index.js', format: 'esm' }],
70+
onwarn,
71+
plugins: [
72+
babel({
73+
exclude: ['node_modules/**', '/**__tests__/**'],
74+
extensions,
75+
babelHelpers: 'runtime',
76+
rootMode: 'upward',
77+
caller: { polyfillMethod: false },
78+
}),
79+
resolve({ extensions }),
80+
resolveTsAsJs,
81+
commonjs({ extensions }),
82+
json(),
83+
],
84+
});
5785
configs.push(typeConfig);
5886
configs.push(typeConfigNext);
5987
} else {

packages/rest/src/RestHelpers.ts

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1-
// Namespace import avoids webpack's per-export CJS validation, which fails in
2-
// StackBlitz WebContainers. Named imports (`import { compile }`) trigger
3-
// `importExportsPresence` checks that break there. `import *` defers property
4-
// access to runtime. No tree-shaking loss: CJS modules are included whole
5-
// regardless of import style, and webpack 5 traces static access on namespaces.
6-
import type { PathFunction, Token, ParamData } from 'path-to-regexp';
7-
import * as pathToRegexpModule from 'path-to-regexp';
8-
const { compile, parse, pathToRegexp } = pathToRegexpModule;
1+
import {
2+
compile,
3+
PathFunction,
4+
parse,
5+
pathToRegexp,
6+
Token,
7+
ParamData,
8+
} from 'path-to-regexp';
99

1010
import { ShortenPath } from './pathTypes.js';
1111

0 commit comments

Comments
 (0)