Skip to content

Commit bca7023

Browse files
committed
Add a Babel transform loader to ESM in the global server
This ensures we can load JSX in there for SSR.
1 parent ff5c97e commit bca7023

File tree

3 files changed

+56
-4
lines changed

3 files changed

+56
-4
lines changed

fixtures/flight/loader/global.js

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import babel from '@babel/core';
2+
3+
const babelOptions = {
4+
babelrc: false,
5+
ignore: [/\/(build|node_modules)\//],
6+
plugins: [
7+
'@babel/plugin-syntax-import-meta',
8+
'@babel/plugin-transform-react-jsx',
9+
],
10+
};
11+
12+
export async function load(url, context, defaultLoad) {
13+
const {format} = context;
14+
const result = await defaultLoad(url, context, defaultLoad);
15+
if (result.format === 'module') {
16+
const opt = Object.assign({filename: url}, babelOptions);
17+
const newResult = await babel.transformAsync(result.source, opt);
18+
if (!newResult) {
19+
if (typeof result.source === 'string') {
20+
return result;
21+
}
22+
return {
23+
source: Buffer.from(result.source).toString('utf8'),
24+
format: 'module',
25+
};
26+
}
27+
return {source: newResult.code, format: 'module'};
28+
}
29+
return defaultLoad(url, context, defaultLoad);
30+
}
31+
32+
async function babelTransformSource(source, context, defaultTransformSource) {
33+
const {format} = context;
34+
if (format === 'module') {
35+
const opt = Object.assign({filename: context.url}, babelOptions);
36+
const newResult = await babel.transformAsync(source, opt);
37+
if (!newResult) {
38+
if (typeof source === 'string') {
39+
return {source};
40+
}
41+
return {
42+
source: Buffer.from(source).toString('utf8'),
43+
};
44+
}
45+
return {source: newResult.code};
46+
}
47+
return defaultTransformSource(source, context, defaultTransformSource);
48+
}
49+
50+
export const transformSource =
51+
process.version < 'v16' ? babelTransformSource : undefined;
52+
export const getSource = process.version < 'v16' ? getSourceImpl : undefined;
File renamed without changes.

fixtures/flight/package.json

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,11 @@
6565
"predev": "cp -r ../../build/oss-experimental/* ./node_modules/",
6666
"prebuild": "cp -r ../../build/oss-experimental/* ./node_modules/",
6767
"dev": "concurrently \"npm run dev:region\" \"npm run dev:global\"",
68-
"dev:global": "NODE_ENV=development BUILD_PATH=dist node server/global",
69-
"dev:region": "NODE_ENV=development BUILD_PATH=dist nodemon --watch src --watch dist -- --experimental-loader ./loader/index.js --conditions=react-server server/region",
68+
"dev:global": "NODE_ENV=development BUILD_PATH=dist node --experimental-loader ./loader/global.js server/global",
69+
"dev:region": "NODE_ENV=development BUILD_PATH=dist nodemon --watch src --watch dist -- --experimental-loader ./loader/region.js --conditions=react-server server/region",
7070
"start": "node scripts/build.js && concurrently \"npm run start:region\" \"npm run start:global\"",
71-
"start:global": "NODE_ENV=production node server/global",
72-
"start:region": "NODE_ENV=production node --experimental-loader ./loader/index.js --conditions=react-server server/region",
71+
"start:global": "NODE_ENV=production node --experimental-loader ./loader/global.js server/global",
72+
"start:region": "NODE_ENV=production node --experimental-loader ./loader/region.js --conditions=react-server server/region",
7373
"build": "node scripts/build.js",
7474
"test": "node scripts/test.js --env=jsdom"
7575
},

0 commit comments

Comments
 (0)