From c96f6ed377c1b191ee76c7d69d6df6742eb9145e Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Mon, 8 Aug 2022 17:17:28 -0700 Subject: [PATCH 1/7] feat: transform jsx with esbuild instead of babel --- package.json | 2 +- packages/plugin-react/src/index.ts | 30 +-- packages/vite/package.json | 2 +- pnpm-lock.yaml | 361 +++++++---------------------- 4 files changed, 99 insertions(+), 296 deletions(-) diff --git a/package.json b/package.json index d5e656455da117..3c4885ba78e8a2 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "@typescript-eslint/parser": "^5.31.0", "conventional-changelog-cli": "^2.2.2", "cross-env": "^7.0.3", - "esbuild": "^0.14.47", + "esbuild": "^0.14.53", "eslint": "^8.21.0", "eslint-define-config": "^1.6.0", "eslint-plugin-import": "^2.26.0", diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index f1adf81510bcef..970d7fef9cdc4e 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -122,13 +122,27 @@ export default function viteReact(opts: Options = {}): PluginOption[] { const viteBabel: Plugin = { name: 'vite:react-babel', enforce: 'pre', - config() { + config(_, { mode }) { + const isProduction = + (process.env.NODE_ENV || process.env.VITE_USER_NODE_ENV || mode) === + 'production' + if (opts.jsxRuntime === 'classic') { return { esbuild: { logOverride: { 'this-is-undefined-in-esm': 'silent' - } + }, + jsx: 'transform', + jsxImportSource: opts.jsxImportSource + } + } + } else { + return { + esbuild: { + jsxDev: !isProduction, + jsx: 'automatic', + jsxImportSource: opts.jsxImportSource } } } @@ -240,18 +254,6 @@ export default function viteReact(opts: Options = {}): PluginOption[] { : [null, false] if (isJSX || (ast = restoredAst)) { - plugins.push([ - await loadPlugin( - '@babel/plugin-transform-react-jsx' + - (isProduction ? '' : '-development') - ), - { - runtime: 'automatic', - importSource: opts.jsxImportSource, - pure: opts.jsxPure !== false - } - ]) - // Avoid inserting `import` statements into CJS modules. if (isCommonJS) { plugins.push(babelImportToRequire) diff --git a/packages/vite/package.json b/packages/vite/package.json index 199b53d81ce30a..82242bd7f2d24f 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -58,7 +58,7 @@ }, "//": "READ CONTRIBUTING.md to understand what to put under deps vs. devDeps!", "dependencies": { - "esbuild": "^0.14.47", + "esbuild": "^0.14.53", "postcss": "^8.4.14", "resolve": "^1.22.1", "rollup": "^2.75.6" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3b00d19680be01..2c00095ef045ca 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,7 +35,7 @@ importers: '@typescript-eslint/parser': ^5.31.0 conventional-changelog-cli: ^2.2.2 cross-env: ^7.0.3 - esbuild: ^0.14.47 + esbuild: ^0.14.53 eslint: ^8.21.0 eslint-define-config: ^1.6.0 eslint-plugin-import: ^2.26.0 @@ -88,7 +88,7 @@ importers: '@typescript-eslint/parser': 5.31.0_vxmhu3tyr7cxhd2vxjkubkv7im conventional-changelog-cli: 2.2.2 cross-env: 7.0.3 - esbuild: 0.14.47 + esbuild: 0.14.53 eslint: 8.21.0 eslint-define-config: 1.6.0 eslint-plugin-import: 2.26.0_eygs4xj346vriqwkjbwjuyviaa @@ -227,7 +227,7 @@ importers: dotenv: ^14.3.2 dotenv-expand: ^5.1.0 es-module-lexer: ^0.10.5 - esbuild: ^0.14.47 + esbuild: ^0.14.53 estree-walker: ^3.0.1 etag: ^1.8.1 fast-glob: ^3.2.11 @@ -262,7 +262,7 @@ importers: ufo: ^0.8.5 ws: ^8.8.1 dependencies: - esbuild: 0.14.47 + esbuild: 0.14.53 postcss: 8.4.14 resolve: 1.22.1 rollup: 2.75.6 @@ -2100,7 +2100,7 @@ packages: /@esbuild-kit/core-utils/2.1.0: resolution: {integrity: sha512-fZirrc2KjeTumVjE4bpleWOk2gD83b7WuGeQqOceKFQL+heNKKkNB5G5pekOUTLzfSBc0hP7hCSBoD9TuR0hLw==} dependencies: - esbuild: 0.14.47 + esbuild: 0.14.53 source-map-support: 0.5.21 dev: true @@ -2111,6 +2111,14 @@ packages: get-tsconfig: 4.1.0 dev: true + /@esbuild/linux-loong64/0.14.53: + resolution: {integrity: sha512-W2dAL6Bnyn4xa/QRSU3ilIK4EzD5wgYXKXJiS1HDF5vU3675qc2bvFyLwbUcdmssDveyndy7FbitrCoiV/eMLg==} + engines: {node: '>=12'} + cpu: [loong64] + os: [linux] + requiresBuild: true + optional: true + /@eslint/eslintrc/1.3.0: resolution: {integrity: sha512-UWW0TMTmk2d7hLcWD1/e2g5HDM/HQ3csaLSqXCfqwh4uNDuNqlaKWXmEsL4Cs41Z0KnILNvwbHAah3C2yt06kw==} engines: {node: ^12.22.0 || ^14.17.0 || >=16.0.0} @@ -4351,400 +4359,193 @@ packages: ext: 1.6.0 dev: false - /esbuild-android-64/0.14.47: - resolution: {integrity: sha512-R13Bd9+tqLVFndncMHssZrPWe6/0Kpv2/dt4aA69soX4PRxlzsVpCvoJeFE8sOEoeVEiBkI0myjlkDodXlHa0g==} + /esbuild-android-64/0.14.53: + resolution: {integrity: sha512-fIL93sOTnEU+NrTAVMIKiAw0YH22HWCAgg4N4Z6zov2t0kY9RAJ50zY9ZMCQ+RT6bnOfDt8gCTnt/RaSNA2yRA==} engines: {node: '>=12'} cpu: [x64] os: [android] requiresBuild: true optional: true - /esbuild-android-64/0.14.50: - resolution: {integrity: sha512-H7iUEm7gUJHzidsBlFPGF6FTExazcgXL/46xxLo6i6bMtPim6ZmXyTccS8yOMpy6HAC6dPZ/JCQqrkkin69n6Q==} - engines: {node: '>=12'} - cpu: [x64] - os: [android] - requiresBuild: true - dev: true - optional: true - - /esbuild-android-arm64/0.14.47: - resolution: {integrity: sha512-OkwOjj7ts4lBp/TL6hdd8HftIzOy/pdtbrNA4+0oVWgGG64HrdVzAF5gxtJufAPOsEjkyh1oIYvKAUinKKQRSQ==} + /esbuild-android-arm64/0.14.53: + resolution: {integrity: sha512-PC7KaF1v0h/nWpvlU1UMN7dzB54cBH8qSsm7S9mkwFA1BXpaEOufCg8hdoEI1jep0KeO/rjZVWrsH8+q28T77A==} engines: {node: '>=12'} cpu: [arm64] os: [android] requiresBuild: true optional: true - /esbuild-android-arm64/0.14.50: - resolution: {integrity: sha512-NFaoqEwa+OYfoYVpQWDMdKII7wZZkAjtJFo1WdnBeCYlYikvUhTnf2aPwPu5qEAw/ie1NYK0yn3cafwP+kP+OQ==} - engines: {node: '>=12'} - cpu: [arm64] - os: [android] - requiresBuild: true - dev: true - optional: true - - /esbuild-darwin-64/0.14.47: - resolution: {integrity: sha512-R6oaW0y5/u6Eccti/TS6c/2c1xYTb1izwK3gajJwi4vIfNs1s8B1dQzI1UiC9T61YovOQVuePDcfqHLT3mUZJA==} - engines: {node: '>=12'} - cpu: [x64] - os: [darwin] - requiresBuild: true - optional: true - - /esbuild-darwin-64/0.14.50: - resolution: {integrity: sha512-gDQsCvGnZiJv9cfdO48QqxkRV8oKAXgR2CGp7TdIpccwFdJMHf8hyIJhMW/05b/HJjET/26Us27Jx91BFfEVSA==} + /esbuild-darwin-64/0.14.53: + resolution: {integrity: sha512-gE7P5wlnkX4d4PKvLBUgmhZXvL7lzGRLri17/+CmmCzfncIgq8lOBvxGMiQ4xazplhxq+72TEohyFMZLFxuWvg==} engines: {node: '>=12'} cpu: [x64] os: [darwin] requiresBuild: true - dev: true optional: true - /esbuild-darwin-arm64/0.14.47: - resolution: {integrity: sha512-seCmearlQyvdvM/noz1L9+qblC5vcBrhUaOoLEDDoLInF/VQ9IkobGiLlyTPYP5dW1YD4LXhtBgOyevoIHGGnw==} + /esbuild-darwin-arm64/0.14.53: + resolution: {integrity: sha512-otJwDU3hnI15Q98PX4MJbknSZ/WSR1I45il7gcxcECXzfN4Mrpft5hBDHXNRnCh+5858uPXBXA1Vaz2jVWLaIA==} engines: {node: '>=12'} cpu: [arm64] os: [darwin] requiresBuild: true optional: true - /esbuild-darwin-arm64/0.14.50: - resolution: {integrity: sha512-36nNs5OjKIb/Q50Sgp8+rYW/PqirRiFN0NFc9hEvgPzNJxeJedktXwzfJSln4EcRFRh5Vz4IlqFRScp+aiBBzA==} - engines: {node: '>=12'} - cpu: [arm64] - os: [darwin] - requiresBuild: true - dev: true - optional: true - - /esbuild-freebsd-64/0.14.47: - resolution: {integrity: sha512-ZH8K2Q8/Ux5kXXvQMDsJcxvkIwut69KVrYQhza/ptkW50DC089bCVrJZZ3sKzIoOx+YPTrmsZvqeZERjyYrlvQ==} + /esbuild-freebsd-64/0.14.53: + resolution: {integrity: sha512-WkdJa8iyrGHyKiPF4lk0MiOF87Q2SkE+i+8D4Cazq3/iqmGPJ6u49je300MFi5I2eUsQCkaOWhpCVQMTKGww2w==} engines: {node: '>=12'} cpu: [x64] os: [freebsd] requiresBuild: true optional: true - /esbuild-freebsd-64/0.14.50: - resolution: {integrity: sha512-/1pHHCUem8e/R86/uR+4v5diI2CtBdiWKiqGuPa9b/0x3Nwdh5AOH7lj+8823C6uX1e0ufwkSLkS+aFZiBCWxA==} - engines: {node: '>=12'} - cpu: [x64] - os: [freebsd] - requiresBuild: true - dev: true - optional: true - - /esbuild-freebsd-arm64/0.14.47: - resolution: {integrity: sha512-ZJMQAJQsIOhn3XTm7MPQfCzEu5b9STNC+s90zMWe2afy9EwnHV7Ov7ohEMv2lyWlc2pjqLW8QJnz2r0KZmeAEQ==} - engines: {node: '>=12'} - cpu: [arm64] - os: [freebsd] - requiresBuild: true - optional: true - - /esbuild-freebsd-arm64/0.14.50: - resolution: {integrity: sha512-iKwUVMQztnPZe5pUYHdMkRc9aSpvoV1mkuHlCoPtxZA3V+Kg/ptpzkcSY+fKd0kuom+l6Rc93k0UPVkP7xoqrw==} + /esbuild-freebsd-arm64/0.14.53: + resolution: {integrity: sha512-9T7WwCuV30NAx0SyQpw8edbKvbKELnnm1FHg7gbSYaatH+c8WJW10g/OdM7JYnv7qkimw2ZTtSA+NokOLd2ydQ==} engines: {node: '>=12'} cpu: [arm64] os: [freebsd] requiresBuild: true - dev: true optional: true - /esbuild-linux-32/0.14.47: - resolution: {integrity: sha512-FxZOCKoEDPRYvq300lsWCTv1kcHgiiZfNrPtEhFAiqD7QZaXrad8LxyJ8fXGcWzIFzRiYZVtB3ttvITBvAFhKw==} + /esbuild-linux-32/0.14.53: + resolution: {integrity: sha512-VGanLBg5en2LfGDgLEUxQko2lqsOS7MTEWUi8x91YmsHNyzJVT/WApbFFx3MQGhkf+XdimVhpyo5/G0PBY91zg==} engines: {node: '>=12'} cpu: [ia32] os: [linux] requiresBuild: true optional: true - /esbuild-linux-32/0.14.50: - resolution: {integrity: sha512-sWUwvf3uz7dFOpLzYuih+WQ7dRycrBWHCdoXJ4I4XdMxEHCECd8b7a9N9u7FzT6XR2gHPk9EzvchQUtiEMRwqw==} - engines: {node: '>=12'} - cpu: [ia32] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /esbuild-linux-64/0.14.47: - resolution: {integrity: sha512-nFNOk9vWVfvWYF9YNYksZptgQAdstnDCMtR6m42l5Wfugbzu11VpMCY9XrD4yFxvPo9zmzcoUL/88y0lfJZJJw==} + /esbuild-linux-64/0.14.53: + resolution: {integrity: sha512-pP/FA55j/fzAV7N9DF31meAyjOH6Bjuo3aSKPh26+RW85ZEtbJv9nhoxmGTd9FOqjx59Tc1ZbrJabuiXlMwuZQ==} engines: {node: '>=12'} cpu: [x64] os: [linux] requiresBuild: true optional: true - /esbuild-linux-64/0.14.50: - resolution: {integrity: sha512-u0PQxPhaeI629t4Y3EEcQ0wmWG+tC/LpP2K7yDFvwuPq0jSQ8SIN+ARNYfRjGW15O2we3XJvklbGV0wRuUCPig==} - engines: {node: '>=12'} - cpu: [x64] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /esbuild-linux-arm/0.14.47: - resolution: {integrity: sha512-ZGE1Bqg/gPRXrBpgpvH81tQHpiaGxa8c9Rx/XOylkIl2ypLuOcawXEAo8ls+5DFCcRGt/o3sV+PzpAFZobOsmA==} - engines: {node: '>=12'} - cpu: [arm] - os: [linux] - requiresBuild: true - optional: true - - /esbuild-linux-arm/0.14.50: - resolution: {integrity: sha512-VALZq13bhmFJYFE/mLEb+9A0w5vo8z+YDVOWeaf9vOTrSC31RohRIwtxXBnVJ7YKLYfEMzcgFYf+OFln3Y0cWg==} + /esbuild-linux-arm/0.14.53: + resolution: {integrity: sha512-/u81NGAVZMopbmzd21Nu/wvnKQK3pT4CrvQ8BTje1STXcQAGnfyKgQlj3m0j2BzYbvQxSy+TMck4TNV2onvoPA==} engines: {node: '>=12'} cpu: [arm] os: [linux] requiresBuild: true - dev: true optional: true - /esbuild-linux-arm64/0.14.47: - resolution: {integrity: sha512-ywfme6HVrhWcevzmsufjd4iT3PxTfCX9HOdxA7Hd+/ZM23Y9nXeb+vG6AyA6jgq/JovkcqRHcL9XwRNpWG6XRw==} + /esbuild-linux-arm64/0.14.53: + resolution: {integrity: sha512-GDmWITT+PMsjCA6/lByYk7NyFssW4Q6in32iPkpjZ/ytSyH+xeEx8q7HG3AhWH6heemEYEWpTll/eui3jwlSnw==} engines: {node: '>=12'} cpu: [arm64] os: [linux] requiresBuild: true optional: true - /esbuild-linux-arm64/0.14.50: - resolution: {integrity: sha512-ZyfoNgsTftD7Rp5S7La5auomKdNeB3Ck+kSKXC4pp96VnHyYGjHHXWIlcbH8i+efRn9brszo1/Thl1qn8RqmhQ==} - engines: {node: '>=12'} - cpu: [arm64] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /esbuild-linux-mips64le/0.14.47: - resolution: {integrity: sha512-mg3D8YndZ1LvUiEdDYR3OsmeyAew4MA/dvaEJxvyygahWmpv1SlEEnhEZlhPokjsUMfRagzsEF/d/2XF+kTQGg==} - engines: {node: '>=12'} - cpu: [mips64el] - os: [linux] - requiresBuild: true - optional: true - - /esbuild-linux-mips64le/0.14.50: - resolution: {integrity: sha512-ygo31Vxn/WrmjKCHkBoutOlFG5yM9J2UhzHb0oWD9O61dGg+Hzjz9hjf5cmM7FBhAzdpOdEWHIrVOg2YAi6rTw==} + /esbuild-linux-mips64le/0.14.53: + resolution: {integrity: sha512-d6/XHIQW714gSSp6tOOX2UscedVobELvQlPMkInhx1NPz4ThZI9uNLQ4qQJHGBGKGfu+rtJsxM4NVHLhnNRdWQ==} engines: {node: '>=12'} cpu: [mips64el] os: [linux] requiresBuild: true - dev: true optional: true - /esbuild-linux-ppc64le/0.14.47: - resolution: {integrity: sha512-WER+f3+szmnZiWoK6AsrTKGoJoErG2LlauSmk73LEZFQ/iWC+KhhDsOkn1xBUpzXWsxN9THmQFltLoaFEH8F8w==} + /esbuild-linux-ppc64le/0.14.53: + resolution: {integrity: sha512-ndnJmniKPCB52m+r6BtHHLAOXw+xBCWIxNnedbIpuREOcbSU/AlyM/2dA3BmUQhsHdb4w3amD5U2s91TJ3MzzA==} engines: {node: '>=12'} cpu: [ppc64] os: [linux] requiresBuild: true optional: true - /esbuild-linux-ppc64le/0.14.50: - resolution: {integrity: sha512-xWCKU5UaiTUT6Wz/O7GKP9KWdfbsb7vhfgQzRfX4ahh5NZV4ozZ4+SdzYG8WxetsLy84UzLX3Pi++xpVn1OkFQ==} - engines: {node: '>=12'} - cpu: [ppc64] - os: [linux] - requiresBuild: true - dev: true - optional: true - - /esbuild-linux-riscv64/0.14.47: - resolution: {integrity: sha512-1fI6bP3A3rvI9BsaaXbMoaOjLE3lVkJtLxsgLHqlBhLlBVY7UqffWBvkrX/9zfPhhVMd9ZRFiaqXnB1T7BsL2g==} - engines: {node: '>=12'} - cpu: [riscv64] - os: [linux] - requiresBuild: true - optional: true - - /esbuild-linux-riscv64/0.14.50: - resolution: {integrity: sha512-0+dsneSEihZTopoO9B6Z6K4j3uI7EdxBP7YSF5rTwUgCID+wHD3vM1gGT0m+pjCW+NOacU9kH/WE9N686FHAJg==} + /esbuild-linux-riscv64/0.14.53: + resolution: {integrity: sha512-yG2sVH+QSix6ct4lIzJj329iJF3MhloLE6/vKMQAAd26UVPVkhMFqFopY+9kCgYsdeWvXdPgmyOuKa48Y7+/EQ==} engines: {node: '>=12'} cpu: [riscv64] os: [linux] requiresBuild: true - dev: true - optional: true - - /esbuild-linux-s390x/0.14.47: - resolution: {integrity: sha512-eZrWzy0xFAhki1CWRGnhsHVz7IlSKX6yT2tj2Eg8lhAwlRE5E96Hsb0M1mPSE1dHGpt1QVwwVivXIAacF/G6mw==} - engines: {node: '>=12'} - cpu: [s390x] - os: [linux] - requiresBuild: true optional: true - /esbuild-linux-s390x/0.14.50: - resolution: {integrity: sha512-tVjqcu8o0P9H4StwbIhL1sQYm5mWATlodKB6dpEZFkcyTI8kfIGWiWcrGmkNGH2i1kBUOsdlBafPxR3nzp3TDA==} + /esbuild-linux-s390x/0.14.53: + resolution: {integrity: sha512-OCJlgdkB+XPYndHmw6uZT7jcYgzmx9K+28PVdOa/eLjdoYkeAFvH5hTwX4AXGLZLH09tpl4bVsEtvuyUldaNCg==} engines: {node: '>=12'} cpu: [s390x] os: [linux] requiresBuild: true - dev: true optional: true - /esbuild-netbsd-64/0.14.47: - resolution: {integrity: sha512-Qjdjr+KQQVH5Q2Q1r6HBYswFTToPpss3gqCiSw2Fpq/ua8+eXSQyAMG+UvULPqXceOwpnPo4smyZyHdlkcPppQ==} + /esbuild-netbsd-64/0.14.53: + resolution: {integrity: sha512-gp2SB+Efc7MhMdWV2+pmIs/Ja/Mi5rjw+wlDmmbIn68VGXBleNgiEZG+eV2SRS0kJEUyHNedDtwRIMzaohWedQ==} engines: {node: '>=12'} cpu: [x64] os: [netbsd] requiresBuild: true optional: true - /esbuild-netbsd-64/0.14.50: - resolution: {integrity: sha512-0R/glfqAQ2q6MHDf7YJw/TulibugjizBxyPvZIcorH0Mb7vSimdHy0XF5uCba5CKt+r4wjax1mvO9lZ4jiAhEg==} - engines: {node: '>=12'} - cpu: [x64] - os: [netbsd] - requiresBuild: true - dev: true - optional: true - - /esbuild-openbsd-64/0.14.47: - resolution: {integrity: sha512-QpgN8ofL7B9z8g5zZqJE+eFvD1LehRlxr25PBkjyyasakm4599iroUpaj96rdqRlO2ShuyqwJdr+oNqWwTUmQw==} + /esbuild-openbsd-64/0.14.53: + resolution: {integrity: sha512-eKQ30ZWe+WTZmteDYg8S+YjHV5s4iTxeSGhJKJajFfQx9TLZJvsJX0/paqwP51GicOUruFpSUAs2NCc0a4ivQQ==} engines: {node: '>=12'} cpu: [x64] os: [openbsd] requiresBuild: true optional: true - /esbuild-openbsd-64/0.14.50: - resolution: {integrity: sha512-7PAtmrR5mDOFubXIkuxYQ4bdNS6XCK8AIIHUiZxq1kL8cFIH5731jPcXQ4JNy/wbj1C9sZ8rzD8BIM80Tqk29w==} - engines: {node: '>=12'} - cpu: [x64] - os: [openbsd] - requiresBuild: true - dev: true - optional: true - - /esbuild-sunos-64/0.14.47: - resolution: {integrity: sha512-uOeSgLUwukLioAJOiGYm3kNl+1wJjgJA8R671GYgcPgCx7QR73zfvYqXFFcIO93/nBdIbt5hd8RItqbbf3HtAQ==} + /esbuild-sunos-64/0.14.53: + resolution: {integrity: sha512-OWLpS7a2FrIRukQqcgQqR1XKn0jSJoOdT+RlhAxUoEQM/IpytS3FXzCJM6xjUYtpO5GMY0EdZJp+ur2pYdm39g==} engines: {node: '>=12'} cpu: [x64] os: [sunos] requiresBuild: true optional: true - /esbuild-sunos-64/0.14.50: - resolution: {integrity: sha512-gBxNY/wyptvD7PkHIYcq7se6SQEXcSC8Y7mE0FJB+CGgssEWf6vBPfTTZ2b6BWKnmaP6P6qb7s/KRIV5T2PxsQ==} - engines: {node: '>=12'} - cpu: [x64] - os: [sunos] - requiresBuild: true - dev: true - optional: true - - /esbuild-windows-32/0.14.47: - resolution: {integrity: sha512-H0fWsLTp2WBfKLBgwYT4OTfFly4Im/8B5f3ojDv1Kx//kiubVY0IQunP2Koc/fr/0wI7hj3IiBDbSrmKlrNgLQ==} + /esbuild-windows-32/0.14.53: + resolution: {integrity: sha512-m14XyWQP5rwGW0tbEfp95U6A0wY0DYPInWBB7D69FAXUpBpBObRoGTKRv36lf2RWOdE4YO3TNvj37zhXjVL5xg==} engines: {node: '>=12'} cpu: [ia32] os: [win32] requiresBuild: true optional: true - /esbuild-windows-32/0.14.50: - resolution: {integrity: sha512-MOOe6J9cqe/iW1qbIVYSAqzJFh0p2LBLhVUIWdMVnNUNjvg2/4QNX4oT4IzgDeldU+Bym9/Tn6+DxvUHJXL5Zw==} - engines: {node: '>=12'} - cpu: [ia32] - os: [win32] - requiresBuild: true - dev: true - optional: true - - /esbuild-windows-64/0.14.47: - resolution: {integrity: sha512-/Pk5jIEH34T68r8PweKRi77W49KwanZ8X6lr3vDAtOlH5EumPE4pBHqkCUdELanvsT14yMXLQ/C/8XPi1pAtkQ==} + /esbuild-windows-64/0.14.53: + resolution: {integrity: sha512-s9skQFF0I7zqnQ2K8S1xdLSfZFsPLuOGmSx57h2btSEswv0N0YodYvqLcJMrNMXh6EynOmWD7rz+0rWWbFpIHQ==} engines: {node: '>=12'} cpu: [x64] os: [win32] requiresBuild: true optional: true - /esbuild-windows-64/0.14.50: - resolution: {integrity: sha512-r/qE5Ex3w1jjGv/JlpPoWB365ldkppUlnizhMxJgojp907ZF1PgLTuW207kgzZcSCXyquL9qJkMsY+MRtaZ5yQ==} - engines: {node: '>=12'} - cpu: [x64] - os: [win32] - requiresBuild: true - dev: true - optional: true - - /esbuild-windows-arm64/0.14.47: - resolution: {integrity: sha512-HFSW2lnp62fl86/qPQlqw6asIwCnEsEoNIL1h2uVMgakddf+vUuMcCbtUY1i8sst7KkgHrVKCJQB33YhhOweCQ==} - engines: {node: '>=12'} - cpu: [arm64] - os: [win32] - requiresBuild: true - optional: true - - /esbuild-windows-arm64/0.14.50: - resolution: {integrity: sha512-EMS4lQnsIe12ZyAinOINx7eq2mjpDdhGZZWDwPZE/yUTN9cnc2Ze/xUTYIAyaJqrqQda3LnDpADKpvLvol6ENQ==} + /esbuild-windows-arm64/0.14.53: + resolution: {integrity: sha512-E+5Gvb+ZWts+00T9II6wp2L3KG2r3iGxByqd/a1RmLmYWVsSVUjkvIxZuJ3hYTIbhLkH5PRwpldGTKYqVz0nzQ==} engines: {node: '>=12'} cpu: [arm64] os: [win32] requiresBuild: true - dev: true optional: true - /esbuild/0.14.47: - resolution: {integrity: sha512-wI4ZiIfFxpkuxB8ju4MHrGwGLyp1+awEHAHVpx6w7a+1pmYIq8T9FGEVVwFo0iFierDoMj++Xq69GXWYn2EiwA==} + /esbuild/0.14.53: + resolution: {integrity: sha512-ohO33pUBQ64q6mmheX1mZ8mIXj8ivQY/L4oVuAshr+aJI+zLl+amrp3EodrUNDNYVrKJXGPfIHFGhO8slGRjuw==} engines: {node: '>=12'} hasBin: true requiresBuild: true optionalDependencies: - esbuild-android-64: 0.14.47 - esbuild-android-arm64: 0.14.47 - esbuild-darwin-64: 0.14.47 - esbuild-darwin-arm64: 0.14.47 - esbuild-freebsd-64: 0.14.47 - esbuild-freebsd-arm64: 0.14.47 - esbuild-linux-32: 0.14.47 - esbuild-linux-64: 0.14.47 - esbuild-linux-arm: 0.14.47 - esbuild-linux-arm64: 0.14.47 - esbuild-linux-mips64le: 0.14.47 - esbuild-linux-ppc64le: 0.14.47 - esbuild-linux-riscv64: 0.14.47 - esbuild-linux-s390x: 0.14.47 - esbuild-netbsd-64: 0.14.47 - esbuild-openbsd-64: 0.14.47 - esbuild-sunos-64: 0.14.47 - esbuild-windows-32: 0.14.47 - esbuild-windows-64: 0.14.47 - esbuild-windows-arm64: 0.14.47 - - /esbuild/0.14.50: - resolution: {integrity: sha512-SbC3k35Ih2IC6trhbMYW7hYeGdjPKf9atTKwBUHqMCYFZZ9z8zhuvfnZihsnJypl74FjiAKjBRqFkBkAd0rS/w==} - engines: {node: '>=12'} - hasBin: true - requiresBuild: true - optionalDependencies: - esbuild-android-64: 0.14.50 - esbuild-android-arm64: 0.14.50 - esbuild-darwin-64: 0.14.50 - esbuild-darwin-arm64: 0.14.50 - esbuild-freebsd-64: 0.14.50 - esbuild-freebsd-arm64: 0.14.50 - esbuild-linux-32: 0.14.50 - esbuild-linux-64: 0.14.50 - esbuild-linux-arm: 0.14.50 - esbuild-linux-arm64: 0.14.50 - esbuild-linux-mips64le: 0.14.50 - esbuild-linux-ppc64le: 0.14.50 - esbuild-linux-riscv64: 0.14.50 - esbuild-linux-s390x: 0.14.50 - esbuild-netbsd-64: 0.14.50 - esbuild-openbsd-64: 0.14.50 - esbuild-sunos-64: 0.14.50 - esbuild-windows-32: 0.14.50 - esbuild-windows-64: 0.14.50 - esbuild-windows-arm64: 0.14.50 - dev: true + '@esbuild/linux-loong64': 0.14.53 + esbuild-android-64: 0.14.53 + esbuild-android-arm64: 0.14.53 + esbuild-darwin-64: 0.14.53 + esbuild-darwin-arm64: 0.14.53 + esbuild-freebsd-64: 0.14.53 + esbuild-freebsd-arm64: 0.14.53 + esbuild-linux-32: 0.14.53 + esbuild-linux-64: 0.14.53 + esbuild-linux-arm: 0.14.53 + esbuild-linux-arm64: 0.14.53 + esbuild-linux-mips64le: 0.14.53 + esbuild-linux-ppc64le: 0.14.53 + esbuild-linux-riscv64: 0.14.53 + esbuild-linux-s390x: 0.14.53 + esbuild-netbsd-64: 0.14.53 + esbuild-openbsd-64: 0.14.53 + esbuild-sunos-64: 0.14.53 + esbuild-windows-32: 0.14.53 + esbuild-windows-64: 0.14.53 + esbuild-windows-arm64: 0.14.53 /escalade/3.1.1: resolution: {integrity: sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==} @@ -6457,7 +6258,7 @@ packages: optional: true dependencies: defu: 6.0.0 - esbuild: 0.14.50 + esbuild: 0.14.53 fs-extra: 10.1.0 globby: 11.1.0 jiti: 1.14.0 @@ -7643,7 +7444,7 @@ packages: '@babel/code-frame': 7.18.6 dev: true - /rollup-plugin-esbuild/4.9.1_nkjtn7ewzjrspe36n7zrekxwnm: + /rollup-plugin-esbuild/4.9.1_qlvih73og4buyikchn2u4fwv5m: resolution: {integrity: sha512-qn/x7Wz9p3Xnva99qcb+nopH0d2VJwVnsxJTGEg+Sh2Z3tqQl33MhOwzekVo1YTKgv+yAmosjcBRJygMfGrtLw==} engines: {node: '>=12'} peerDependencies: @@ -7653,7 +7454,7 @@ packages: '@rollup/pluginutils': 4.2.1 debug: 4.3.4 es-module-lexer: 0.9.3 - esbuild: 0.14.50 + esbuild: 0.14.53 joycon: 3.1.1 jsonc-parser: 3.0.0 rollup: 2.77.0 @@ -8569,7 +8370,7 @@ packages: chalk: 5.0.1 consola: 2.15.3 defu: 6.0.0 - esbuild: 0.14.50 + esbuild: 0.14.53 hookable: 5.1.1 jiti: 1.14.0 magic-string: 0.26.2 @@ -8583,7 +8384,7 @@ packages: rimraf: 3.0.2 rollup: 2.77.0 rollup-plugin-dts: 4.2.2_55kiftncucr43pz4hskma6yi2q - rollup-plugin-esbuild: 4.9.1_nkjtn7ewzjrspe36n7zrekxwnm + rollup-plugin-esbuild: 4.9.1_qlvih73og4buyikchn2u4fwv5m scule: 0.2.1 typescript: 4.7.4 untyped: 0.4.4 From 68444365ea42cbf972d44c53915b32ccbe4dcbb7 Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Tue, 9 Aug 2022 11:39:05 -0700 Subject: [PATCH 2/7] chore: update lockfile --- pnpm-lock.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3d8397ea47fcd7..31d5ff5cf76420 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -227,7 +227,7 @@ importers: dotenv: ^14.3.2 dotenv-expand: ^5.1.0 es-module-lexer: ^0.10.5 - esbuild: ^0.14.53 + esbuild: ^0.14.47 estree-walker: ^3.0.1 etag: ^1.8.1 fast-glob: ^3.2.11 @@ -262,7 +262,7 @@ importers: ufo: ^0.8.5 ws: ^8.8.1 dependencies: - esbuild: 0.14.47 + esbuild: 0.14.53 postcss: 8.4.16 resolve: 1.22.1 rollup: 2.75.6 @@ -2125,7 +2125,7 @@ packages: /@esbuild-kit/core-utils/2.1.0: resolution: {integrity: sha512-fZirrc2KjeTumVjE4bpleWOk2gD83b7WuGeQqOceKFQL+heNKKkNB5G5pekOUTLzfSBc0hP7hCSBoD9TuR0hLw==} dependencies: - esbuild: 0.14.50 + esbuild: 0.14.53 source-map-support: 0.5.21 dev: true From 746f5b8492dd566043f1b8b71097c50694473ff8 Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Tue, 9 Aug 2022 13:40:01 -0700 Subject: [PATCH 3/7] fix: remove jsxPure option --- packages/plugin-react/src/index.ts | 6 ------ 1 file changed, 6 deletions(-) diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index 970d7fef9cdc4e..550b03b75c5289 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -34,12 +34,6 @@ export interface Options { * @default "react" */ jsxImportSource?: string - /** - * Set this to `true` to annotate the JSX factory with `\/* @__PURE__ *\/`. - * This option is ignored when `jsxRuntime` is not `"automatic"`. - * @default true - */ - jsxPure?: boolean /** * Babel configuration applied in both dev and prod. */ From 9fac44191fdc24f1860a7d6b5003502d594facf2 Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Mon, 15 Aug 2022 09:20:27 -0700 Subject: [PATCH 4/7] Update index.ts --- packages/plugin-react/src/index.ts | 1 + 1 file changed, 1 insertion(+) diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index 550b03b75c5289..8ad6638354a44a 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -117,6 +117,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] { name: 'vite:react-babel', enforce: 'pre', config(_, { mode }) { + // Copied from https://github.com/vitejs/vite/blob/4e9bdd4fb3654a9d43917e1cb682d3d2bad25115/packages/vite/src/node/config.ts#L488-L490 const isProduction = (process.env.NODE_ENV || process.env.VITE_USER_NODE_ENV || mode) === 'production' From e2ef29582f6b4e625219c2a100dfbaead1f47041 Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Sun, 18 Sep 2022 22:21:05 -0700 Subject: [PATCH 5/7] chore: update esbuild --- package.json | 2 +- packages/vite/package.json | 2 +- pnpm-lock.yaml | 246 ++++++++++++++++++++++++++++++++++++- 3 files changed, 244 insertions(+), 6 deletions(-) diff --git a/package.json b/package.json index 788a673890aafe..3b1aa5e19e0c4c 100644 --- a/package.json +++ b/package.json @@ -61,7 +61,7 @@ "@typescript-eslint/parser": "^5.33.0", "conventional-changelog-cli": "^2.2.2", "cross-env": "^7.0.3", - "esbuild": "^0.14.53", + "esbuild": "^0.15.8", "eslint": "^8.21.0", "eslint-define-config": "^1.6.0", "eslint-plugin-import": "^2.26.0", diff --git a/packages/vite/package.json b/packages/vite/package.json index e08659c56f3c35..c7640fda14a4fc 100644 --- a/packages/vite/package.json +++ b/packages/vite/package.json @@ -58,7 +58,7 @@ }, "//": "READ CONTRIBUTING.md to understand what to put under deps vs. devDeps!", "dependencies": { - "esbuild": "^0.14.47", + "esbuild": "^0.15.8", "postcss": "^8.4.16", "resolve": "^1.22.1", "rollup": "^2.75.6" diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 31d5ff5cf76420..213a87197a457a 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -35,7 +35,7 @@ importers: '@typescript-eslint/parser': ^5.33.0 conventional-changelog-cli: ^2.2.2 cross-env: ^7.0.3 - esbuild: ^0.14.53 + esbuild: ^0.15.8 eslint: ^8.21.0 eslint-define-config: ^1.6.0 eslint-plugin-import: ^2.26.0 @@ -88,7 +88,7 @@ importers: '@typescript-eslint/parser': 5.33.0_vxmhu3tyr7cxhd2vxjkubkv7im conventional-changelog-cli: 2.2.2 cross-env: 7.0.3 - esbuild: 0.14.53 + esbuild: 0.15.8 eslint: 8.21.0 eslint-define-config: 1.6.0 eslint-plugin-import: 2.26.0_qfqnhzzittf54udqwes54xx65q @@ -227,7 +227,7 @@ importers: dotenv: ^14.3.2 dotenv-expand: ^5.1.0 es-module-lexer: ^0.10.5 - esbuild: ^0.14.47 + esbuild: ^0.15.8 estree-walker: ^3.0.1 etag: ^1.8.1 fast-glob: ^3.2.11 @@ -262,7 +262,7 @@ importers: ufo: ^0.8.5 ws: ^8.8.1 dependencies: - esbuild: 0.14.53 + esbuild: 0.15.8 postcss: 8.4.16 resolve: 1.22.1 rollup: 2.75.6 @@ -2136,12 +2136,31 @@ packages: get-tsconfig: 4.1.0 dev: true + /@esbuild/android-arm/0.15.8: + resolution: {integrity: sha512-CyEWALmn+no/lbgbAJsbuuhT8s2J19EJGHkeyAwjbFJMrj80KJ9zuYsoAvidPTU7BgBf87r/sgae8Tw0dbOc4Q==} + engines: {node: '>=12'} + cpu: [arm] + os: [android] + requiresBuild: true + dependencies: + esbuild-wasm: 0.15.8 + optional: true + /@esbuild/linux-loong64/0.14.53: resolution: {integrity: sha512-W2dAL6Bnyn4xa/QRSU3ilIK4EzD5wgYXKXJiS1HDF5vU3675qc2bvFyLwbUcdmssDveyndy7FbitrCoiV/eMLg==} engines: {node: '>=12'} cpu: [loong64] os: [linux] requiresBuild: true + dev: true + optional: true + + /@esbuild/linux-loong64/0.15.8: + resolution: {integrity: sha512-pE5RQsOTSERCtfZdfCT25wzo7dfhOSlhAXcsZmuvRYhendOv7djcdvtINdnDp2DAjP17WXlBB4nBO6sHLczmsg==} + engines: {node: '>=12'} + cpu: [loong64] + os: [linux] + requiresBuild: true optional: true /@eslint/eslintrc/1.3.0: @@ -4390,6 +4409,17 @@ packages: cpu: [x64] os: [android] requiresBuild: true + dev: true + optional: true + + /esbuild-android-64/0.15.8: + resolution: {integrity: sha512-bVh8FIKOolF7/d4AMzt7xHlL0Ljr+mYKSHI39TJWDkybVWHdn6+4ODL3xZGHOxPpdRpitemXA1WwMKYBsw8dGw==} + engines: {node: '>=12'} + cpu: [x64] + os: [android] + requiresBuild: true + dependencies: + esbuild-wasm: 0.15.8 optional: true /esbuild-android-arm64/0.14.53: @@ -4398,6 +4428,15 @@ packages: cpu: [arm64] os: [android] requiresBuild: true + dev: true + optional: true + + /esbuild-android-arm64/0.15.8: + resolution: {integrity: sha512-ReAMDAHuo0H1h9LxRabI6gwYPn8k6WiUeyxuMvx17yTrJO+SCnIfNc/TSPFvDwtK9MiyiKG/2dBYHouT/M0BXQ==} + engines: {node: '>=12'} + cpu: [arm64] + os: [android] + requiresBuild: true optional: true /esbuild-darwin-64/0.14.53: @@ -4406,6 +4445,15 @@ packages: cpu: [x64] os: [darwin] requiresBuild: true + dev: true + optional: true + + /esbuild-darwin-64/0.15.8: + resolution: {integrity: sha512-KaKcGfJ+yto7Fo5gAj3xwxHMd1fBIKatpCHK8znTJLVv+9+NN2/tIPBqA4w5rBwjX0UqXDeIE2v1xJP+nGEXgA==} + engines: {node: '>=12'} + cpu: [x64] + os: [darwin] + requiresBuild: true optional: true /esbuild-darwin-arm64/0.14.53: @@ -4414,6 +4462,15 @@ packages: cpu: [arm64] os: [darwin] requiresBuild: true + dev: true + optional: true + + /esbuild-darwin-arm64/0.15.8: + resolution: {integrity: sha512-8tjEaBgAKnXCkP7bhEJmEqdG9HEV6oLkF36BrMzpfW2rgaw0c48Zrxe+9RlfeGvs6gDF4w+agXyTjikzsS3izw==} + engines: {node: '>=12'} + cpu: [arm64] + os: [darwin] + requiresBuild: true optional: true /esbuild-freebsd-64/0.14.53: @@ -4422,6 +4479,15 @@ packages: cpu: [x64] os: [freebsd] requiresBuild: true + dev: true + optional: true + + /esbuild-freebsd-64/0.15.8: + resolution: {integrity: sha512-jaxcsGHYzn2L0/lffON2WfH4Nc+d/EwozVTP5K2v016zxMb5UQMhLoJzvLgBqHT1SG0B/mO+a+THnJCMVg15zw==} + engines: {node: '>=12'} + cpu: [x64] + os: [freebsd] + requiresBuild: true optional: true /esbuild-freebsd-arm64/0.14.53: @@ -4430,6 +4496,15 @@ packages: cpu: [arm64] os: [freebsd] requiresBuild: true + dev: true + optional: true + + /esbuild-freebsd-arm64/0.15.8: + resolution: {integrity: sha512-2xp2UlljMvX8HExtcg7VHaeQk8OBU0CSl1j18B5CcZmSDkLF9p3utuMXIopG3a08fr9Hv+Dz6+seSXUow/G51w==} + engines: {node: '>=12'} + cpu: [arm64] + os: [freebsd] + requiresBuild: true optional: true /esbuild-linux-32/0.14.53: @@ -4438,6 +4513,15 @@ packages: cpu: [ia32] os: [linux] requiresBuild: true + dev: true + optional: true + + /esbuild-linux-32/0.15.8: + resolution: {integrity: sha512-9u1E54BRz1FQMl86iaHK146+4ID2KYNxL3trLZT4QLLx3M7Q9n4lGG3lrzqUatGR2cKy8c33b0iaCzsItZWkFg==} + engines: {node: '>=12'} + cpu: [ia32] + os: [linux] + requiresBuild: true optional: true /esbuild-linux-64/0.14.53: @@ -4446,6 +4530,15 @@ packages: cpu: [x64] os: [linux] requiresBuild: true + dev: true + optional: true + + /esbuild-linux-64/0.15.8: + resolution: {integrity: sha512-4HxrsN9eUzJXdVGMTYA5Xler82FuZUu21bXKN42zcLHHNKCAMPUzD62I+GwDhsdgUBAUj0tRXDdsQHgaP6v0HA==} + engines: {node: '>=12'} + cpu: [x64] + os: [linux] + requiresBuild: true optional: true /esbuild-linux-arm/0.14.53: @@ -4454,6 +4547,15 @@ packages: cpu: [arm] os: [linux] requiresBuild: true + dev: true + optional: true + + /esbuild-linux-arm/0.15.8: + resolution: {integrity: sha512-7DVBU9SFjX4+vBwt8tHsUCbE6Vvl6y6FQWHAgyw1lybC5gULqn/WnjHYHN2/LJaZRsDBvxWT4msEgwLGq1Wd3Q==} + engines: {node: '>=12'} + cpu: [arm] + os: [linux] + requiresBuild: true optional: true /esbuild-linux-arm64/0.14.53: @@ -4462,6 +4564,15 @@ packages: cpu: [arm64] os: [linux] requiresBuild: true + dev: true + optional: true + + /esbuild-linux-arm64/0.15.8: + resolution: {integrity: sha512-1OCm7Aq0tEJT70PbxmHSGYDLYP8DKH8r4Nk7/XbVzWaduo9beCjGBB+tGZIHK6DdTQ3h00/4Tb/70YMH/bOtKg==} + engines: {node: '>=12'} + cpu: [arm64] + os: [linux] + requiresBuild: true optional: true /esbuild-linux-mips64le/0.14.53: @@ -4470,6 +4581,15 @@ packages: cpu: [mips64el] os: [linux] requiresBuild: true + dev: true + optional: true + + /esbuild-linux-mips64le/0.15.8: + resolution: {integrity: sha512-yeFoNPVFPEzZvFYBfUQNG2TjGRaCyV1E27OcOg4LOtnGrxb2wA+mkW3luckyv1CEyd00mpAg7UdHx8nlx3ghgA==} + engines: {node: '>=12'} + cpu: [mips64el] + os: [linux] + requiresBuild: true optional: true /esbuild-linux-ppc64le/0.14.53: @@ -4478,6 +4598,15 @@ packages: cpu: [ppc64] os: [linux] requiresBuild: true + dev: true + optional: true + + /esbuild-linux-ppc64le/0.15.8: + resolution: {integrity: sha512-CEyMMUUNabXibw8OSNmBXhOIGhnjNVl5Lpseiuf00iKN0V47oqDrbo4dsHz1wH62m49AR8iG8wpDlTqfYgKbtg==} + engines: {node: '>=12'} + cpu: [ppc64] + os: [linux] + requiresBuild: true optional: true /esbuild-linux-riscv64/0.14.53: @@ -4486,6 +4615,15 @@ packages: cpu: [riscv64] os: [linux] requiresBuild: true + dev: true + optional: true + + /esbuild-linux-riscv64/0.15.8: + resolution: {integrity: sha512-OCGSOaspMUjexSCU8ZiA0UnV/NiRU+s2vIfEcAQWQ6u32R+2luyfh/4ZaY6jFbylJE07Esc/yRvb9Q5fXuClXA==} + engines: {node: '>=12'} + cpu: [riscv64] + os: [linux] + requiresBuild: true optional: true /esbuild-linux-s390x/0.14.53: @@ -4494,6 +4632,15 @@ packages: cpu: [s390x] os: [linux] requiresBuild: true + dev: true + optional: true + + /esbuild-linux-s390x/0.15.8: + resolution: {integrity: sha512-RHdpdfxRTSrZXZJlFSLazFU4YwXLB5Rgf6Zr5rffqSsO4y9JybgtKO38bFwxZNlDXliYISXN/YROKrG9s7mZQA==} + engines: {node: '>=12'} + cpu: [s390x] + os: [linux] + requiresBuild: true optional: true /esbuild-netbsd-64/0.14.53: @@ -4502,6 +4649,15 @@ packages: cpu: [x64] os: [netbsd] requiresBuild: true + dev: true + optional: true + + /esbuild-netbsd-64/0.15.8: + resolution: {integrity: sha512-VolFFRatBH09T5QMWhiohAWCOien1R1Uz9K0BRVVTBgBaVBt7eArsXTKxVhUgRf2vwu2c2SXkuP0r7HLG0eozw==} + engines: {node: '>=12'} + cpu: [x64] + os: [netbsd] + requiresBuild: true optional: true /esbuild-openbsd-64/0.14.53: @@ -4510,6 +4666,15 @@ packages: cpu: [x64] os: [openbsd] requiresBuild: true + dev: true + optional: true + + /esbuild-openbsd-64/0.15.8: + resolution: {integrity: sha512-HTAPlg+n4kUeE/isQxlCfsOz0xJGNoT5LJ9oYZWFKABfVf4Ycu7Zlf5ITgOnrdheTkz8JeL/gISIOCFAoOXrSA==} + engines: {node: '>=12'} + cpu: [x64] + os: [openbsd] + requiresBuild: true optional: true /esbuild-sunos-64/0.14.53: @@ -4518,6 +4683,22 @@ packages: cpu: [x64] os: [sunos] requiresBuild: true + dev: true + optional: true + + /esbuild-sunos-64/0.15.8: + resolution: {integrity: sha512-qMP/jR/FzcIOwKj+W+Lb+8Cfr8GZHbHUJxAPi7DUhNZMQ/6y7sOgRzlOSpRrbbUntrRZh0MqOyDhJ3Gpo6L1QA==} + engines: {node: '>=12'} + cpu: [x64] + os: [sunos] + requiresBuild: true + optional: true + + /esbuild-wasm/0.15.8: + resolution: {integrity: sha512-Y7uCl5RNO4URjlemjdx++ukVHEMt5s5AfMWYUnMiK4Sry+pPCvQIctzXq6r6FKCyGKjX6/NGMCqR2OX6aLxj0w==} + engines: {node: '>=12'} + hasBin: true + requiresBuild: true optional: true /esbuild-windows-32/0.14.53: @@ -4526,6 +4707,15 @@ packages: cpu: [ia32] os: [win32] requiresBuild: true + dev: true + optional: true + + /esbuild-windows-32/0.15.8: + resolution: {integrity: sha512-RKR1QHh4iWzjUhkP8Yqi75PPz/KS+b8zw3wUrzw6oAkj+iU5Qtyj61ZDaSG3Qf2vc6hTIUiPqVTqBH0NpXFNwg==} + engines: {node: '>=12'} + cpu: [ia32] + os: [win32] + requiresBuild: true optional: true /esbuild-windows-64/0.14.53: @@ -4534,6 +4724,15 @@ packages: cpu: [x64] os: [win32] requiresBuild: true + dev: true + optional: true + + /esbuild-windows-64/0.15.8: + resolution: {integrity: sha512-ag9ptYrsizgsR+PQE8QKeMqnosLvAMonQREpLw4evA4FFgOBMLEat/dY/9txbpozTw9eEOYyD3a4cE9yTu20FA==} + engines: {node: '>=12'} + cpu: [x64] + os: [win32] + requiresBuild: true optional: true /esbuild-windows-arm64/0.14.53: @@ -4542,6 +4741,15 @@ packages: cpu: [arm64] os: [win32] requiresBuild: true + dev: true + optional: true + + /esbuild-windows-arm64/0.15.8: + resolution: {integrity: sha512-dbpAb0VyPaUs9mgw65KRfQ9rqiWCHpNzrJusoPu+LpEoswosjt/tFxN7cd2l68AT4qWdBkzAjDLRon7uqMeWcg==} + engines: {node: '>=12'} + cpu: [arm64] + os: [win32] + requiresBuild: true optional: true /esbuild/0.14.53: @@ -4571,6 +4779,36 @@ packages: esbuild-windows-32: 0.14.53 esbuild-windows-64: 0.14.53 esbuild-windows-arm64: 0.14.53 + dev: true + + /esbuild/0.15.8: + resolution: {integrity: sha512-Remsk2dmr1Ia65sU+QasE6svJbsHe62lzR+CnjpUvbZ+uSYo1SitiOWPRfZQkCu82YWZBBKXiD/j0i//XWMZ+Q==} + engines: {node: '>=12'} + hasBin: true + requiresBuild: true + optionalDependencies: + '@esbuild/android-arm': 0.15.8 + '@esbuild/linux-loong64': 0.15.8 + esbuild-android-64: 0.15.8 + esbuild-android-arm64: 0.15.8 + esbuild-darwin-64: 0.15.8 + esbuild-darwin-arm64: 0.15.8 + esbuild-freebsd-64: 0.15.8 + esbuild-freebsd-arm64: 0.15.8 + esbuild-linux-32: 0.15.8 + esbuild-linux-64: 0.15.8 + esbuild-linux-arm: 0.15.8 + esbuild-linux-arm64: 0.15.8 + esbuild-linux-mips64le: 0.15.8 + esbuild-linux-ppc64le: 0.15.8 + esbuild-linux-riscv64: 0.15.8 + esbuild-linux-s390x: 0.15.8 + esbuild-netbsd-64: 0.15.8 + esbuild-openbsd-64: 0.15.8 + esbuild-sunos-64: 0.15.8 + esbuild-windows-32: 0.15.8 + esbuild-windows-64: 0.15.8 + esbuild-windows-arm64: 0.15.8 /escalade/3.1.1: resolution: {integrity: sha512-k0er2gUkLf8O0zKJiAhmkTnJlTvINGv7ygDNPbeIsX/TJjGJZHuh9B2UxbsaEkmlEo9MfhrSzmhIlhRlI2GXnw==} From 6bed321d1313938cf67724c8d7825aae52bfae40 Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Sun, 18 Sep 2022 22:34:52 -0700 Subject: [PATCH 6/7] feat: use esbuild jsx side effects option --- packages/plugin-react/src/index.ts | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index 3244fd2981bc39..cd732c4e3fd915 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -34,6 +34,12 @@ export interface Options { * @default "react" */ jsxImportSource?: string + /** + * Set this to `true` to annotate the JSX factory with `\/* @__PURE__ *\/`. + * This option is ignored when `jsxRuntime` is not `"automatic"`. + * @default true + */ + jsxPure?: boolean /** * Babel configuration applied in both dev and prod. */ @@ -129,7 +135,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] { 'this-is-undefined-in-esm': 'silent' }, jsx: 'transform', - jsxImportSource: opts.jsxImportSource + jsxImportSource: opts.jsxImportSource, + jsxSideEffects: opts.jsxPure === false } } } else { @@ -137,7 +144,8 @@ export default function viteReact(opts: Options = {}): PluginOption[] { esbuild: { jsxDev: !isProduction, jsx: 'automatic', - jsxImportSource: opts.jsxImportSource + jsxImportSource: opts.jsxImportSource, + jsxSideEffects: opts.jsxPure === false } } } From ee396ba9ca9d48023e80206df4d571da7f652ff1 Mon Sep 17 00:00:00 2001 From: Ryan Tsao Date: Thu, 3 Nov 2022 15:25:31 -0700 Subject: [PATCH 7/7] fix: remove broken restoreJsx --- packages/plugin-react/src/index.ts | 28 +-- .../jsx-runtime/babel-import-to-require.ts | 35 --- .../src/jsx-runtime/babel-restore-jsx.spec.ts | 132 ---------- .../src/jsx-runtime/babel-restore-jsx.ts | 232 ------------------ .../src/jsx-runtime/restore-jsx.spec.ts | 147 ----------- .../src/jsx-runtime/restore-jsx.ts | 74 ------ 6 files changed, 2 insertions(+), 646 deletions(-) delete mode 100644 packages/plugin-react/src/jsx-runtime/babel-import-to-require.ts delete mode 100644 packages/plugin-react/src/jsx-runtime/babel-restore-jsx.spec.ts delete mode 100644 packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts delete mode 100644 packages/plugin-react/src/jsx-runtime/restore-jsx.spec.ts delete mode 100644 packages/plugin-react/src/jsx-runtime/restore-jsx.ts diff --git a/packages/plugin-react/src/index.ts b/packages/plugin-react/src/index.ts index cd732c4e3fd915..fe7b209c44ed12 100644 --- a/packages/plugin-react/src/index.ts +++ b/packages/plugin-react/src/index.ts @@ -1,7 +1,6 @@ -import path from 'node:path' import type { ParserOptions, TransformOptions, types as t } from '@babel/core' import * as babel from '@babel/core' -import { createFilter, normalizePath } from 'vite' +import { createFilter } from 'vite' import type { Plugin, PluginOption, ResolvedConfig } from 'vite' import MagicString from 'magic-string' import type { SourceMap } from 'magic-string' @@ -12,8 +11,6 @@ import { runtimeCode, runtimePublicPath } from './fast-refresh' -import { babelImportToRequire } from './jsx-runtime/babel-import-to-require' -import { restoreJSX } from './jsx-runtime/restore-jsx' export interface Options { include?: string | RegExp | Array @@ -95,7 +92,6 @@ const prependReactImportCode = "import React from 'react'; " export default function viteReact(opts: Options = {}): PluginOption[] { // Provide default values for Rollup compat. let devBase = '/' - let resolvedCacheDir: string let filter = createFilter(opts.include, opts.exclude) let needHiresSourcemap = false let isProduction = true @@ -153,7 +149,6 @@ export default function viteReact(opts: Options = {}): PluginOption[] { configResolved(config) { devBase = config.base projectRoot = config.root - resolvedCacheDir = normalizePath(path.resolve(config.cacheDir)) filter = createFilter(opts.include, opts.exclude, { resolve: projectRoot }) @@ -243,26 +238,7 @@ export default function viteReact(opts: Options = {}): PluginOption[] { let ast: t.File | null | undefined let prependReactImport = false if (!isProjectFile || isJSX) { - if (useAutomaticRuntime) { - // By reverse-compiling "React.createElement" calls into JSX, - // React elements provided by dependencies will also use the - // automatic runtime! - // Avoid parsing the optimized react-dom since it will never - // contain compiled JSX and it's a pretty big file (800kb). - const isOptimizedReactDom = - id.startsWith(resolvedCacheDir) && id.includes('/react-dom.js') - const [restoredAst, isCommonJS] = - !isProjectFile && !isJSX && !isOptimizedReactDom - ? await restoreJSX(babel, code, id) - : [null, false] - - if (isJSX || (ast = restoredAst)) { - // Avoid inserting `import` statements into CJS modules. - if (isCommonJS) { - plugins.push(babelImportToRequire) - } - } - } else if (isProjectFile) { + if (!useAutomaticRuntime && isProjectFile) { // These plugins are only needed for the classic runtime. if (!isProduction) { plugins.push( diff --git a/packages/plugin-react/src/jsx-runtime/babel-import-to-require.ts b/packages/plugin-react/src/jsx-runtime/babel-import-to-require.ts deleted file mode 100644 index cd6608221512c9..00000000000000 --- a/packages/plugin-react/src/jsx-runtime/babel-import-to-require.ts +++ /dev/null @@ -1,35 +0,0 @@ -import type * as babelCore from '@babel/core' - -/** - * Replace this: - * - * import { jsx as _jsx } from "react/jsx-runtime" - * - * with this: - * - * var _jsx = require("react/jsx-runtime").jsx - */ -export function babelImportToRequire({ types: t }: typeof babelCore): { - visitor: babelCore.Visitor -} { - return { - visitor: { - ImportDeclaration(path) { - const decl = path.node - const spec = decl.specifiers[0] as babelCore.types.ImportSpecifier - - path.replaceWith( - t.variableDeclaration('var', [ - t.variableDeclarator( - spec.local, - t.memberExpression( - t.callExpression(t.identifier('require'), [decl.source]), - spec.imported - ) - ) - ]) - ) - } - } - } -} diff --git a/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.spec.ts b/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.spec.ts deleted file mode 100644 index 5590bfb9d7fa5f..00000000000000 --- a/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.spec.ts +++ /dev/null @@ -1,132 +0,0 @@ -import * as babel from '@babel/core' -import { describe, expect, it } from 'vitest' -import babelRestoreJSX from './babel-restore-jsx' - -function jsx(code: string) { - return babel.transform(code, { - parserOpts: { plugins: ['jsx'] }, - plugins: [babelRestoreJSX] - })?.code -} - -// Tests adapted from: https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx/blob/63137b6/test/index.js -describe('babel-restore-jsx', () => { - it('should convert 1-argument calls', () => { - expect(jsx('React.createElement("h1")')).toMatchInlineSnapshot(`"

;"`) - expect(jsx('React.createElement(Foo)')).toMatchInlineSnapshot(`";"`) - expect(jsx('React.createElement(Foo.Bar)')).toMatchInlineSnapshot( - `";"` - ) - expect(jsx('React.createElement(Foo.Bar.Baz)')).toMatchInlineSnapshot( - `";"` - ) - }) - - it('should convert effective 1-argument calls (with null or undefined)', () => { - expect(jsx('React.createElement("h1", null)')).toMatchInlineSnapshot( - `"

;"` - ) - expect(jsx('React.createElement("h2", null, null)')).toMatchInlineSnapshot( - `"

;"` - ) - expect(jsx('React.createElement("h3", undefined)')).toMatchInlineSnapshot( - `"

;"` - ) - }) - - it('should handle props without children', () => { - expect(jsx('React.createElement("h1", {hi: there})')).toMatchInlineSnapshot( - `"

;"` - ) - expect( - jsx('React.createElement("h2", {"hi": there})') - ).toMatchInlineSnapshot(`"

;"`) - expect( - jsx('React.createElement("h3", {hi: "there"})') - ).toMatchInlineSnapshot(`"

;"`) - }) - - it('should handle spread props', () => { - expect(jsx('React.createElement("h1", props)')).toMatchInlineSnapshot( - `"

;"` - ) - expect(jsx('React.createElement("h1", getProps())')).toMatchInlineSnapshot( - `"

;"` - ) - }) - - it('should handle mixed props', () => { - expect( - jsx('React.createElement("h1", _extends({ hi: "there" }, props))') - ).toMatchInlineSnapshot(`"

;"`) - expect( - jsx('React.createElement("h1", _extends({}, props, { hi: "there" }))') - ).toMatchInlineSnapshot(`"

;"`) - expect( - jsx('React.createElement("h1", { ...props, hi: "there" })') - ).toMatchInlineSnapshot(`"

;"`) - }) - - it('should handle props and ignore “null”/“undefined” children', () => { - expect( - jsx('React.createElement("h1", {hi: there}, null, undefined)') - ).toMatchInlineSnapshot(`"

;"`) - }) - - it('should ignore “null”/“undefined” props and handle children', () => { - expect( - jsx('React.createElement("h1", null, "Header")') - ).toMatchInlineSnapshot(`"

Header

;"`) - //this can be created from e.g. '

Header{"harhar"}

', but i think there’s no downside to merging it - expect( - jsx('React.createElement("h2", null, "Header", "harhar")') - ).toMatchInlineSnapshot(`"

Headerharhar

;"`) - expect( - jsx('React.createElement("h3", null, React.createElement("i"))') - ).toMatchInlineSnapshot(`"

;"`) - expect( - jsx('React.createElement("h4", null, "a", React.createElement("b"), "c")') - ).toMatchInlineSnapshot(`"

ac

;"`) - }) - - it('should handle props and children', () => { - //we extensively tested props and children separately, so only sth. basic - expect( - jsx('React.createElement("h1", {hi: there}, "Header")') - ).toMatchInlineSnapshot(`"

Header

;"`) - }) - - it('should ignore intermingled “null”/“undefined” children', () => { - expect( - jsx('React.createElement("h1", null, null, "Header", undefined)') - ).toMatchInlineSnapshot(`"

Header

;"`) - }) - - it('should handle children in nested expressions', () => { - expect( - jsx( - 'React.createElement("h1", null, foo ? React.createElement("p") : null)' - ) - ).toMatchInlineSnapshot(`"

{foo ?

: null}

;"`) - }) - - it('should handle lowercase component names', () => { - expect(jsx('React.createElement(aaa)')).toMatchInlineSnapshot( - `"React.createElement(aaa);"` - ) - }) - - it('should not handle contains __self prop', () => { - expect( - jsx('React.createElement(Provider, { __self: this })') - ).toMatchInlineSnapshot('";"') - }) - - it('should not handle contains __source prop', () => { - expect( - jsx( - 'React.createElement(Provider, { __source: { fileName: _jsxFileName, lineNumber: 133 }})' - ) - ).toMatchInlineSnapshot('";"') - }) -}) diff --git a/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts b/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts deleted file mode 100644 index e5ee9ce454b555..00000000000000 --- a/packages/plugin-react/src/jsx-runtime/babel-restore-jsx.ts +++ /dev/null @@ -1,232 +0,0 @@ -/** - * https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx - * @license GNU General Public License v3.0 - */ -import type * as babel from '@babel/core' - -interface PluginOptions { - reactAlias: string -} - -/** - * Visitor factory for babel, converting React.createElement(...) to ... - * - * What we want to handle here is this CallExpression: - * - * React.createElement( - * type: StringLiteral|Identifier|MemberExpression, - * [props: ObjectExpression|Expression], - * [...children: StringLiteral|Expression] - * ) - * - * Any of those arguments might also be missing (undefined) and/or invalid. - */ -export default function ( - { types: t }: typeof babel, - { reactAlias = 'React' }: PluginOptions -): babel.PluginObj { - /** - * Get a `JSXElement` from a `CallExpression`. - * Returns `null` if this impossible. - */ - function getJSXNode(node: any): any { - if (!isReactCreateElement(node)) { - return null - } - - //nameNode and propsNode may be undefined, getJSX* need to handle that - const [nameNode, propsNode, ...childNodes] = node.arguments - - const name = getJSXName(nameNode) - if (name == null) { - return null //name is required - } - - const props = getJSXProps(propsNode) - if (props == null) { - return null //no props → [], invalid → null - } - - const children = getJSXChildren(childNodes) - if (children == null) { - return null //no children → [], invalid → null - } - - if ( - t.isJSXMemberExpression(name) && - t.isJSXIdentifier(name.object) && - name.object.name === reactAlias && - name.property.name === 'Fragment' - ) { - return t.jsxFragment( - t.jsxOpeningFragment(), - t.jsxClosingFragment(), - children - ) - } - - // self-closing tag if no children - const selfClosing = children.length === 0 - const startTag = t.jsxOpeningElement(name, props, selfClosing) - startTag.loc = node.loc - const endTag = selfClosing ? null : t.jsxClosingElement(name) - - return t.jsxElement(startTag, endTag, children, selfClosing) - } - - /** - * Get a JSXIdentifier or JSXMemberExpression from a Node of known type. - * Returns null if an unknown node type, null or undefined is passed. - */ - function getJSXName(node: any): any { - if (node == null) { - return null - } - - const name = getJSXIdentifier(node, true) - if (name != null) { - return name - } - - if (!t.isMemberExpression(node)) { - return null - } - const object = getJSXName(node.object) - const property = getJSXName(node.property) - if (object == null || property == null) { - return null - } - return t.jsxMemberExpression(object, property) - } - - /** - * Get an array of JSX(Spread)Attribute from a props ObjectExpression. - * Handles the _extends Expression babel creates from SpreadElement nodes. - * Returns null if a validation error occurs. - */ - function getJSXProps(node: any): any[] | null { - if (node == null || isNullLikeNode(node)) { - return [] - } - - if ( - t.isCallExpression(node) && - t.isIdentifier(node.callee, { name: '_extends' }) - ) { - const props: any[] = node.arguments.map(getJSXProps) - //if calling this recursively works, flatten. - if (props.every((prop) => prop != null)) { - return [].concat(...props) - } - } - - if (!t.isObjectExpression(node) && t.isExpression(node)) - return [t.jsxSpreadAttribute(node)] - - if (!isPlainObjectExpression(node)) { - return null - } - return node.properties - .map((prop: any) => - t.isObjectProperty(prop) - ? t.jsxAttribute( - getJSXIdentifier(prop.key)!, - getJSXAttributeValue(prop.value) - ) - : t.jsxSpreadAttribute(prop.argument) - ) - .filter((prop: any) => - t.isJSXIdentifier(prop.name) - ? prop.name.name !== '__self' && prop.name.name !== '__source' - : true - ) - } - - function getJSXChild(node: any) { - if (t.isStringLiteral(node)) { - return t.jsxText(node.value) - } - if (isReactCreateElement(node)) { - return getJSXNode(node) - } - if (t.isExpression(node)) { - return t.jsxExpressionContainer(node) - } - return null - } - - function getJSXChildren(nodes: any[]) { - const children = nodes - .filter((node) => !isNullLikeNode(node)) - .map(getJSXChild) - if (children.some((child) => child == null)) { - return null - } - return children - } - - function getJSXIdentifier(node: any, tag = false) { - //TODO: JSXNamespacedName - if (t.isIdentifier(node) && (!tag || node.name.match(/^[A-Z]/))) { - return t.jsxIdentifier(node.name) - } - if (t.isStringLiteral(node)) { - return t.jsxIdentifier(node.value) - } - return null - } - - function getJSXAttributeValue(node: any) { - if (t.isStringLiteral(node)) { - return node - } - if (t.isJSXElement(node)) { - return node - } - if (t.isExpression(node)) { - return t.jsxExpressionContainer(node) - } - return null - } - - /** - * Tests if a node is a CallExpression with callee `React.createElement` - */ - const isReactCreateElement = (node: any) => - t.isCallExpression(node) && - t.isMemberExpression(node.callee) && - t.isIdentifier(node.callee.object, { name: reactAlias }) && - t.isIdentifier(node.callee.property, { name: 'createElement' }) && - !node.callee.computed - - /** - * Tests if a node is `null` or `undefined` - */ - const isNullLikeNode = (node: any) => - t.isNullLiteral(node) || t.isIdentifier(node, { name: 'undefined' }) - - /** - * Tests if a node is an object expression with noncomputed, nonmethod attrs - */ - const isPlainObjectExpression = (node: any) => - t.isObjectExpression(node) && - node.properties.every( - (property) => - t.isSpreadElement(property) || - (t.isObjectProperty(property, { computed: false }) && - getJSXIdentifier(property.key) != null && - getJSXAttributeValue(property.value) != null) - ) - - return { - visitor: { - CallExpression(path) { - const node = getJSXNode(path.node) - if (node == null) { - return null - } - path.replaceWith(node) - } - } - } -} diff --git a/packages/plugin-react/src/jsx-runtime/restore-jsx.spec.ts b/packages/plugin-react/src/jsx-runtime/restore-jsx.spec.ts deleted file mode 100644 index 00ea39673ec415..00000000000000 --- a/packages/plugin-react/src/jsx-runtime/restore-jsx.spec.ts +++ /dev/null @@ -1,147 +0,0 @@ -import * as babel from '@babel/core' -import { describe, expect, it } from 'vitest' -import { parseReactAlias, restoreJSX } from './restore-jsx' - -describe('parseReactAlias', () => { - it('handles cjs require', () => { - expect(parseReactAlias(`const React = require("react")`)) - .toMatchInlineSnapshot(` - [ - "React", - true, - ] - `) - }) - - it('handles cjs require (minified)', () => { - expect(parseReactAlias(`var F=require('foo');var R=require('react')`)) - .toMatchInlineSnapshot(` - [ - "R", - true, - ] - `) - }) - - it('does not handle destructured cjs require', () => { - expect(parseReactAlias(`var {createElement} = require("react")`)) - .toMatchInlineSnapshot(` - [ - undefined, - false, - ] - `) - }) - - it('handles esm import', () => { - expect(parseReactAlias(`import React from 'react'`)).toMatchInlineSnapshot(` - [ - "React", - false, - ] - `) - }) - - it('handles esm import namespace', () => { - expect(parseReactAlias(`import * as React from "react"`)) - .toMatchInlineSnapshot(` - [ - "React", - false, - ] - `) - }) - - it('does not handle destructured esm import', () => { - expect(parseReactAlias(`import {createElement} from "react"`)) - .toMatchInlineSnapshot(` - [ - undefined, - false, - ] - `) - }) -}) - -async function jsx(sourceCode: string) { - const [ast] = await restoreJSX(babel, sourceCode, 'test.js') - if (ast == null) { - return ast - } - const { code } = await babel.transformFromAstAsync(ast, null, { - configFile: false - }) - return code -} -// jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; -// React__default.createElement(Foo)`) -// Tests adapted from: https://github.com/flying-sheep/babel-plugin-transform-react-createelement-to-jsx/blob/63137b6/test/index.js -describe('restore-jsx', () => { - it('should trans to ', async () => { - expect( - await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; - React__default.createElement(foo)`) - ).toMatchInlineSnapshot(` - "import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; - React__default.createElement(foo);" - `) - expect( - await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; - React__default.createElement("h1")`) - ).toMatch(`

;`) - expect( - await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; - React__default.createElement(Foo)`) - ).toMatch(`;`) - expect( - await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; - React__default.createElement(Foo.Bar)`) - ).toMatch(`;`) - expect( - await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; - React__default.createElement(Foo.Bar.Baz)`) - ).toMatch(`;`) - }) - - it('should handle props', async () => { - expect( - await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; - React__default.createElement(foo, {hi: there})`) - ).toMatchInlineSnapshot(` - "import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; - React__default.createElement(foo, { - hi: there - });" - `) - expect( - await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; - React__default.createElement("h1", {hi: there})`) - ).toMatch(`

;`) - expect( - await jsx(`import React__default, { PureComponent, Component, forwardRef, memo, createElement } from 'react'; - React__default.createElement(Foo, {hi: there})`) - ).toMatch(`;`) - }) - - it('should handle Fragment', async () => { - expect( - await jsx(`import R, { Fragment } from 'react'; - R.createElement(Fragment) - `) - ).toMatchInlineSnapshot(` - "import R, { Fragment } from 'react'; - ;" - `) - }) - - it('should handle Fragment alias', async () => { - expect( - await jsx(`import RA, { Fragment as F } from 'react'; - RA.createElement(F, null, RA.createElement(RA.Fragment)) - `) - ).toMatchInlineSnapshot(` - "import RA, { Fragment as F } from 'react'; - <>;" - `) - }) -}) diff --git a/packages/plugin-react/src/jsx-runtime/restore-jsx.ts b/packages/plugin-react/src/jsx-runtime/restore-jsx.ts deleted file mode 100644 index ddc3c1530e0d9f..00000000000000 --- a/packages/plugin-react/src/jsx-runtime/restore-jsx.ts +++ /dev/null @@ -1,74 +0,0 @@ -import type * as babelCore from '@babel/core' - -type RestoredJSX = [ - result: babelCore.types.File | null | undefined, - isCommonJS: boolean -] - -let babelRestoreJSX: Promise | undefined - -const jsxNotFound: RestoredJSX = [null, false] - -async function getBabelRestoreJSX() { - if (!babelRestoreJSX) - babelRestoreJSX = import('./babel-restore-jsx').then((r) => { - const fn = r.default - if ('default' in fn) - // @ts-expect-error - return fn.default - return fn - }) - return babelRestoreJSX -} - -/** Restore JSX from `React.createElement` calls */ -export async function restoreJSX( - babel: typeof babelCore, - code: string, - filename: string -): Promise { - const [reactAlias, isCommonJS] = parseReactAlias(code) - - if (!reactAlias) { - return jsxNotFound - } - - const reactJsxRE = new RegExp( - `\\b${reactAlias}\\.(createElement|Fragment)\\b`, - 'g' - ) - - if (!reactJsxRE.test(code)) { - return jsxNotFound - } - - const result = await babel.transformAsync(code, { - babelrc: false, - configFile: false, - ast: true, - code: false, - filename, - parserOpts: { - plugins: ['jsx'] - }, - plugins: [[await getBabelRestoreJSX(), { reactAlias }]] - }) - - return [result?.ast, isCommonJS] -} - -export function parseReactAlias( - code: string -): [alias: string | undefined, isCommonJS: boolean] { - let match = code.match( - /\b(var|let|const)\s+([^=\{\s]+)\s*=\s*require\(["']react["']\)/ - ) - if (match) { - return [match[2], true] - } - match = code.match(/^import\s+(?:\*\s+as\s+)?(\w+).+?\bfrom\s*["']react["']/m) - if (match) { - return [match[1], false] - } - return [undefined, false] -}