main
js 52 lines 1.42 KB
Raw
1 import {
2 resolve,
3 load as reactLoad,
4 getSource as getSourceImpl,
5 transformSource as reactTransformSource,
6 } from 'react-server-dom-esm/node-loader';
7
8 export {resolve};
9
10 async function textLoad(url, context, defaultLoad) {
11 const {format} = context;
12 const result = await defaultLoad(url, context, defaultLoad);
13 if (result.format === 'module') {
14 if (typeof result.source === 'string') {
15 return result;
16 }
17 return {
18 source: Buffer.from(result.source).toString('utf8'),
19 format: 'module',
20 };
21 }
22 return result;
23 }
24
25 export async function load(url, context, defaultLoad) {
26 return await reactLoad(url, context, (u, c) => {
27 return textLoad(u, c, defaultLoad);
28 });
29 }
30
31 async function textTransformSource(source, context, defaultTransformSource) {
32 const {format} = context;
33 if (format === 'module') {
34 if (typeof source === 'string') {
35 return {source};
36 }
37 return {
38 source: Buffer.from(source).toString('utf8'),
39 };
40 }
41 return defaultTransformSource(source, context, defaultTransformSource);
42 }
43
44 async function transformSourceImpl(source, context, defaultTransformSource) {
45 return await reactTransformSource(source, context, (s, c) => {
46 return textTransformSource(s, c, defaultTransformSource);
47 });
48 }
49
50 export const transformSource =
51 process.version < 'v16' ? transformSourceImpl : undefined;
52 export const getSource = process.version < 'v16' ? getSourceImpl : undefined;