Skip to content

Commit bb19860

Browse files
authored
fix(sandpack): load transpiled file (#8601)
* fix(sandpack): load transpiled file * encode file content * revert * revert * add comment
1 parent 3e9d4e5 commit bb19860

File tree

7 files changed

+100
-23
lines changed

7 files changed

+100
-23
lines changed

packages/app/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,6 +343,7 @@
343343
"postcss-flexbugs-fixes": "^4.1.0",
344344
"postcss-import": "^12.0.1",
345345
"postcss-loader": "3.0.0",
346+
"postcss-url": "^10.1.3",
346347
"postcss-normalize": "^8.0.1",
347348
"postcss-preset-env": "^6.7.0",
348349
"preval.macro": "^1.0.1",

packages/app/src/sandbox/eval/transpilers/postcss/loader.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import postcss, { ProcessOptions } from 'postcss';
22
import postcssImportPlugin from 'postcss-import';
3+
import postcssUrl from 'postcss-url';
34
import { join } from 'path';
45
import { isDependencyPath } from 'sandbox/eval/utils/is-dependency-path';
56

@@ -59,6 +60,9 @@ export default async function (
5960
return tModule.module.code;
6061
},
6162
}),
63+
postcssUrl({
64+
url: 'rebase',
65+
}),
6266
];
6367

6468
const options: ProcessOptions = {

packages/app/src/sandbox/index.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,15 @@ requirePolyfills().then(() => {
7171
data: {},
7272
});
7373
}
74+
} else if (data.type === 'get-modules') {
75+
const manager = getCurrentManager();
76+
77+
if (manager) {
78+
dispatch({
79+
type: 'all-modules',
80+
data: manager.getModules(),
81+
});
82+
}
7483
} else if (data.type === 'sign-in') {
7584
await requestSandpackSecretFromApp(data.teamId);
7685

packages/app/src/sandbox/worker/sw.no-webpack.js

Lines changed: 36 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -232,26 +232,43 @@ self.addEventListener('fetch', event => {
232232
* TODO: figure out a way to filter bundler requests and
233233
* local requests. Now, it only supports `public`
234234
*/
235+
235236
if (
236-
parsedUrl.origin !== self.location.origin ||
237-
!parsedUrl.pathname.startsWith('/public')
237+
parsedUrl.origin === self.location.origin &&
238+
(parsedUrl.pathname.startsWith('/public') ||
239+
parsedUrl.pathname.startsWith('/node_modules/'))
238240
) {
239-
return;
240-
}
241-
242-
const handleRequest = async () => {
243-
debug(`[SW]: request `, req);
244-
245-
const response = await getResponse(req);
246-
const swResponse = new Response(response.body, {
247-
headers: response.headers,
248-
status: response.status,
249-
});
250-
251-
debug(`[SW]: response`, response);
252-
return swResponse;
253-
};
241+
const handleRequest = async () => {
242+
debug(`[SW]: request `, req);
243+
244+
const response = await getResponse(req);
245+
const pathname = parsedUrl.pathname;
246+
const responseBody =
247+
pathname.endsWith('.woff2') ||
248+
pathname.endsWith('.woff') ||
249+
pathname.endsWith('.ttf')
250+
? base64ToUint8Array(response.body)
251+
: response.body;
252+
253+
const swResponse = new Response(responseBody, {
254+
headers: response.headers,
255+
status: response.status,
256+
});
257+
258+
debug(`[SW]: response`, response);
259+
return swResponse;
260+
};
254261

255-
// eslint-disable-next-line
256-
return event.respondWith(handleRequest());
262+
// eslint-disable-next-line
263+
return event.respondWith(handleRequest());
264+
}
257265
});
266+
267+
function base64ToUint8Array(base64String) {
268+
const binaryString = atob(base64String);
269+
const uint8Array = new Uint8Array(binaryString.length);
270+
for (let i = 0; i < binaryString.length; i++) {
271+
uint8Array[i] = binaryString.charCodeAt(i);
272+
}
273+
return uint8Array;
274+
}

packages/sandpack-core/src/npm/dynamic/fetch-protocols/utils/tar-store.ts

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,21 @@ export class TarStore implements FetchProtocol {
2424
private normalizeTar(tarContents: UntarredFiles) {
2525
const normalized: { [path: string]: DeserializedFetchedTar } = {};
2626
tarContents.forEach(tar => {
27+
/**
28+
* TODO: it's necessary to encode the font files as base64,
29+
* otherwise they will be corrupted when fetched by the sandpack service worker.
30+
*
31+
* Ideally, in the future, we should use ArrayBuffer instead of strings
32+
*/
33+
const encode =
34+
tar.name.endsWith('.woff2') ||
35+
tar.name.endsWith('.woff') ||
36+
tar.name.endsWith('.ttf')
37+
? 'base64'
38+
: undefined;
39+
2740
normalized[tar.name.replace(/^[^/]+/, '')] = {
28-
// TODO(@CompuIves): store buffers rather than strings for binary files
29-
content: Buffer.from(tar.buffer).toString(),
41+
content: Buffer.from(tar.buffer).toString(encode),
3042
};
3143
});
3244
return normalized;

packages/sandpack-core/src/sandpack-secret.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ function getPopupDimensions() {
5858

5959
export function getProtocolAndHostWithSSE() {
6060
if (document.location.host.startsWith('localhost')) {
61-
return 'https://6er17b-3000.csb.app';
61+
return 'http://localhost:3000';
6262
}
6363

6464
if (document.location.host.endsWith('.io')) {

yarn.lock

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10158,6 +10158,11 @@ csstype@^3.0.2, csstype@^3.0.4:
1015810158
resolved "https://registry.yarnpkg.com/csstype/-/csstype-3.1.1.tgz#841b532c45c758ee546a11d5bd7b7b473c8c30b9"
1015910159
integrity sha512-DJR/VvkAvSZW9bTouZue2sSxDwdTN92uHjqeKVm+0dAqdfNykRzQ95tay8aXMBAAPpUiq4Qcug2L7neoRh2Egw==
1016010160

10161+
cuint@^0.2.2:
10162+
version "0.2.2"
10163+
resolved "https://registry.yarnpkg.com/cuint/-/cuint-0.2.2.tgz#408086d409550c2631155619e9fa7bcadc3b991b"
10164+
integrity sha512-d4ZVpCW31eWwCMe1YT3ur7mUDnTXbgwyzaL320DrcRT45rfjYxkt5QWLrmOJ+/UEAI2+fQgKe/fCjR8l4TpRgw==
10165+
1016110166
currently-unhandled@^0.4.1:
1016210167
version "0.4.1"
1016310168
resolved "https://registry.yarnpkg.com/currently-unhandled/-/currently-unhandled-0.4.1.tgz#988df33feab191ef799a61369dd76c17adf957ea"
@@ -17015,7 +17020,7 @@ magic-string@^0.25.2, magic-string@^0.25.5, magic-string@^0.25.7:
1701517020
dependencies:
1701617021
sourcemap-codec "^1.4.4"
1701717022

17018-
make-dir@3.1.0, make-dir@^3.0.0, make-dir@^3.0.2:
17023+
make-dir@3.1.0, make-dir@^3.0.0, make-dir@^3.0.2, make-dir@~3.1.0:
1701917024
version "3.1.0"
1702017025
resolved "https://registry.yarnpkg.com/make-dir/-/make-dir-3.1.0.tgz#415e967046b3a7f1d185277d84aa58203726a13f"
1702117026
integrity sha512-g3FeP20LNwhALb/6Cz6Dd4F2ngze0jz7tbzrD2wAV+o9FeNHe4rL+yK2md0J/fiSf1sa1ADhXqi5+oVwOM/eGw==
@@ -17458,6 +17463,11 @@ mime@^2.4.4:
1745817463
resolved "https://registry.yarnpkg.com/mime/-/mime-2.6.0.tgz#a2a682a95cd4d0cb1d6257e28f83da7e35800367"
1745917464
integrity sha512-USPkMeET31rOMiarsBNIHZKLGgvKc/LrjofAnBlOttf5ajRvqiRA8QsenbcooctK6d6Ts6aqZXBA+XbkKthiQg==
1746017465

17466+
mime@~2.5.2:
17467+
version "2.5.2"
17468+
resolved "https://registry.yarnpkg.com/mime/-/mime-2.5.2.tgz#6e3dc6cc2b9510643830e5f19d5cb753da5eeabe"
17469+
integrity sha512-tqkh47FzKeCPD2PUiPB6pkbMzsCasjxAfC62/Wap5qrUWcb+sFasXUC5I3gYM5iBM8v/Qpn4UK0x+j0iHyFPDg==
17470+
1746117471
mimic-fn@^1.0.0:
1746217472
version "1.2.0"
1746317473
resolved "https://registry.yarnpkg.com/mimic-fn/-/mimic-fn-1.2.0.tgz#820c86a39334640e99516928bd03fca88057d022"
@@ -17569,6 +17579,13 @@ minimatch@^9.0.0, minimatch@^9.0.1:
1756917579
dependencies:
1757017580
brace-expansion "^2.0.1"
1757117581

17582+
minimatch@~3.0.4:
17583+
version "3.0.8"
17584+
resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.0.8.tgz#5e6a59bd11e2ab0de1cfb843eb2d82e546c321c1"
17585+
integrity sha512-6FsRAQsxQ61mw+qP1ZzbL9Bc78x2p5OqNgNpnoAFLTrX8n5Kxph0CsnhmKKNXTWjXqU5L0pGPR7hYk+XWZr60Q==
17586+
dependencies:
17587+
brace-expansion "^1.1.7"
17588+
1757217589
minimist-options@4.1.0:
1757317590
version "4.1.0"
1757417591
resolved "https://registry.yarnpkg.com/minimist-options/-/minimist-options-4.1.0.tgz#c0655713c53a8a2ebd77ffa247d342c40f010619"
@@ -19958,6 +19975,16 @@ postcss-selector-parser@^6.0.10:
1995819975
cssesc "^3.0.0"
1995919976
util-deprecate "^1.0.2"
1996019977

19978+
postcss-url@^10.1.3:
19979+
version "10.1.3"
19980+
resolved "https://registry.yarnpkg.com/postcss-url/-/postcss-url-10.1.3.tgz#54120cc910309e2475ec05c2cfa8f8a2deafdf1e"
19981+
integrity sha512-FUzyxfI5l2tKmXdYc6VTu3TWZsInayEKPbiyW+P6vmmIrrb4I6CGX0BFoewgYHLK+oIL5FECEK02REYRpBvUCw==
19982+
dependencies:
19983+
make-dir "~3.1.0"
19984+
mime "~2.5.2"
19985+
minimatch "~3.0.4"
19986+
xxhashjs "~0.2.2"
19987+
1996119988
postcss-value-parser@^3.2.3:
1996219989
version "3.3.1"
1996319990
resolved "https://registry.yarnpkg.com/postcss-value-parser/-/postcss-value-parser-3.3.1.tgz#9ff822547e2893213cf1c30efa51ac5fd1ba8281"
@@ -26206,6 +26233,13 @@ xterm@^3.8.1:
2620626233
resolved "https://registry.yarnpkg.com/xterm/-/xterm-3.14.5.tgz#c9d14e48be6873aa46fb429f22f2165557fd2dea"
2620726234
integrity sha512-DVmQ8jlEtL+WbBKUZuMxHMBgK/yeIZwkXB81bH+MGaKKnJGYwA+770hzhXPfwEIokK9On9YIFPRleVp/5G7z9g==
2620826235

26236+
xxhashjs@~0.2.2:
26237+
version "0.2.2"
26238+
resolved "https://registry.yarnpkg.com/xxhashjs/-/xxhashjs-0.2.2.tgz#8a6251567621a1c46a5ae204da0249c7f8caa9d8"
26239+
integrity sha512-AkTuIuVTET12tpsVIQo+ZU6f/qDmKuRUcjaqR+OIvm+aCBsZ95i7UVY5WJ9TMsSaZ0DA2WxoZ4acu0sPH+OKAw==
26240+
dependencies:
26241+
cuint "^0.2.2"
26242+
2620926243
y18n@^3.2.1:
2621026244
version "3.2.2"
2621126245
resolved "https://registry.yarnpkg.com/y18n/-/y18n-3.2.2.tgz#85c901bd6470ce71fc4bb723ad209b70f7f28696"

0 commit comments

Comments
 (0)