我正在尝试用Vite创建我的React应用程序的生产构建。当我运行npm run dev
命令时,应用程序将启动并似乎正常工作,但在构建期间,我总是通过一些第三方依赖项获得这些类型的解析器错误,告诉它包含一个意外的令牌。
我错过了一些东西在我的配置文件或它可能是什么?
❯ npm run build
> project build
> tsc && vite build
vite v2.9.12 building for production...
✓ 4 modules transformed.
[commonjs] Unexpected token (1:20) in /Users/../node_modules/react-router-dom/index.js
file: /Users/../node_modules/react-router-dom/index.js:1:20
1: /**
^
2: * React Router DOM v6.3.0
3: *
error during build:
SyntaxError: Unexpected token (1:20) in /Users/../node_modules/react-router-dom/index.js
at Parser.pp$4.raise (/Users/../node_modules/rollup/dist/shared/rollup.js:19740:13)
at Parser.pp$9.unexpected (/Users/../node_modules/rollup/dist/shared/rollup.js:17034:8)
at Parser.pp$5.parseIdent (/Users/../node_modules/rollup/dist/shared/rollup.js:19671:10)
at Parser.pp$8.parseImportSpecifiers (/Users/../node_modules/rollup/dist/shared/rollup.js:18117:27)
at Parser.pp$8.parseImport (/Users/../node_modules/rollup/dist/shared/rollup.js:18078:28)
at Parser.pp$8.parseStatement (/Users/../rollup/dist/shared/rollup.js:17210:49)
at Parser.pp$8.parseTopLevel (/Users/../node_modules/rollup/dist/shared/rollup.js:17091:21)
at Parser.parse (/Users/../node_modules/rollup/dist/shared/rollup.js:16863:15)
at Function.parse (/Users/../node_modules/rollup/dist/shared/rollup.js:16913:35)
at Graph.contextParse (/Users/m../node_modules/rollup/dist/shared/rollup.js:23017:38)
项目是用React和TypeScript初始化的。我必须为几个需要节点依赖的库配置多边形。vite.config.ts
看起来像这样:
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import * as path from 'path';
import { NodeGlobalsPolyfillPlugin } from '@esbuild-plugins/node-globals-polyfill';
import { NodeModulesPolyfillPlugin } from '@esbuild-plugins/node-modules-polyfill';
import polyfillNode from 'rollup-plugin-polyfill-node';
// https://vitejs.dev/config/
export default defineConfig({
plugins: [react(), polyfillNode()],
resolve: {
alias: {
'@': path.resolve(__dirname, 'src'),
util: 'rollup-plugin-node-polyfills/polyfills/util',
events: 'rollup-plugin-node-polyfills/polyfills/events',
stream: 'rollup-plugin-node-polyfills/polyfills/stream',
path: 'rollup-plugin-node-polyfills/polyfills/path',
zlib: 'rollup-plugin-node-polyfills/polyfills/zlib',
},
},
define: {
// By default, Vite doesn't include shims for NodeJS/
// necessary for segment analytics lib to work
global: {},
process: { env: { HOME: './src' } },
},
server: {
host: true,
port: 5001,
hmr: { host: 'localhost' },
proxy: {
'/api': {
target: 'http://localhost:3001',
changeOrigin: true,
secure: false,
rewrite: (path) => path.replace(/^/api/, ''),
},
},
},
optimizeDeps: {
esbuildOptions: {
// Node.js global to browser globalThis
define: {
global: 'globalThis',
},
// Enable esbuild polyfill plugins
plugins: [
NodeGlobalsPolyfillPlugin({
process: true,
buffer: true,
}),
NodeModulesPolyfillPlugin(),
],
},
},
build: {
commonjsOptions: {
transformMixedEsModules: true,
},
rollupOptions: {
output: {
dir: 'output',
format: 'cjs',
},
plugins: [
// Enable rollup polyfills plugin
// used during production bundling
polyfillNode(),
],
},
},
});
package.json
:
{
"scripts": {
"dev": "vite",
"build": "tsc && vite build",
"preview": "vite preview"
},
"dependencies": {
"@emotion/react": "^11.9.3",
"@emotion/styled": "^11.9.3",
"@esbuild-plugins/node-globals-polyfill": "^0.1.1",
"@esbuild-plugins/node-modules-polyfill": "^0.1.4",
"@mui/icons-material": "^5.8.4",
"@mui/lab": "^5.0.0-alpha.87",
"@mui/material": "^5.8.5",
"@mui/x-date-pickers": "^5.0.0-alpha.6",
"@react-pdf/renderer": "^2.2.0",
"@types/luxon": "^2.3.2",
"amazon-cognito-identity-js": "^5.2.9",
"axios": "^0.27.2",
"draft-js": "^0.11.7",
"events": "^3.3.0",
"luxon": "^2.4.0",
"preval.macro": "^5.0.0",
"react": "^18.0.0",
"react-beautiful-dnd": "^13.1.0",
"react-dom": "^18.0.0",
"react-draft-wysiwyg": "^1.14.7",
"react-dropzone": "^14.2.1",
"react-redux": "^8.0.2",
"react-router-dom": "^6.3.0",
"redux-logger": "^3.0.6",
"rollup-plugin-polyfill-node": "^0.9.0",
"stream": "^0.0.2",
"util": "^0.12.4",
"uuid": "^8.3.2",
"zlib": "^1.0.5"
},
"devDependencies": {
"@reduxjs/toolkit": "^1.8.2",
"@rollup/plugin-commonjs": "^22.0.1",
"@testing-library/jest-dom": "^5.16.4",
"@testing-library/react": "^13.3.0",
"@types/draft-js": "^0.11.9",
"@types/jest": "^28.1.3",
"@types/node": "^18.0.0",
"@types/preval.macro": "^3.0.0",
"@types/react": "^18.0.0",
"@types/react-beautiful-dnd": "^13.1.2",
"@types/react-dom": "^18.0.0",
"@types/react-draft-wysiwyg": "^1.13.4",
"@types/react-redux": "^7.1.24",
"@types/react-router-dom": "^5.3.3",
"@types/redux-logger": "^3.0.9",
"@types/uuid": "^8.3.4",
"@vitejs/plugin-react": "^1.3.0",
"jest": "^28.1.1",
"typescript": "^4.6.3",
"vite": "^2.9.9"
}
}
我认为你应该使用global: '({})'
而不是global: {}
在vite.config.ts
查看下面的链接了解更多细节。https://github.com/vitejs/vite/issues/8909
我遇到了一个类似的问题,但通过将下面一行添加到我的条目文件的顶部(在vue项目中是main.ts
)
window.global ||= window;
确保在之前添加。
请务必从vite.config.ts中删除global的定义
global: {} // Delete this
window.global ||= window;
在运行最终构建时为我工作,但对于运行
的开发返回了vite问题。的工作都很好,我想是必要的第三个解决方案,也许使用vite.config.js