diff --git a/packages/fes-build-vite/package.json b/packages/fes-build-vite/package.json index c7c871ee..35101367 100644 --- a/packages/fes-build-vite/package.json +++ b/packages/fes-build-vite/package.json @@ -24,11 +24,12 @@ "access": "public" }, "dependencies": { + "@babel/core": "^7.17.9", "@fesjs/utils": "^2.0.4", - "@vitejs/plugin-legacy": "^1.8.0", "@vitejs/plugin-vue": "^2.2.4", "@vitejs/plugin-vue-jsx": "^1.3.8", "autoprefixer": "^10.4.4", + "babel-plugin-polyfill-corejs3": "^0.5.2", "less": "^4.1.2", "postcss-flexbugs-fixes": "^5.0.2", "postcss-safe-parser": "^6.0.0", @@ -37,6 +38,7 @@ "vite-plugin-html": "^3.2.0" }, "peerDependencies": { - "@vue/compiler-sfc": "^3.0.5" + "@vue/compiler-sfc": "^3.0.5", + "core-js": "^3.21.1" } } diff --git a/packages/fes-build-vite/src/commands/build/babelPolyfillPlugin.js b/packages/fes-build-vite/src/commands/build/babelPolyfillPlugin.js new file mode 100644 index 00000000..1b018a39 --- /dev/null +++ b/packages/fes-build-vite/src/commands/build/babelPolyfillPlugin.js @@ -0,0 +1,32 @@ +import { transformSync } from '@babel/core'; + +const DEFAULT_FILTER = /\.[jt]sx?$/; + +export default (config) => ({ + name: 'vite-pligin:babel-polyfill', + transform(code, id) { + const [filepath] = id.split('?'); + if (DEFAULT_FILTER.test(id) || DEFAULT_FILTER.test(filepath)) { + const result = transformSync(code, { + babelrc: false, + ast: true, + targets: config.targets, + configFile: false, + plugins: [ + [ + 'polyfill-corejs3', + { + method: 'usage-global', + }, + ], + ], + sourceFileName: id, + }); + + return { + code: result.code, + map: result.map, + }; + } + }, +}); diff --git a/packages/fes-build-vite/src/commands/build/getBuildConfig.js b/packages/fes-build-vite/src/commands/build/getBuildConfig.js index cc0f0a86..7f1aad1b 100644 --- a/packages/fes-build-vite/src/commands/build/getBuildConfig.js +++ b/packages/fes-build-vite/src/commands/build/getBuildConfig.js @@ -1,43 +1,39 @@ -import legacy from '@vitejs/plugin-legacy'; import { getInnerCommonConfig } from '../../common/getConfig'; +import babelPolyfillPlugin from './babelPolyfillPlugin'; export default async (api) => { const { deepmerge, getTargetsAndBrowsersList } = api.utils; const { build = {} } = api.config.viteOption; - const { browserslist } = getTargetsAndBrowsersList({ config: api.config }); + const { targets, browserslist } = getTargetsAndBrowsersList({ config: api.config }); - const bundleConfig = deepmerge( - { - mode: 'production', - css: { - postcss: { - plugins: [ - require('postcss-flexbugs-fixes'), - require('postcss-safe-parser'), - require('autoprefixer')({ - ...api.config.autoprefixer, - overrideBrowserslist: browserslist, - }), - ], - }, - }, - plugins: [ - legacy({ - targets: browserslist, - ...api.config.viteLegacy, - }), - ], - build: { - ...build, - terserOptions: build.terserOptions || api.config.terserOptions, - target: build.target || 'es2015', - outDir: build.outDir || api.config.outputPath || 'dist', - assetsInlineLimit: build.assetsInlineLimit || api.config.inlineLimit || 8192, + const bundleConfig = deepmerge(getInnerCommonConfig(api), { + mode: 'production', + css: { + postcss: { + plugins: [ + require('postcss-flexbugs-fixes'), + require('postcss-safe-parser'), + require('autoprefixer')({ + ...api.config.autoprefixer, + overrideBrowserslist: browserslist, + }), + ], }, }, - getInnerCommonConfig(api), - ); + plugins: [ + babelPolyfillPlugin({ + targets, + }), + ], + build: { + ...build, + terserOptions: build.terserOptions || api.config.terserOptions, + target: build.target || 'es2015', + outDir: build.outDir || api.config.outputPath || 'dist', + assetsInlineLimit: build.assetsInlineLimit || api.config.inlineLimit || 8192, + }, + }); return api.applyPlugins({ type: api.ApplyPluginsType.modify, diff --git a/packages/fes-build-vite/src/commands/dev/getDevConfig.js b/packages/fes-build-vite/src/commands/dev/getDevConfig.js index 8566c590..c5903994 100644 --- a/packages/fes-build-vite/src/commands/dev/getDevConfig.js +++ b/packages/fes-build-vite/src/commands/dev/getDevConfig.js @@ -24,20 +24,17 @@ export default async (api, args) => { args: {}, }); - const bundleConfig = deepmerge( - { - mode: 'development', - plugins: [viteMiddlewarePlugin(beforeMiddlewares, middlewares)], - server: { - ...server, - proxy: server?.proxy || api.config.proxy, - port, - host: hostname, - https: process.env.HTTPS || args.https, - }, + const bundleConfig = deepmerge(getInnerCommonConfig(api), { + mode: 'development', + plugins: [viteMiddlewarePlugin(beforeMiddlewares, middlewares)], + server: { + ...server, + proxy: server?.proxy || api.config.proxy, + port, + host: hostname, + https: process.env.HTTPS || args.https, }, - getInnerCommonConfig(api), - ); + }); return api.applyPlugins({ type: api.ApplyPluginsType.modify, diff --git a/packages/fes-build-webpack/src/plugins/commands/dev/connectHistoryMiddleware.js b/packages/fes-build-webpack/src/plugins/commands/dev/connectHistoryMiddleware.js index 0eada6f2..b73c38e3 100644 --- a/packages/fes-build-webpack/src/plugins/commands/dev/connectHistoryMiddleware.js +++ b/packages/fes-build-webpack/src/plugins/commands/dev/connectHistoryMiddleware.js @@ -2,16 +2,12 @@ import { extname } from 'path'; import historyFallback from 'connect-history-api-fallback'; const ASSET_EXT_NAMES = ['.ico', '.png', '.jpg', '.jpeg', '.gif', '.svg']; -const SKIP_PATHS_PREFIX = ['/@']; const proxyMiddleware = (api) => (req, res, next) => { const proxyConfig = api.config.proxy; if (proxyConfig && Object.keys(proxyConfig).some((path) => req.url.startsWith(path))) { return next(); } - if (SKIP_PATHS_PREFIX.find((prefix) => req.url.startsWith(prefix))) { - return next(); - } if (ASSET_EXT_NAMES.includes(extname(req.url))) { return next(); } diff --git a/packages/fes-build-webpack/src/plugins/commands/dev/index.js b/packages/fes-build-webpack/src/plugins/commands/dev/index.js index aed64328..b1285999 100644 --- a/packages/fes-build-webpack/src/plugins/commands/dev/index.js +++ b/packages/fes-build-webpack/src/plugins/commands/dev/index.js @@ -1,8 +1,3 @@ -/** - * @copy 该文件代码大部分出自 umi,有需要请参考: - * https://github.com/umijs/umi/blob/master/packages/preset-built-in/src/plugins/commands/dev/dev.ts - */ - export default (api) => { const { paths, diff --git a/packages/fes-template-vite/package.json b/packages/fes-template-vite/package.json index 57e74c42..00a6bb25 100644 --- a/packages/fes-template-vite/package.json +++ b/packages/fes-template-vite/package.json @@ -45,12 +45,13 @@ "@vue/compiler-sfc": "^3.2.2" }, "dependencies": { + "@fesjs/build-vite": "^1.0.0", "@fesjs/fes": "^2.0.0", "@fesjs/plugin-icon": "^2.0.0", "@fesjs/plugin-request": "^2.0.0", "@fesjs/plugin-windicss": "^2.0.8", - "@fesjs/build-vite": "^1.0.0", + "core-js": "^3.21.1", "vue": "^3.2.2" }, "private": true -} \ No newline at end of file +} diff --git a/yarn.lock b/yarn.lock index 6c9fcff8..4e680a34 100644 --- a/yarn.lock +++ b/yarn.lock @@ -179,6 +179,27 @@ json5 "^2.1.2" semver "^6.3.0" +"@babel/core@^7.17.9": + version "7.17.9" + resolved "https://registry.npmmirror.com/@babel/core/-/core-7.17.9.tgz#6bae81a06d95f4d0dec5bb9d74bbc1f58babdcfe" + integrity sha512-5ug+SfZCpDAkVp9SFIZAzlW18rlzsOcJGaetCjkySnrXXDUw9AR8cDUm1iByTmdWM6yxX6/zycaV76w3YTF2gw== + dependencies: + "@ampproject/remapping" "^2.1.0" + "@babel/code-frame" "^7.16.7" + "@babel/generator" "^7.17.9" + "@babel/helper-compilation-targets" "^7.17.7" + "@babel/helper-module-transforms" "^7.17.7" + "@babel/helpers" "^7.17.9" + "@babel/parser" "^7.17.9" + "@babel/template" "^7.16.7" + "@babel/traverse" "^7.17.9" + "@babel/types" "^7.17.0" + convert-source-map "^1.7.0" + debug "^4.1.0" + gensync "^1.0.0-beta.2" + json5 "^2.2.1" + semver "^6.3.0" + "@babel/eslint-parser@^7.16.3": version "7.17.0" resolved "https://registry.npmmirror.com/@babel/eslint-parser/-/eslint-parser-7.17.0.tgz#eabb24ad9f0afa80e5849f8240d0e5facc2d90d6" @@ -197,6 +218,15 @@ jsesc "^2.5.1" source-map "^0.5.0" +"@babel/generator@^7.17.9": + version "7.17.9" + resolved "https://registry.npmmirror.com/@babel/generator/-/generator-7.17.9.tgz#f4af9fd38fa8de143c29fce3f71852406fc1e2fc" + integrity sha512-rAdDousTwxbIxbz5I7GEQ3lUip+xVCXooZNbsydCWs3xA7ZsYOv+CFRdzGxRX78BmQHu9B1Eso59AOZQOJDEdQ== + dependencies: + "@babel/types" "^7.17.0" + jsesc "^2.5.1" + source-map "^0.5.0" + "@babel/helper-annotate-as-pure@^7.16.7": version "7.16.7" resolved "https://registry.npmmirror.com/@babel/helper-annotate-as-pure/-/helper-annotate-as-pure-7.16.7.tgz#bb2339a7534a9c128e3102024c60760a3a7f3862" @@ -280,6 +310,14 @@ "@babel/template" "^7.16.7" "@babel/types" "^7.16.7" +"@babel/helper-function-name@^7.17.9": + version "7.17.9" + resolved "https://registry.npmmirror.com/@babel/helper-function-name/-/helper-function-name-7.17.9.tgz#136fcd54bc1da82fcb47565cf16fd8e444b1ff12" + integrity sha512-7cRisGlVtiVqZ0MW0/yFB4atgpGLWEHUVYnb448hZK4x+vih0YO5UoS11XIYtZYqHd0dIPMdUSv8q5K4LdMnIg== + dependencies: + "@babel/template" "^7.16.7" + "@babel/types" "^7.17.0" + "@babel/helper-get-function-arity@^7.16.7": version "7.16.7" resolved "https://registry.npmmirror.com/@babel/helper-get-function-arity/-/helper-get-function-arity-7.16.7.tgz#ea08ac753117a669f1508ba06ebcc49156387419" @@ -404,6 +442,15 @@ "@babel/traverse" "^7.17.3" "@babel/types" "^7.17.0" +"@babel/helpers@^7.17.9": + version "7.17.9" + resolved "https://registry.npmmirror.com/@babel/helpers/-/helpers-7.17.9.tgz#b2af120821bfbe44f9907b1826e168e819375a1a" + integrity sha512-cPCt915ShDWUEzEp3+UNRktO2n6v49l5RSnG9M5pS24hA+2FAc5si+Pn1i4VVbQQ+jh+bIZhPFQOJOzbrOYY1Q== + dependencies: + "@babel/template" "^7.16.7" + "@babel/traverse" "^7.17.9" + "@babel/types" "^7.17.0" + "@babel/highlight@^7.16.7": version "7.16.10" resolved "https://registry.npmmirror.com/@babel/highlight/-/highlight-7.16.10.tgz#744f2eb81579d6eea753c227b0f570ad785aba88" @@ -418,6 +465,11 @@ resolved "https://registry.npmmirror.com/@babel/parser/-/parser-7.17.8.tgz#2817fb9d885dd8132ea0f8eb615a6388cca1c240" integrity sha512-BoHhDJrJXqcg+ZL16Xv39H9n+AqJ4pcDrQBGZN+wHxIysrLZ3/ECwCBUch/1zUNhnsXULcONU3Ei5Hmkfk6kiQ== +"@babel/parser@^7.17.9": + version "7.17.9" + resolved "https://registry.npmmirror.com/@babel/parser/-/parser-7.17.9.tgz#9c94189a6062f0291418ca021077983058e171ef" + integrity sha512-vqUSBLP8dQHFPdPi9bc5GK9vRkYHJ49fsZdtoJ8EQ8ibpwk5rPKfvNIwChB0KVXcIjcepEBBd2VHC5r9Gy8ueg== + "@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression@^7.16.7": version "7.16.7" resolved "https://registry.npmmirror.com/@babel/plugin-bugfix-safari-id-destructuring-collision-in-function-expression/-/plugin-bugfix-safari-id-destructuring-collision-in-function-expression-7.16.7.tgz#4eda6d6c2a0aa79c70fa7b6da67763dfe2141050" @@ -1137,11 +1189,6 @@ dependencies: regenerator-runtime "^0.13.4" -"@babel/standalone@^7.17.8": - version "7.17.8" - resolved "https://registry.npmmirror.com/@babel/standalone/-/standalone-7.17.8.tgz#517064e5b21015476d4dc9c6518b47bf2ec4d094" - integrity sha512-tr3SDpVnxR/fzrxyG+HZPAyEA9eTHZIAjy4eqrc7m+KBwsdo1YvTbUfJ6teWHQ177mk6GmdmltsIiOYCcvRPWA== - "@babel/template@^7.0.0", "@babel/template@^7.16.7", "@babel/template@^7.3.3": version "7.16.7" resolved "https://registry.npmmirror.com/@babel/template/-/template-7.16.7.tgz#8d126c8701fde4d66b264b3eba3d96f07666d155" @@ -1167,6 +1214,22 @@ debug "^4.1.0" globals "^11.1.0" +"@babel/traverse@^7.17.9": + version "7.17.9" + resolved "https://registry.npmmirror.com/@babel/traverse/-/traverse-7.17.9.tgz#1f9b207435d9ae4a8ed6998b2b82300d83c37a0d" + integrity sha512-PQO8sDIJ8SIwipTPiR71kJQCKQYB5NGImbOviK8K+kg5xkNSYXLBupuX9QhatFowrsvo9Hj8WgArg3W7ijNAQw== + dependencies: + "@babel/code-frame" "^7.16.7" + "@babel/generator" "^7.17.9" + "@babel/helper-environment-visitor" "^7.16.7" + "@babel/helper-function-name" "^7.17.9" + "@babel/helper-hoist-variables" "^7.16.7" + "@babel/helper-split-export-declaration" "^7.16.7" + "@babel/parser" "^7.17.9" + "@babel/types" "^7.17.0" + debug "^4.1.0" + globals "^11.1.0" + "@babel/types@^7.0.0", "@babel/types@^7.16.0", "@babel/types@^7.16.7", "@babel/types@^7.16.8", "@babel/types@^7.17.0", "@babel/types@^7.3.0", "@babel/types@^7.3.3", "@babel/types@^7.4.4": version "7.17.0" resolved "https://registry.npmmirror.com/@babel/types/-/types-7.17.0.tgz#a826e368bccb6b3d84acd76acad5c0d87342390b" @@ -2145,17 +2208,6 @@ dependencies: "@types/yargs-parser" "*" -"@vitejs/plugin-legacy@^1.8.0": - version "1.8.0" - resolved "https://registry.npmmirror.com/@vitejs/plugin-legacy/-/plugin-legacy-1.8.0.tgz#6521e6de924641094aad5d804eaef3ac7db71284" - integrity sha512-S3+uL1zp8GLUbmJAQk2wQbZLTyISKRFSMBwCFI3XQVRD3OZshqkiPyOKdRiSPlP9HoGz+q90kk+1qPm1tJRqCg== - dependencies: - "@babel/standalone" "^7.17.8" - core-js "^3.21.1" - magic-string "^0.26.1" - regenerator-runtime "^0.13.9" - systemjs "^6.12.1" - "@vitejs/plugin-vue-jsx@^1.3.8": version "1.3.9" resolved "https://registry.npmmirror.com/@vitejs/plugin-vue-jsx/-/plugin-vue-jsx-1.3.9.tgz#2a9f9c5adcc90556aa56bc60dd923e6259d5f40e" @@ -3260,7 +3312,7 @@ babel-plugin-polyfill-corejs2@^0.3.0: "@babel/helper-define-polyfill-provider" "^0.3.1" semver "^6.1.1" -babel-plugin-polyfill-corejs3@^0.5.0: +babel-plugin-polyfill-corejs3@^0.5.0, babel-plugin-polyfill-corejs3@^0.5.2: version "0.5.2" resolved "https://registry.npmmirror.com/babel-plugin-polyfill-corejs3/-/babel-plugin-polyfill-corejs3-0.5.2.tgz#aabe4b2fa04a6e038b688c5e55d44e78cd3a5f72" integrity sha512-G3uJih0XWiID451fpeFaYGVuxHEjzKTHtc9uGFEjR6hHrvNzeS/PX+LLLcetJcytsB5m4j+K3o/EpXJNb/5IEQ== @@ -7671,7 +7723,7 @@ json-stringify-safe@^5.0.1, json-stringify-safe@~5.0.1: resolved "https://registry.npmmirror.com/json-stringify-safe/-/json-stringify-safe-5.0.1.tgz#1296a2d58fd45f19a0f6ce01d65701e2c735b6eb" integrity sha512-ZClg6AaYvamvYEE82d3Iyd3vSSIjQ+odgjaTzRuO3s7toCdFKczob2i0zCh7JE8kWn17yvAWhUVxvqGwUalsRA== -json5@2.x, json5@^2.1.2, json5@^2.2.0: +json5@2.x, json5@^2.1.2, json5@^2.2.0, json5@^2.2.1: version "2.2.1" resolved "https://registry.npmmirror.com/json5/-/json5-2.2.1.tgz#655d50ed1e6f95ad1a3caababd2b0efda10b395c" integrity sha512-1hqLFMSrGHRHxav9q9gNjJ5EXznIxGVO09xQRrwplcS8qs28pZ8s8hupZAmqDwZUmVZ2Qb2jnyPOWcDH8m8dlA== @@ -8038,13 +8090,6 @@ magic-string@^0.25.0, magic-string@^0.25.7: dependencies: sourcemap-codec "^1.4.8" -magic-string@^0.26.1: - version "0.26.1" - resolved "https://registry.npmmirror.com/magic-string/-/magic-string-0.26.1.tgz#ba9b651354fa9512474199acecf9c6dbe93f97fd" - integrity sha512-ndThHmvgtieXe8J/VGPjG+Apu7v7ItcD5mhEIvOscWjPF/ccOiLxHaSuCAS2G+3x4GKsAbT8u7zdyamupui8Tg== - dependencies: - sourcemap-codec "^1.4.8" - make-dir@^2.0.0, make-dir@^2.1.0: version "2.1.0" resolved "https://registry.npmmirror.com/make-dir/-/make-dir-2.1.0.tgz#5f0310e18b8be898cc07009295a30ae41e91e6f5" @@ -9747,7 +9792,7 @@ regenerate@^1.4.2: resolved "https://registry.npmmirror.com/regenerate/-/regenerate-1.4.2.tgz#b9346d8827e8f5a32f7ba29637d398b69014848a" integrity sha512-zrceR/XhGYU/d/opr2EKO7aRHUeiBI8qjtfHqADTwZd6Szfy16la6kqD0MIUs5z5hx6AaKa+PixpPrR289+I0A== -regenerator-runtime@^0.13.4, regenerator-runtime@^0.13.9: +regenerator-runtime@^0.13.4: version "0.13.9" resolved "https://registry.npmmirror.com/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz#8925742a98ffd90814988d7566ad30ca3b263b52" integrity sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA== @@ -10921,11 +10966,6 @@ symbol-tree@^3.2.4: resolved "https://registry.npmmirror.com/symbol-tree/-/symbol-tree-3.2.4.tgz#430637d248ba77e078883951fb9aa0eed7c63fa2" integrity sha512-9QNk5KwDF+Bvz+PyObkmSYjI5ksVUYtjW7AU22r2NKcfLJcXp96hkDWU3+XndOsUb+AQ9QhfzfCT2O+CNWT5Tw== -systemjs@^6.12.1: - version "6.12.1" - resolved "https://registry.npmmirror.com/systemjs/-/systemjs-6.12.1.tgz#47cdd23a6ec9f1b01cf5b5f70562c8550da229d3" - integrity sha512-hqTN6kW+pN6/qro6G9OZ7ceDQOcYno020zBQKpZQLsJhYTDMCMNfXi/Y8duF5iW+4WWZr42ry0MMkcRGpbwG2A== - tapable@^2.0.0, tapable@^2.1.1, tapable@^2.2.0: version "2.2.1" resolved "https://registry.npmmirror.com/tapable/-/tapable-2.2.1.tgz#1967a73ef4060a82f12ab96af86d52fdb76eeca0"