fix(vite): don't replace process.env (#1543)

This commit is contained in:
Daniel Roe 2021-10-28 15:48:42 +01:00 committed by GitHub
parent fe7bc9c7cf
commit f5aea9f742
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 5 additions and 28 deletions

View File

@ -4,7 +4,6 @@ import { createVuePlugin } from 'vite-plugin-vue2'
import PluginLegacy from '@vitejs/plugin-legacy' import PluginLegacy from '@vitejs/plugin-legacy'
import consola from 'consola' import consola from 'consola'
import { jsxPlugin } from './plugins/jsx' import { jsxPlugin } from './plugins/jsx'
import { replace } from './plugins/replace'
import { ViteBuildContext, ViteOptions } from './types' import { ViteBuildContext, ViteOptions } from './types'
export async function buildClient (ctx: ViteBuildContext) { export async function buildClient (ctx: ViteBuildContext) {
@ -17,7 +16,11 @@ export async function buildClient (ctx: ViteBuildContext) {
const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, { const clientConfig: vite.InlineConfig = vite.mergeConfig(ctx.config, {
define: { define: {
global: 'globalThis' global: 'globalThis',
'process.client': 'true',
'process.server': 'false',
'process.static': 'false',
'module.hot': 'false'
}, },
cacheDir: resolve(ctx.nuxt.options.rootDir, 'node_modules/.cache/vite/client'), cacheDir: resolve(ctx.nuxt.options.rootDir, 'node_modules/.cache/vite/client'),
resolve: { resolve: {
@ -33,13 +36,6 @@ export async function buildClient (ctx: ViteBuildContext) {
ssrManifest: true ssrManifest: true
}, },
plugins: [ plugins: [
replace({
'process.env': 'import.meta.env',
'process.client': 'true',
'process.server': 'false',
'process.static': 'false',
'module.hot': 'false'
}),
jsxPlugin(), jsxPlugin(),
createVuePlugin(ctx.config.vue), createVuePlugin(ctx.config.vue),
PluginLegacy() PluginLegacy()

View File

@ -8,7 +8,6 @@ import type { Connect } from 'vite'
import { visualizer } from 'rollup-plugin-visualizer' import { visualizer } from 'rollup-plugin-visualizer'
import { transform } from 'esbuild' import { transform } from 'esbuild'
import { cacheDirPlugin } from './plugins/cache-dir' import { cacheDirPlugin } from './plugins/cache-dir'
import { replace } from './plugins/replace'
import { wpfs } from './utils/wpfs' import { wpfs } from './utils/wpfs'
import type { ViteBuildContext, ViteOptions } from './vite' import type { ViteBuildContext, ViteOptions } from './vite'
import { writeManifest } from './manifest' import { writeManifest } from './manifest'
@ -37,7 +36,6 @@ export async function buildClient (ctx: ViteBuildContext) {
outDir: resolve(ctx.nuxt.options.buildDir, 'dist/client') outDir: resolve(ctx.nuxt.options.buildDir, 'dist/client')
}, },
plugins: [ plugins: [
replace({ 'process.env': 'import.meta.env' }),
cacheDirPlugin(ctx.nuxt.options.rootDir, 'client'), cacheDirPlugin(ctx.nuxt.options.rootDir, 'client'),
vuePlugin(ctx.config.vue), vuePlugin(ctx.config.vue),
viteJsxPlugin() viteJsxPlugin()

View File

@ -1,17 +0,0 @@
import type { Plugin } from 'vite'
export function replace (replacements: Record<string, string>) {
return <Plugin>{
name: 'nuxt:replace',
transform (code) {
Object.entries(replacements).forEach(([key, value]) => {
const escapedKey = key.replace(/\./g, '\\.')
code = code.replace(new RegExp(escapedKey, 'g'), value)
})
return {
code,
map: null
}
}
}
}