@vitejs/plugin-react
Advanced tools
Comparing version 1.1.0-beta.0 to 1.1.0-beta.1
@@ -0,1 +1,13 @@ | ||
# [1.1.0-beta.1](https://github.com/vitejs/vite/compare/plugin-react@1.1.0-beta.0...plugin-react@1.1.0-beta.1) (2021-11-19) | ||
### Bug Fixes | ||
* **plugin-react:** apply `babel.plugins` to project files only ([#5255](https://github.com/vitejs/vite/issues/5255)) ([377d0be](https://github.com/vitejs/vite/commit/377d0be5cf85a50240e160beaaafda77b7199452)) | ||
* **plugin-react:** remove querystring from sourcemap filename ([#5760](https://github.com/vitejs/vite/issues/5760)) ([d93a9fa](https://github.com/vitejs/vite/commit/d93a9fab8986f3659e79d7b0b065e99ef625a5dd)) | ||
* **plugin-react:** restore usage of extension instead of id ([#5761](https://github.com/vitejs/vite/issues/5761)) ([59471b1](https://github.com/vitejs/vite/commit/59471b186612d3da0083543e23d660747d3287f3)) | ||
* **plugin-react:** uncompiled JSX in linked pkgs ([#5669](https://github.com/vitejs/vite/issues/5669)) ([41a7c9c](https://github.com/vitejs/vite/commit/41a7c9ccfbc1a7bc60aec672056eac3966ddd036)) | ||
# [1.1.0-beta.0](https://github.com/vitejs/vite/compare/plugin-react@1.0.6...plugin-react@1.1.0-beta.0) (2021-10-28) | ||
@@ -2,0 +14,0 @@ |
@@ -286,4 +286,11 @@ var __create = Object.create; | ||
var babelRestoreJSX; | ||
var jsxNotFound = [null, false]; | ||
async function restoreJSX(babel2, code, filename) { | ||
if (filename.includes("/.vite/react-dom.js")) { | ||
return jsxNotFound; | ||
} | ||
const [reactAlias, isCommonJS] = parseReactAlias(code); | ||
if (!reactAlias) { | ||
return jsxNotFound; | ||
} | ||
const reactJsxRE = new RegExp("\\b" + reactAlias + "\\.(createElement|Fragment)\\b", "g"); | ||
@@ -296,3 +303,3 @@ let hasCompiledJsx = false; | ||
if (!hasCompiledJsx) { | ||
return [null, false]; | ||
return jsxNotFound; | ||
} | ||
@@ -357,3 +364,3 @@ code = code.replace(/createElement\(Fragment,/g, "createElement(React.Fragment,"); | ||
async transform(code, id, options) { | ||
var _a2, _b2; | ||
var _a2, _b2, _c2, _d; | ||
const ssr = typeof options === "boolean" ? options : (options == null ? void 0 : options.ssr) === true; | ||
@@ -363,22 +370,9 @@ const [filepath, querystring = ""] = id.split("?"); | ||
if (/\.(mjs|[tj]sx?)$/.test(extension)) { | ||
const plugins = [...userPlugins]; | ||
const parserPlugins = [ | ||
...userParserPlugins, | ||
"importMeta", | ||
"topLevelAwait", | ||
"classProperties", | ||
"classPrivateProperties", | ||
"classPrivateMethods" | ||
]; | ||
if (!extension.endsWith(".ts")) { | ||
parserPlugins.push("jsx"); | ||
} | ||
const isTypeScript = /\.tsx?$/.test(extension); | ||
if (isTypeScript) { | ||
parserPlugins.push("typescript"); | ||
} | ||
const isNodeModules = id.includes("node_modules"); | ||
const isJSX = extension.endsWith("x"); | ||
const isNodeModules = id.includes("/node_modules/"); | ||
const isProjectFile = !isNodeModules && (id[0] === "\0" || id.startsWith(projectRoot + "/")); | ||
const plugins = isProjectFile ? [...userPlugins] : []; | ||
let useFastRefresh = false; | ||
if (!skipFastRefresh && !ssr && !isNodeModules) { | ||
const isReactModule = extension.endsWith("x") || code.includes("react"); | ||
const isReactModule = isJSX || code.includes("react"); | ||
if (isReactModule && filter(id)) { | ||
@@ -393,6 +387,6 @@ useFastRefresh = true; | ||
let ast; | ||
if (isNodeModules || extension.endsWith("x")) { | ||
if (!isProjectFile || isJSX) { | ||
if (useAutomaticRuntime) { | ||
const [restoredAst, isCommonJS] = isNodeModules ? await restoreJSX(babel, code, id) : [null, false]; | ||
if (!isNodeModules || (ast = restoredAst)) { | ||
const [restoredAst, isCommonJS] = !isProjectFile && !isJSX ? await restoreJSX(babel, code, id) : [null, false]; | ||
if (isJSX || (ast = restoredAst)) { | ||
plugins.push([ | ||
@@ -409,3 +403,3 @@ await loadPlugin("@babel/plugin-transform-react-jsx" + (isProduction ? "" : "-development")), | ||
} | ||
} else if (!isNodeModules) { | ||
} else if (isProjectFile) { | ||
if (!isProduction) { | ||
@@ -419,2 +413,20 @@ plugins.push(await loadPlugin("@babel/plugin-transform-react-jsx-self"), await loadPlugin("@babel/plugin-transform-react-jsx-source")); | ||
} | ||
const shouldSkip = !plugins.length && !((_a2 = opts.babel) == null ? void 0 : _a2.configFile) && !(isProjectFile && ((_b2 = opts.babel) == null ? void 0 : _b2.babelrc)); | ||
if (shouldSkip) { | ||
return; | ||
} | ||
const parserPlugins = [ | ||
...userParserPlugins, | ||
"importMeta", | ||
"topLevelAwait", | ||
"classProperties", | ||
"classPrivateProperties", | ||
"classPrivateMethods" | ||
]; | ||
if (!extension.endsWith(".ts")) { | ||
parserPlugins.push("jsx"); | ||
} | ||
if (/\.tsx?$/.test(extension)) { | ||
parserPlugins.push("typescript"); | ||
} | ||
const isReasonReact = extension.endsWith(".bs.js"); | ||
@@ -428,4 +440,4 @@ const babelOpts = __spreadProps(__spreadValues({ | ||
filename: id, | ||
sourceFileName: id, | ||
parserOpts: __spreadProps(__spreadValues({}, (_a2 = opts.babel) == null ? void 0 : _a2.parserOpts), { | ||
sourceFileName: filepath, | ||
parserOpts: __spreadProps(__spreadValues({}, (_c2 = opts.babel) == null ? void 0 : _c2.parserOpts), { | ||
sourceType: "module", | ||
@@ -435,3 +447,3 @@ allowAwaitOutsideFunction: true, | ||
}), | ||
generatorOpts: __spreadProps(__spreadValues({}, (_b2 = opts.babel) == null ? void 0 : _b2.generatorOpts), { | ||
generatorOpts: __spreadProps(__spreadValues({}, (_d = opts.babel) == null ? void 0 : _d.generatorOpts), { | ||
decoratorsBeforeExport: true | ||
@@ -438,0 +450,0 @@ }), |
{ | ||
"name": "@vitejs/plugin-react", | ||
"version": "1.1.0-beta.0", | ||
"version": "1.1.0-beta.1", | ||
"license": "MIT", | ||
@@ -36,11 +36,11 @@ "author": "Evan You", | ||
"dependencies": { | ||
"@babel/core": "^7.15.5", | ||
"@babel/plugin-transform-react-jsx": "^7.14.9", | ||
"@babel/plugin-transform-react-jsx-development": "^7.14.5", | ||
"@babel/plugin-transform-react-jsx-self": "^7.14.9", | ||
"@babel/plugin-transform-react-jsx-source": "^7.14.5", | ||
"@babel/core": "^7.16.0", | ||
"@babel/plugin-transform-react-jsx": "^7.16.0", | ||
"@babel/plugin-transform-react-jsx-development": "^7.16.0", | ||
"@babel/plugin-transform-react-jsx-self": "^7.16.0", | ||
"@babel/plugin-transform-react-jsx-source": "^7.16.0", | ||
"@rollup/pluginutils": "^4.1.1", | ||
"react-refresh": "^0.10.0", | ||
"react-refresh": "^0.11.0", | ||
"resolve": "^1.20.0" | ||
} | ||
} |
@@ -111,32 +111,13 @@ import type { ParserOptions, TransformOptions, types as t } from '@babel/core' | ||
if (/\.(mjs|[tj]sx?)$/.test(extension)) { | ||
const plugins = [...userPlugins] | ||
const isJSX = extension.endsWith('x') | ||
const isNodeModules = id.includes('/node_modules/') | ||
const isProjectFile = | ||
!isNodeModules && (id[0] === '\0' || id.startsWith(projectRoot + '/')) | ||
const parserPlugins: typeof userParserPlugins = [ | ||
...userParserPlugins, | ||
'importMeta', | ||
// This plugin is applied before esbuild transforms the code, | ||
// so we need to enable some stage 3 syntax that is supported in | ||
// TypeScript and some environments already. | ||
'topLevelAwait', | ||
'classProperties', | ||
'classPrivateProperties', | ||
'classPrivateMethods' | ||
] | ||
const plugins = isProjectFile ? [...userPlugins] : [] | ||
if (!extension.endsWith('.ts')) { | ||
parserPlugins.push('jsx') | ||
} | ||
const isTypeScript = /\.tsx?$/.test(extension) | ||
if (isTypeScript) { | ||
parserPlugins.push('typescript') | ||
} | ||
const isNodeModules = id.includes('node_modules') | ||
let useFastRefresh = false | ||
if (!skipFastRefresh && !ssr && !isNodeModules) { | ||
// Modules with .js or .ts extension must import React. | ||
const isReactModule = | ||
extension.endsWith('x') || code.includes('react') | ||
const isReactModule = isJSX || code.includes('react') | ||
if (isReactModule && filter(id)) { | ||
@@ -152,3 +133,3 @@ useFastRefresh = true | ||
let ast: t.File | null | undefined | ||
if (isNodeModules || extension.endsWith('x')) { | ||
if (!isProjectFile || isJSX) { | ||
if (useAutomaticRuntime) { | ||
@@ -158,7 +139,8 @@ // By reverse-compiling "React.createElement" calls into JSX, | ||
// automatic runtime! | ||
const [restoredAst, isCommonJS] = isNodeModules | ||
? await restoreJSX(babel, code, id) | ||
: [null, false] | ||
const [restoredAst, isCommonJS] = | ||
!isProjectFile && !isJSX | ||
? await restoreJSX(babel, code, id) | ||
: [null, false] | ||
if (!isNodeModules || (ast = restoredAst)) { | ||
if (isJSX || (ast = restoredAst)) { | ||
plugins.push([ | ||
@@ -180,3 +162,3 @@ await loadPlugin( | ||
} | ||
} else if (!isNodeModules) { | ||
} else if (isProjectFile) { | ||
// These plugins are only needed for the classic runtime. | ||
@@ -198,2 +180,35 @@ if (!isProduction) { | ||
// Plugins defined through this Vite plugin are only applied | ||
// to modules within the project root, but "babel.config.js" | ||
// files can define plugins that need to be applied to every | ||
// module, including node_modules and linked packages. | ||
const shouldSkip = | ||
!plugins.length && | ||
!opts.babel?.configFile && | ||
!(isProjectFile && opts.babel?.babelrc) | ||
if (shouldSkip) { | ||
return // Avoid parsing if no plugins exist. | ||
} | ||
const parserPlugins: typeof userParserPlugins = [ | ||
...userParserPlugins, | ||
'importMeta', | ||
// This plugin is applied before esbuild transforms the code, | ||
// so we need to enable some stage 3 syntax that is supported in | ||
// TypeScript and some environments already. | ||
'topLevelAwait', | ||
'classProperties', | ||
'classPrivateProperties', | ||
'classPrivateMethods' | ||
] | ||
if (!extension.endsWith('.ts')) { | ||
parserPlugins.push('jsx') | ||
} | ||
if (/\.tsx?$/.test(extension)) { | ||
parserPlugins.push('typescript') | ||
} | ||
const isReasonReact = extension.endsWith('.bs.js') | ||
@@ -208,3 +223,3 @@ | ||
filename: id, | ||
sourceFileName: id, | ||
sourceFileName: filepath, | ||
parserOpts: { | ||
@@ -211,0 +226,0 @@ ...opts.babel?.parserOpts, |
@@ -7,2 +7,4 @@ import type { PluginItem, types as t } from '@babel/core' | ||
const jsxNotFound: RestoredJSX = [null, false] | ||
/** Restore JSX from `React.createElement` calls */ | ||
@@ -14,3 +16,13 @@ export async function restoreJSX( | ||
): Promise<RestoredJSX> { | ||
// Avoid parsing the optimized react-dom since it will never | ||
// contain compiled JSX and it's a pretty big file (800kb). | ||
if (filename.includes('/.vite/react-dom.js')) { | ||
return jsxNotFound | ||
} | ||
const [reactAlias, isCommonJS] = parseReactAlias(code) | ||
if (!reactAlias) { | ||
return jsxNotFound | ||
} | ||
const reactJsxRE = new RegExp( | ||
@@ -29,3 +41,3 @@ '\\b' + reactAlias + '\\.(createElement|Fragment)\\b', | ||
if (!hasCompiledJsx) { | ||
return [null, false] | ||
return jsxNotFound | ||
} | ||
@@ -32,0 +44,0 @@ |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
56791
1347
+ Addedreact-refresh@0.11.0(transitive)
- Removedreact-refresh@0.10.0(transitive)
Updated@babel/core@^7.16.0
Updated@babel/plugin-transform-react-jsx-development@^7.16.0
Updatedreact-refresh@^0.11.0