我正试着从Webpack转到维特。我使用的是库模式,但是每次构建它时,我都会在构建的文件中获得基于节点的代码。到目前为止,这就是我所拥有的:
import {resolve} from 'path'
import {defineConfig, splitVendorChunkPlugin} from 'vite'
export default defineConfig(({mode}) => {
return {
plugins: [splitVendorChunkPlugin()],
build: {
emptyOutDir: false,
minify: mode === "dev" ? false : 'terser',
target: 'es2015',
lib: {
formats: ['cjs'],
name: 'Spark2',
entry: resolve(__dirname, 'src/app.ts'),
},
commonjsOptions: {
include: [/node_modules/]
},
outDir: './static',
rollupOptions: {
output: {
manualChunks: (id) => {
if (id.includes('node_modules')) {
return 'vendors';
}
},
entryFileNames: mode === "dev" ? 'js/main.js' : 'js/main.min.js',
chunkFileNames: mode === "dev" ? 'js/[name].js' : 'js/[name].min.js',
assetFileNames: mode === "dev" ? '[ext]/[name].[ext]' : '[ext]/[name].min.[ext]',
},
}
}
}
})
它构建得很好,但是在浏览器中,我得到了一个错误,因为:
Uncaught ReferenceError: process is not defined
at vendors.js:70:19
查看代码,我得到了一行类似于const EMPTY_OBJ = process.env.NODE_ENV !== "production" ? Object.freeze({}) : {};
的代码
我应该如何为网络捆绑这个包呢?
发布于 2022-10-19 08:13:57
我通过在define
下添加plugins
对象来修正它。看上去像是:
// removed for brevity
plugins: [splitVendorChunkPlugin()],
define: {
'process.env.NODE_ENV': JSON.stringify(mode),
},
// removed for brevity
关于这一点的更多信息可以在https://vitejs.dev/config/#environment-variables上找到。
https://stackoverflow.com/questions/74120349
复制相似问题