vite-plugin-environment icon indicating copy to clipboard operation
vite-plugin-environment copied to clipboard

Plugin crushes the build in Preact application

Open alfesen opened this issue 2 years ago • 1 comments

process.env variables work perfectly well during development, but every attempt to build the app results in a bunch of errors.

[commonjs--resolver] Transform failed with 2 errors:  
error: The define key "process.env.ProgramFiles(x86)" contains invalid identifier "ProgramFiles(x86)"       
error: The define key "process.env.CommonProgramFiles(x86)" contains invalid identifier "CommonProgramFiles(x86)"
error during build:
Error: Transform failed with 2 errors:
error: The define key "process.env.ProgramFiles(x86)" contains invalid identifier "ProgramFiles(x86)"       
error: The define key "process.env.CommonProgramFiles(x86)" contains invalid identifier "CommonProgramFiles(x86)"
    at failureErrorWithLog (*path*\node_modules\.pnpm\[email protected]\node_modules\esbuild\lib\main.js:1651:15)
    at *path*\node_modules\.pnpm\[email protected]\node_modules\esbuild\lib\main.js:849:29
    at responseCallbacks.<computed> (*path*\node_modules\.pnpm\[email protected]\node_modules\esbuild\lib\main.js:704:9)   
    at handleIncomingPacket (*path*\node_modules\.pnpm\[email protected]\node_modules\esbuild\lib\main.js:764:9)
    at Socket.readFromStdout (*path*\node_modules\.pnpm\[email protected]\node_modules\esbuild\lib\main.js:680:7)
    at Socket.emit (node:events:514:28)
    at addChunk (node:internal/streams/readable:545:12)
    at readableAddChunkPushByteMode (node:internal/streams/readable:495:3)
    at Readable.push (node:internal/streams/readable:375:5)
    at Pipe.onStreamRead (node:internal/stream_base_commons:190:23)
 ELIFECYCLE  Command failed with exit code 1.

My initial guess was that it's pnpm's fault, but it resulted the same with npm. Only the removal of EnvironmentPlugin solves the build problem.

import { defineConfig } from 'vite'
import preact from '@preact/preset-vite'
import { resolve } from 'path'
import EnvironmentPlugin from 'vite-plugin-environment'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [preact(), EnvironmentPlugin('all')],
  resolve: {
    alias: {
      components: resolve(__dirname, './src/components'),
      hooks: resolve(__dirname, './src/hooks')
    }
  }
})

alfesen avatar Feb 06 '24 09:02 alfesen

Sorry for necro-ing but having the same issues.

I've tracked it down to being the problem with "all" as it injects all system variables, and these happen to be formatted in such a way that they are not a valid identifier. You could try exposing individual variables with the array syntax, but this, at least for me, defeats the purpose of this plugin as we can do this with define just as well

DelliriumX avatar May 31 '24 00:05 DelliriumX