diff --git a/packages/build/vite-config/package.json b/packages/build/vite-config/package.json index 9db3d9956f..d598a3054e 100644 --- a/packages/build/vite-config/package.json +++ b/packages/build/vite-config/package.json @@ -30,13 +30,13 @@ "fs-extra": "^10.1.0", "less": "^4.1.2", "path": "^0.12.7", - "rollup-plugin-polyfill-node": "^0.13.0", "rollup-plugin-visualizer": "^5.8.3", "semver": "^7.7.1", "shelljs": "^0.8.5", "vite": "^5.4.2", "vite-plugin-externalize-dependencies": "^1.0.1", "vite-plugin-monaco-editor": "^1.1.0", + "vite-plugin-node-polyfills": "^0.23.0", "vite-plugin-static-copy": "^1.0.6", "vite-plugin-svg-icons": "^2.0.1" }, diff --git a/packages/build/vite-config/src/default-config.js b/packages/build/vite-config/src/default-config.js index 0c53dcedf4..c200fed23d 100644 --- a/packages/build/vite-config/src/default-config.js +++ b/packages/build/vite-config/src/default-config.js @@ -5,7 +5,7 @@ import monacoEditorPluginCjs from 'vite-plugin-monaco-editor' import vueJsx from '@vitejs/plugin-vue-jsx' import nodeGlobalsPolyfillPluginCjs from '@esbuild-plugins/node-globals-polyfill' import nodeModulesPolyfillPluginCjs from '@esbuild-plugins/node-modules-polyfill' -import nodePolyfill from 'rollup-plugin-polyfill-node' +import { nodePolyfills } from 'vite-plugin-node-polyfills' import esbuildCopy from 'esbuild-plugin-copy' import { createSvgIconsPlugin } from 'vite-plugin-svg-icons' import visualizerCjs from 'rollup-plugin-visualizer' @@ -31,7 +31,17 @@ const getDefaultConfig = (engineConfig) => { publicDir: path.resolve(root, './public'), resolve: { extensions: ['.js', '.jsx', '.vue', '.ts', '.tsx'], - alias: {} + alias: { + ...Object.fromEntries( + ['buffer', 'global', 'process'].map((k) => { + const location = `vite-plugin-node-polyfills/shims/${k}` + return [ + location, + path.resolve(`node_modules/@opentiny/tiny-engine-vite-config/node_modules/${location}/dist/index.cjs`) + ] + }) + ) + } }, server: { // 这里保证本地启动服务是localhost,支持js多线程和谷歌浏览器读写本地文件api @@ -75,7 +85,14 @@ const getDefaultConfig = (engineConfig) => { } } }), - vueJsx() + vueJsx(), + nodePolyfills({ + globals: { + Buffer: true, // can also be 'build', 'dev', or false + global: true, + process: true + } + }) ], optimizeDeps: { esbuildOptions: { @@ -106,7 +123,6 @@ const getDefaultConfig = (engineConfig) => { minify: true, sourcemap: false, rollupOptions: { - plugins: [nodePolyfill({ include: null })], // 使用@rollup/plugin-inject的默认值{include: null}, 即在所有代码中生效 input: { index: path.resolve(process.cwd(), './index.html'), preview: path.resolve(process.cwd(), './preview.html') diff --git a/packages/canvas/package.json b/packages/canvas/package.json index 979bbb7968..578915a666 100644 --- a/packages/canvas/package.json +++ b/packages/canvas/package.json @@ -55,8 +55,8 @@ "@types/diff-match-patch": "^1.0.36", "@vitejs/plugin-vue": "^5.1.2", "@vitejs/plugin-vue-jsx": "^4.0.1", - "rollup-plugin-polyfill-node": "^0.13.0", - "vite": "^5.4.2" + "vite": "^5.4.2", + "vite-plugin-node-polyfills": "^0.23.0" }, "peerDependencies": { "@opentiny/vue": "^3.20.0", diff --git a/packages/canvas/scripts/canvas-vite.config.ts b/packages/canvas/scripts/canvas-vite.config.ts index 178f6e92a5..3889306110 100644 --- a/packages/canvas/scripts/canvas-vite.config.ts +++ b/packages/canvas/scripts/canvas-vite.config.ts @@ -13,13 +13,21 @@ import { defineConfig } from 'vite' import vue from '@vitejs/plugin-vue' import vueJsx from '@vitejs/plugin-vue-jsx' -import nodePolyfill from 'rollup-plugin-polyfill-node' +import { nodePolyfills } from 'vite-plugin-node-polyfills' +import path from 'node:path' // https://vitejs.dev/config/ export default defineConfig({ base: './', plugins: [ vue(), vueJsx(), + nodePolyfills({ + globals: { + Buffer: true, + global: true, + process: true + } + }), { name: 'vite-plugin-style-inline-loader', apply: 'build', @@ -46,13 +54,17 @@ export default defineConfig({ publicDir: false, resolve: { alias: { - assert: 'rollup-plugin-node-polyfills/polyfills/assert' + ...Object.fromEntries( + ['buffer', 'global', 'process'].map((k) => { + const location = `vite-plugin-node-polyfills/shims/${k}` + return [location, path.resolve(`node_modules/${location}/dist/index.cjs`)] + }) + ) } }, build: { cssCodeSplit: true, rollupOptions: { - plugins: [nodePolyfill({ include: null })], external: [ 'vue', 'vue-i18n', diff --git a/packages/design-core/package.json b/packages/design-core/package.json index 3e361e6f77..bf50434a97 100644 --- a/packages/design-core/package.json +++ b/packages/design-core/package.json @@ -108,8 +108,8 @@ "cross-env": "^7.0.3", "eslint": "^8.38.0", "less": "^4.1.2", - "rollup-plugin-polyfill-node": "^0.13.0", "vite": "^5.4.2", + "vite-plugin-node-polyfills": "^0.23.0", "vite-plugin-static-copy": "^1.0.6" }, "peerDependencies": { diff --git a/packages/design-core/vite.config.js b/packages/design-core/vite.config.js index e17c3b554a..415f064354 100644 --- a/packages/design-core/vite.config.js +++ b/packages/design-core/vite.config.js @@ -2,14 +2,14 @@ import { defineConfig } from 'vite' import path from 'path' import vue from '@vitejs/plugin-vue' import vueJsx from '@vitejs/plugin-vue-jsx' -import nodeGlobalsPolyfillPluginCjs from '@esbuild-plugins/node-globals-polyfill' -import nodeModulesPolyfillPluginCjs from '@esbuild-plugins/node-modules-polyfill' -import nodePolyfill from 'rollup-plugin-polyfill-node' +// import nodeGlobalsPolyfillPluginCjs from '@esbuild-plugins/node-globals-polyfill' +// import nodeModulesPolyfillPluginCjs from '@esbuild-plugins/node-modules-polyfill' +// import { nodePolyfills } from 'vite-plugin-node-polyfills' import { fileURLToPath } from 'node:url' import { viteStaticCopy } from 'vite-plugin-static-copy' -const nodeGlobalsPolyfillPlugin = nodeGlobalsPolyfillPluginCjs.default -const nodeModulesPolyfillPlugin = nodeModulesPolyfillPluginCjs.default +// const nodeGlobalsPolyfillPlugin = nodeGlobalsPolyfillPluginCjs.default +// const nodeModulesPolyfillPlugin = nodeModulesPolyfillPluginCjs.default const __filename = fileURLToPath(import.meta.url) const __dirname = path.dirname(__filename) @@ -44,19 +44,36 @@ export default defineConfig({ } ] }) + // nodePolyfills({ + // globals: { + // Buffer: true, + // global: true, + // process: true + // } + // }) ], publicDir: false, - optimizeDeps: { - esbuildOptions: { - plugins: [ - nodeGlobalsPolyfillPlugin({ - process: true, - buffer: true - }), - nodeModulesPolyfillPlugin() - ] + resolve: { + alias: { + ...Object.fromEntries( + ['buffer', 'global', 'process'].map((k) => { + const location = `vite-plugin-node-polyfills/shims/${k}` + return [location, path.resolve(`node_modules/${location}/dist/index.cjs`)] + }) + ) } }, + // optimizeDeps: { + // esbuildOptions: { + // plugins: [ + // nodeGlobalsPolyfillPlugin({ + // process: true, + // buffer: true + // }), + // nodeModulesPolyfillPlugin() + // ] + // } + // }, base: './', define: { 'import.meta': 'import.meta', @@ -86,7 +103,7 @@ export default defineConfig({ formats: ['es'] }, rollupOptions: { - plugins: [nodePolyfill({ include: null }), addViteIgnorePlugin()], + plugins: [addViteIgnorePlugin()], output: { banner: (chunk) => { if (chunk.name === 'index') {