Skip to content

Commit b536ca6

Browse files
authored
Merge pull request #21 from react18-tools/beta
Beta
2 parents 952f597 + 238d6a6 commit b536ca6

File tree

8 files changed

+70
-16
lines changed

8 files changed

+70
-16
lines changed

.changeset/cold-pillows-wink.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"esbuild-plugin-react18": minor
3+
---
4+
5+
Remove duplicate 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.1.6",
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+
"cold-pillows-wink"
19+
]
20+
}

lib/CHANGELOG.md

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

3+
## 0.2.0-beta.0
4+
5+
### Minor Changes
6+
7+
- ecb89d5: Remove duplicate require("react/jsx-runtime")
8+
39
## 0.1.6
410

511
### 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.1.6",
5+
"version": "0.2.0-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: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,12 @@ function replaceBuild(buildReplacePattern: ReplacePattern, result: BuildResult)
123123
);
124124
}
125125

126+
const useClientRegExp = /^(["']use strict["'];)?["']use client["'];?/i;
127+
const useServerRegExp = /^(["']use strict["'];)?["']use server["'];?/i;
128+
const jsxImportRegExp = /(var |,)?[a-zA-Z_$][\w$]*=require\("react\/jsx-runtime"\);?/g;
129+
const regExp2replace2GetVar0 = /(var |,)/;
130+
const regExp2replace2GetVar = /[=]require\(['"]react\/jsx-runtime['"]\);?/;
131+
126132
function onEndCallBack(result: BuildResult, options: React18PluginOptions, write?: boolean) {
127133
/** remove empty file imports */
128134
const emptyChunkFiles = result.outputFiles
@@ -139,20 +145,30 @@ function onEndCallBack(result: BuildResult, options: React18PluginOptions, write
139145
?.filter(f => !f.path.endsWith(".map"))
140146
.forEach(f => {
141147
let txt = f.text;
142-
txt = txt.replace(
143-
/^(["']use strict["'];)?["']use client["'];?/i,
144-
'"use client";\n"use strict";',
145-
);
148+
txt = txt.replace(useClientRegExp, '"use client";\n"use strict";');
146149

147150
/** module level use server */
148-
txt = txt.replace(
149-
/^(["']use strict["'];)?["']use server["'];?/i,
150-
'"use server";\n"use strict";',
151-
);
151+
txt = txt.replace(useServerRegExp, '"use server";\n"use strict";');
152152

153153
/** remove empty file imports */
154154
txt = txt.replace(emptyChunkImportRegExp, "");
155155

156+
/** remove extra jsx-runtime imports */
157+
if (f.path.endsWith(".js")) {
158+
const jsxMatches = txt.match(jsxImportRegExp);
159+
if (jsxMatches !== null && jsxMatches.length > 1) {
160+
const importVarName = jsxMatches[0]
161+
.replace(regExp2replace2GetVar, "")
162+
.replace(regExp2replace2GetVar0, "");
163+
for (let index = 1; index < jsxMatches.length; index++) {
164+
txt = txt.replace(jsxMatches[index], "");
165+
const v1 = jsxMatches[index]
166+
.replace(regExp2replace2GetVar, "")
167+
.replace(regExp2replace2GetVar0, "");
168+
txt = txt.replace(new RegExp(`\\b${v1}\\b`, "g"), importVarName);
169+
}
170+
}
171+
}
156172
f.contents = new TextEncoder().encode(txt);
157173
});
158174

packages/shared/package.json

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,16 @@
3737
"vite-tsconfig-paths": "^4.3.2",
3838
"vitest": "^1.6.0"
3939
},
40+
"peerDependencies": {
41+
"@types/react": "16.8 - 19",
42+
"next": "10 - 14",
43+
"react": "16.8 - 19"
44+
},
45+
"peerDependenciesMeta": {
46+
"next": {
47+
"optional": true
48+
}
49+
},
4050
"dependencies": {
4151
"@mayank1513/fork-me": "^2.1.2",
4252
"@repo/scripts": "workspace:*",

packages/shared/src/server/logo/logo.module.scss

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
overflow: hidden;
99
box-shadow: 0px 2px 8px -1px #0000001a;
1010
padding: 2px;
11-
width: 180px;
11+
width: 215px;
1212
span {
1313
font-weight: 700;
1414
font-family: var(--font-mono);
@@ -61,9 +61,3 @@
6161
background-position: -100% 50%;
6262
}
6363
}
64-
65-
@media screen and (min-width: 800px) {
66-
.logo {
67-
width: 210px;
68-
}
69-
}

pnpm-lock.yaml

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)