Skip to content

Commit 37f69ee

Browse files
committed
Isomorphic React as esbuild plugin
1 parent fdfb619 commit 37f69ee

1 file changed

Lines changed: 55 additions & 46 deletions

File tree

packages/bundle/esbuild.static.mjs

Lines changed: 55 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -15,55 +15,21 @@ const isomorphicReactPlugin = {
1515
name: 'isomorphic-react',
1616
setup(build) {
1717
// eslint-disable-next-line require-unicode-regexp
18-
build.onResolve({ filter: /^(react|react-dom)$/ }, ({ path: pkgName }) => ({
19-
path: resolve(fileURLToPath(import.meta.url), `../../isomorphic-${pkgName}/dist/${pkgName}.js`)
18+
build.onResolve({ filter: /^(react|react-dom)$/, namespace: 'file' }, ({ path }) => ({
19+
namespace: 'isomorphic-react',
20+
path
2021
}));
21-
}
22-
};
23-
24-
const BASE_CONFIG = {
25-
alias: {
26-
adaptivecards: '@msinternal/adaptivecards',
27-
'base64-js': '@msinternal/base64-js',
28-
'botframework-directlinejs': '@msinternal/botframework-directlinejs',
29-
'microsoft-cognitiveservices-speech-sdk': '@msinternal/microsoft-cognitiveservices-speech-sdk',
30-
'object-assign': '@msinternal/object-assign',
31-
// TODO: [P0] We probably don't need repack React as we have isomorphic React.
32-
// Can we make isomorphic React an esbuild plugin instead?
33-
// react: '@msinternal/react',
34-
// 'react-dom': '@msinternal/react-dom',
35-
'react-is': '@msinternal/react-is'
36-
},
37-
bundle: true,
38-
format: 'esm',
39-
loader: { '.js': 'jsx' },
40-
minify: true,
41-
outdir: resolve(fileURLToPath(import.meta.url), `../static/`),
42-
platform: 'browser',
43-
sourcemap: true,
44-
splitting: true,
45-
write: true,
4622

47-
/** @type { import('esbuild').Plugin[] } */
48-
plugins: [
49-
isomorphicReactPlugin,
50-
{
51-
name: 'static-builder',
52-
setup(build) {
53-
// eslint-disable-next-line require-unicode-regexp
54-
build.onResolve({ filter: /^[^.]/ }, async args => {
55-
// Only ESM can be externalized, CJS cannot be externalized because require() is not guaranteed to be at top-level.
56-
if (args.kind === 'import-statement') {
57-
const path = await addConfig(args);
58-
59-
return path ? { external: true, path } : undefined;
60-
}
23+
// eslint-disable-next-line require-unicode-regexp
24+
build.onLoad({ filter: /^react$/, namespace: 'isomorphic-react' }, () => ({
25+
contents: "import React from 'react'; module.exports = globalThis.React || React;"
26+
}));
6127

62-
return undefined;
63-
});
64-
}
65-
}
66-
]
28+
// eslint-disable-next-line require-unicode-regexp
29+
build.onLoad({ filter: /^react-dom$/, namespace: 'isomorphic-react' }, () => ({
30+
contents: "import ReactDOM from 'react-dom'; module.exports = globalThis.ReactDOM || ReactDOM;"
31+
}));
32+
}
6733
};
6834

6935
function createWatcherPlugin(name) {
@@ -179,6 +145,49 @@ function getPendingConfig() {
179145
}
180146
}
181147

148+
const BASE_CONFIG = {
149+
alias: {
150+
adaptivecards: '@msinternal/adaptivecards',
151+
'base64-js': '@msinternal/base64-js',
152+
'botframework-directlinejs': '@msinternal/botframework-directlinejs',
153+
'microsoft-cognitiveservices-speech-sdk': '@msinternal/microsoft-cognitiveservices-speech-sdk',
154+
'object-assign': '@msinternal/object-assign',
155+
react: '@msinternal/react',
156+
'react-dom': '@msinternal/react-dom',
157+
'react-is': '@msinternal/react-is'
158+
},
159+
bundle: true,
160+
format: 'esm',
161+
loader: { '.js': 'jsx' },
162+
minify: true,
163+
outdir: resolve(fileURLToPath(import.meta.url), `../static/`),
164+
platform: 'browser',
165+
sourcemap: true,
166+
splitting: true,
167+
write: true,
168+
169+
/** @type { import('esbuild').Plugin[] } */
170+
plugins: [
171+
isomorphicReactPlugin,
172+
{
173+
name: 'static-builder',
174+
setup(build) {
175+
// eslint-disable-next-line require-unicode-regexp
176+
build.onResolve({ filter: /^[^.]/ }, async args => {
177+
// Only ESM can be externalized, CJS cannot be externalized because require() is not guaranteed to be at top-level.
178+
if (args.kind === 'import-statement') {
179+
const path = await addConfig(args);
180+
181+
return path ? { external: true, path } : undefined;
182+
}
183+
184+
return undefined;
185+
});
186+
}
187+
}
188+
]
189+
};
190+
182191
async function buildNextConfig() {
183192
const config = getPendingConfig();
184193

0 commit comments

Comments
 (0)