Skip to content

Commit

Permalink
use @babel/plugin-transform-react-jsx-sourc during dev
Browse files Browse the repository at this point in the history
  • Loading branch information
dmail committed Jan 8, 2025
1 parent dd64dcc commit 93008f3
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion packages/related/cli/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/cli",
"version": "0.2.32",
"version": "0.2.33",
"description": "Command Line Interface for jsenv",
"license": "MIT",
"repository": {
Expand Down
2 changes: 1 addition & 1 deletion packages/related/cli/template-web-preact/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
"@babel/plugin-transform-react-jsx": "7.25.9",
"@jsenv/assert": "4.4.2",
"@jsenv/core": "39.9.3",
"@jsenv/plugin-preact": "1.6.32",
"@jsenv/plugin-preact": "1.6.33",
"@jsenv/plugin-bundling": "2.7.21",
"@jsenv/plugin-minification": "1.5.12",
"@jsenv/eslint-config-relax": "1.3.9",
Expand Down
2 changes: 1 addition & 1 deletion packages/related/cli/template-web-react/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
"@babel/plugin-transform-react-jsx": "7.25.9",
"@jsenv/assert": "4.4.2",
"@jsenv/core": "39.9.3",
"@jsenv/plugin-react": "1.5.54",
"@jsenv/plugin-react": "1.5.55",
"@jsenv/plugin-bundling": "2.7.21",
"@jsenv/plugin-minification": "1.5.12",
"@jsenv/eslint-config-relax": "1.3.9",
Expand Down
3 changes: 2 additions & 1 deletion packages/related/plugin-preact/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/plugin-preact",
"version": "1.6.32",
"version": "1.6.33",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -24,6 +24,7 @@
"babel-plugin-transform-hook-names": "1.0.2",
"@babel/plugin-transform-react-jsx": "7.25.9",
"@babel/plugin-transform-react-jsx-development": "7.25.9",
"@babel/plugin-transform-react-jsx-source": "7.25.9",
"@jsenv/ast": "6.4.1",
"@jsenv/sourcemap": "1.2.29",
"@jsenv/url-meta": "8.5.2",
Expand Down
5 changes: 4 additions & 1 deletion packages/related/plugin-preact/src/jsenv_plugin_preact.js
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ export const jsenvPluginPreact = ({
],
]
: []),
...(jsxEnabled && urlInfo.context.dev
? ["@babel/plugin-transform-react-jsx-source"]
: []),
...(hookNamesEnabled ? ["babel-plugin-transform-hook-names"] : []),
...(refreshEnabled ? ["@prefresh/babel-plugin"] : []),
],
Expand Down Expand Up @@ -172,7 +175,7 @@ export const jsenvPluginPreact = ({
source: "jsenv_preact",
});
code = afterJsxReplace.content;
// can't do this: ths resulting sourcemap is wrong by a few lines
// can't compose import maps: the resulting sourcemap is wrong by a few lines
// (3-4) and would makes debugging experience close to horrible
// map = await composeTwoSourcemaps(map, afterJsxReplace.sourcemap);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/related/plugin-react/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@jsenv/plugin-react",
"version": "1.5.54",
"version": "1.5.55",
"license": "MIT",
"repository": {
"type": "git",
Expand All @@ -23,6 +23,7 @@
"dependencies": {
"@babel/plugin-transform-react-jsx": "7.25.9",
"@babel/plugin-transform-react-jsx-development": "7.25.9",
"@babel/plugin-transform-react-jsx-source": "7.25.9",
"@jsenv/ast": "6.4.1",
"@jsenv/plugin-commonjs": "2.9.11",
"@jsenv/sourcemap": "1.2.29",
Expand Down
29 changes: 20 additions & 9 deletions packages/related/plugin-react/src/jsenv_plugin_react.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,11 +99,14 @@ const jsenvPluginJsxAndRefresh = ({
],
]
: []),
...(jsxEnabled && urlInfo.context.dev
? ["@babel/plugin-transform-react-jsx-source"]
: []),
...(refreshEnabled
? [["react-refresh/babel", { skipEnvCheck: true }]]
: []),
];
const { code, map } = await applyBabelPlugins({
let { code, map } = await applyBabelPlugins({
babelPlugins,
input: urlInfo.content,
inputIsJsModule: true,
Expand Down Expand Up @@ -141,6 +144,13 @@ const jsenvPluginJsxAndRefresh = ({
index = code.indexOf(importSpecifier, index + 1);
}
}
const afterJsxReplace = magicSource.toContentAndSourcemap({
source: "jsenv_preact",
});
code = afterJsxReplace.content;
// can't compose import maps: the resulting sourcemap is wrong by a few lines
// (3-4) and would makes debugging experience close to horrible
// map = await composeTwoSourcemaps(map, afterJsxReplace.sourcemap);
}
if (refreshEnabled) {
const hasReg = /\$RefreshReg\$\(/.test(code);
Expand All @@ -151,26 +161,27 @@ const jsenvPluginJsxAndRefresh = ({
expectedType: "js_module",
specifier: "@jsenv/plugin-react/src/client/react_refresh.js",
});
magicSource.prepend(`import { installReactRefresh } from ${
const prelude = `import { installReactRefresh } from ${
reactRefreshClientReference.generatedSpecifier
}
};
const __react_refresh__ = installReactRefresh(${JSON.stringify(urlInfo.url)});
`);
`;
code = `${prelude.replace(/\n/g, "")}${code}`;
if (hasReg) {
magicSource.append(`
code = `${code}
__react_refresh__.end();
import.meta.hot.accept(__react_refresh__.acceptCallback);`);
import.meta.hot.accept(__react_refresh__.acceptCallback);`;
}
}
}
const result = magicSource.toContentAndSourcemap();
return {
content: result.content,
content: code,
sourcemap: map,
// "no sourcemap is better than wrong sourcemap":
// I don't know exactly what is resulting in bad sourcemaps
// but I suspect hooknames or prefresh to be responsible
sourcemapIsWrong: jsxEnabled && refreshEnabled,
// sourcemapIsWrong: jsxEnabled && refreshEnabled,
};
},
},
Expand Down

0 comments on commit 93008f3

Please sign in to comment.