Skip to content

Commit 8e46912

Browse files
authored
Fix babel warning when downloading dynamic files in worker (codesandbox#2464)
Whenever we try to download dynamic files in a worker we need to write the downloaded files to the filesystem. The reason for this is that we have a separate fs in the worker that's synced eagerly, because when we need to execute files in the worker we can only retrieve the files synchronously. We used to save the files to the WorkerFS, the WorkerFS is responsible for downloading the files from the main thread. But this FS would get confused by also writing to it from the worker, and then get out of sync. I now wrapped the WorkerFS with an OverlayFS so that writes are written on top of the FS, but not to the FS. I had to bump our browserfs version for this.
1 parent d7dfad2 commit 8e46912

File tree

12 files changed

+33
-18
lines changed

12 files changed

+33
-18
lines changed

packages/app/config/build.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,13 @@ const staticAssets = [
2929
from: 'packages/app/static',
3030
to: 'static',
3131
},
32+
{
33+
from: isDev
34+
? 'standalone-packages/codesandbox-browserfs/build'
35+
: 'standalone-packages/codesandbox-browserfs/dist',
36+
to: 'static/browserfs4',
37+
},
38+
// For caching purposes
3239
{
3340
from: isDev
3441
? 'standalone-packages/codesandbox-browserfs/build'

packages/app/src/app/components/CodeEditor/Monaco/workers/linter/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import Linter from 'eslint/lib/linter';
33
import monkeypatch from './monkeypatch-babel-eslint';
44

55
self.importScripts(
6-
`${process.env.CODESANDBOX_HOST}/static/browserfs3/browserfs.min.js`
6+
`${process.env.CODESANDBOX_HOST}/static/browserfs4/browserfs.min.js`
77
);
88

99
/* eslint-disable global-require */

packages/app/src/app/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@
4848
</script>
4949
<!-- End Google Tag Manager -->
5050
<!-- <script async src="//cdn.headwayapp.co/widget.js"></script> -->
51-
<script src="<%= webpackConfig.output.publicPath %>static/browserfs3/browserfs<%=process.env.NODE_ENV
51+
<script src="<%= webpackConfig.output.publicPath %>static/browserfs4/browserfs<%=process.env.NODE_ENV
5252
=== 'development' ? '' : '.min'%>.js"
5353
type="text/javascript"></script>
5454

packages/app/src/app/vscode/extensionHostWorker/common/global.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable global-require */
12
import { EventEmitter } from 'events';
23
import requirePolyfills from '@codesandbox/common/lib/load-dynamic-polyfills';
34

@@ -19,13 +20,12 @@ export const initializePolyfills = () => {
1920

2021
export const loadBrowserFS = () => {
2122
ctx.importScripts(
22-
`${process.env.CODESANDBOX_HOST}/static/browserfs3/browserfs.js`
23+
`${process.env.CODESANDBOX_HOST}/static/browserfs4/browserfs.js`
2324
);
2425
};
2526

2627
export const initializeGlobals = () => {
2728
// We need to initialize some node environment stubs
28-
ctx.BrowserFS = ctx.BrowserFS;
2929
ctx.process = ctx.BrowserFS.BFSRequire('process');
3030
ctx.process.platform = 'linux';
3131
ctx.process.stdin = new EventEmitter();

packages/app/src/embed/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
</script>
3838
<!-- End Google Tag Manager -->
3939

40-
<script src="<%= webpackConfig.output.publicPath %>static/browserfs3/browserfs<%=process.env.NODE_ENV
40+
<script src="<%= webpackConfig.output.publicPath %>static/browserfs4/browserfs<%=process.env.NODE_ENV
4141
=== 'development' ? '' : '.min'%>.js"
4242
type="text/javascript"></script>
4343

packages/app/src/sandbox/eval/npm/fetch-npm-module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ const urlProtocols = {
7979
},
8080
unpkg: {
8181
file: async (name: string, version: string, path: string) =>
82-
`https://unpkg.com/${name}@${version}/${path}`,
82+
`https://unpkg.com/${name}@${version}${path}`,
8383
meta: async (name: string, version: string) =>
8484
`https://unpkg.com/${name}@${version}/?meta`,
8585
normalizeMeta: normalize,

packages/app/src/sandbox/eval/transpilers/babel/worker/babel-worker.js

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import dynamicCSSModules from './plugins/babel-plugin-dynamic-css-modules';
1212

1313
import { buildWorkerError } from '../../utils/worker-error-handler';
1414
import getDependencies from './get-require-statements';
15-
import { downloadFromError } from './dynamic-download';
15+
import { downloadFromError, downloadPath } from './dynamic-download';
1616

1717
import { evaluateFromPath, resetCache } from './evaluate';
1818
import {
@@ -85,10 +85,16 @@ async function initializeBrowserFS() {
8585
return new Promise(resolve => {
8686
BrowserFS.configure(
8787
{
88-
fs: 'AsyncMirror',
88+
fs: 'OverlayFS',
8989
options: {
90-
sync: { fs: 'InMemory' },
91-
async: { fs: 'WorkerFS', options: { worker: self } },
90+
writable: { fs: 'InMemory' },
91+
readable: {
92+
fs: 'AsyncMirror',
93+
options: {
94+
sync: { fs: 'InMemory' },
95+
async: { fs: 'WorkerFS', options: { worker: self } },
96+
},
97+
},
9298
},
9399
},
94100
e => {
@@ -127,6 +133,7 @@ async function installPlugin(Babel, BFSRequire, plugin, currentPath, isV7) {
127133
let evaluatedPlugin = null;
128134

129135
try {
136+
await downloadPath(plugin);
130137
evaluatedPlugin = evaluateFromPath(
131138
fs,
132139
BFSRequire,

packages/app/src/sandbox/eval/transpilers/babel/worker/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import loadPolyfills from '@codesandbox/common/lib/load-dynamic-polyfills';
33
require('app/config/polyfills');
44

55
self.importScripts(
6-
`${process.env.CODESANDBOX_HOST}/static/browserfs3/browserfs.min.js`
6+
`${process.env.CODESANDBOX_HOST}/static/browserfs4/browserfs.min.js`
77
);
88

99
self.process = self.BrowserFS.BFSRequire('process');

packages/app/src/sandbox/eval/transpilers/sass/worker/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
self.importScripts(
2-
`${process.env.CODESANDBOX_HOST}/static/browserfs3/browserfs.min.js`
2+
`${process.env.CODESANDBOX_HOST}/static/browserfs4/browserfs.min.js`
33
);
44

55
self.process = self.BrowserFS.BFSRequire('process');

packages/app/src/sandbox/index.html

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
<title>Sandbox - CodeSandbox</title>
88
<link rel="manifest" href="/manifest.json">
99
<link rel="shortcut icon" href="/favicon.ico">
10-
<script src="<%= webpackConfig.output.publicPath %>static/browserfs3/browserfs.min.js"
10+
<script src="<%= webpackConfig.output.publicPath %>static/browserfs4/browserfs.min.js"
1111
type="text/javascript"></script>
1212

1313
<script>

0 commit comments

Comments
 (0)