Skip to content

Commit f42d5e1

Browse files
authored
Merge pull request #24 from react18-tools/beta
Beta fix strict mode issues
2 parents 96a15c1 + 3c8d2bf commit f42d5e1

File tree

5 files changed

+49
-6
lines changed

5 files changed

+49
-6
lines changed

.changeset/afraid-hotels-end.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
---
2+
"esbuild-plugin-react18": patch
3+
---
4+
5+
Add option to disableJSXRequireDedup.
6+
7+
In case you face any errors, or you want to speed up build a bit, try disabling deduplication of require("react/jsx-runtime")

.changeset/pre.json

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
{
2+
"mode": "exit",
3+
"tag": "beta",
4+
"initialVersions": {
5+
"@example/express": "0.0.0",
6+
"@example/nextjs": "0.0.0",
7+
"@example/remix": "0.0.0",
8+
"@example/vite": "0.0.0",
9+
"esbuild-plugin-react18": "0.2.1",
10+
"@repo/eslint-config": "0.0.0",
11+
"@repo/typescript-config": "0.0.0",
12+
"@repo/jest-presets": "0.0.0",
13+
"@repo/logger": "0.0.0",
14+
"@repo/shared": "0.0.0",
15+
"@repo/scripts": "0.0.0"
16+
},
17+
"changesets": [
18+
"afraid-hotels-end"
19+
]
20+
}

lib/CHANGELOG.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
# esbuild-plugin-react18
22

3+
## 0.2.2-beta.0
4+
5+
### Patch Changes
6+
7+
- d08f4cf: Add option to disableJSXRequireDedup.
8+
9+
In case you face any errors, or you want to speed up build a bit, try disabling deduplication of require("react/jsx-runtime")
10+
311
## 0.2.1
412

513
### Patch Changes

lib/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"name": "esbuild-plugin-react18",
33
"author": "Mayank Kumar Chaudhari <https://mayank-chaudhari.vercel.app>",
44
"private": false,
5-
"version": "0.2.1",
5+
"version": "0.2.2-beta.0",
66
"description": "Unlock the Potential of React Server Components! Harness the power of an ESBuild plugin designed for crafting libraries compatible with RSC (React18 Server Components).",
77
"license": "MPL-2.0",
88
"main": "./dist/index.js",

lib/src/index.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ data-testid` attributes. If your
6060
* to avoid any unexpected results.
6161
*/
6262
buildReplacePatterns?: ReplacePattern[];
63+
64+
/**
65+
* In case you face any errors, or you want to speed up build a bit, try disabling deduplication of require("react/jsx-runtime")
66+
*/
67+
disableJSXRequireDedup?: boolean;
6368
}
6469

6570
function removeTests(build: PluginBuild, options: React18PluginOptions) {
@@ -125,7 +130,7 @@ function replaceBuild(buildReplacePattern: ReplacePattern, result: BuildResult)
125130

126131
const useClientRegExp = /^(["']use strict["'];)?["']use client["'];?/i;
127132
const useServerRegExp = /^(["']use strict["'];)?["']use server["'];?/i;
128-
const jsxImportRegExp = /(var |,)?[a-zA-Z_$][\w$]*=require\("react\/jsx-runtime"\)[;,]?/g;
133+
const jsxImportRegExp = /(var |,)[a-zA-Z_$][\w$]*=require\("react\/jsx-runtime"\)[;,]?/g;
129134
const regExp2replace2GetVar0 = /(var |,)/;
130135
const regExp2replace2GetVar = /[=]require\(['"]react\/jsx-runtime['"]\)[;,]?/;
131136

@@ -154,16 +159,17 @@ function onEndCallBack(result: BuildResult, options: React18PluginOptions, write
154159
txt = txt.replace(emptyChunkImportRegExp, "");
155160

156161
/** remove extra jsx-runtime imports */
157-
if (f.path.endsWith(".js")) {
162+
if (!options.disableJSXRequireDedup && f.path.endsWith(".js")) {
158163
const jsxMatches = txt.match(jsxImportRegExp);
159164
if (jsxMatches !== null && jsxMatches.length > 1) {
160165
const importVarName = jsxMatches[0]
161166
.replace(regExp2replace2GetVar, "")
162167
.replace(regExp2replace2GetVar0, "");
163168
for (let index = 1; index < jsxMatches.length; index++) {
164-
const token = jsxMatches[index];
165-
const toReplace = /^,.*,$/.test(token) ? token.slice(1) : token;
166-
txt = txt.replace(toReplace, "");
169+
let token = jsxMatches[index];
170+
if (/^,.*,$/.test(token)) token = token.slice(1);
171+
else if (token.startsWith("var ") && token.endsWith(",")) token = token.slice(4);
172+
txt = txt.replace(token, "");
167173
const v1 = jsxMatches[index]
168174
.replace(regExp2replace2GetVar, "")
169175
.replace(regExp2replace2GetVar0, "");
@@ -196,6 +202,8 @@ function onEndCallBack(result: BuildResult, options: React18PluginOptions, write
196202
function setup(build: PluginBuild, options: React18PluginOptions = {}) {
197203
const write = build.initialOptions.write;
198204
build.initialOptions.write = false;
205+
// Avoid addiitonal computation when in watch mode or minification is not required
206+
if (!build.initialOptions.minify) options.disableJSXRequireDedup = true;
199207

200208
if (!options.keepTests) removeTests(build, options);
201209

0 commit comments

Comments
 (0)