diff --git a/__fixtures__/app_process_env/js/@nextcloud-vite-config-main.mjs b/__fixtures__/app_process_env/js/@nextcloud-vite-config-main.mjs new file mode 100644 index 00000000..7af7b74d --- /dev/null +++ b/__fixtures__/app_process_env/js/@nextcloud-vite-config-main.mjs @@ -0,0 +1,2 @@ +/*! third party licenses: js/vendor.LICENSE.txt */ +window.my_timezone={}.TZ; diff --git a/__fixtures__/app_process_env/js/@nextcloud-vite-config-main.mjs.map b/__fixtures__/app_process_env/js/@nextcloud-vite-config-main.mjs.map new file mode 100644 index 00000000..5d007175 --- /dev/null +++ b/__fixtures__/app_process_env/js/@nextcloud-vite-config-main.mjs.map @@ -0,0 +1 @@ +{"version":3,"file":"@nextcloud-vite-config-main.mjs","sources":["../main.js"],"sourcesContent":["/**\n * SPDX-FileCopyrightText: 2023 Ferdinand Thiessen \n *\n * SPDX-License-Identifier: AGPL-3.0-or-later\n */\nwindow.my_timezone = process.env.TZ\n"],"names":[],"mappings":";AAKA,OAAO,YAA0B,GAAA"} \ No newline at end of file diff --git a/__fixtures__/app_process_env/main.js b/__fixtures__/app_process_env/main.js new file mode 100644 index 00000000..be302c38 --- /dev/null +++ b/__fixtures__/app_process_env/main.js @@ -0,0 +1,6 @@ +/** + * SPDX-FileCopyrightText: 2023 Ferdinand Thiessen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ +window.my_timezone = process.env.TZ diff --git a/__tests__/__snapshots__/appconfig.spec.ts.snap b/__tests__/__snapshots__/appconfig.spec.ts.snap new file mode 100644 index 00000000..e7c381ba --- /dev/null +++ b/__tests__/__snapshots__/appconfig.spec.ts.snap @@ -0,0 +1,7 @@ +// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html + +exports[`app config > replaces process.env 1`] = ` +"/*! third party licenses: js/vendor.LICENSE.txt */ +window.my_timezone={}.TZ; +" +`; diff --git a/__tests__/appconfig.spec.ts b/__tests__/appconfig.spec.ts new file mode 100644 index 00000000..88f41759 --- /dev/null +++ b/__tests__/appconfig.spec.ts @@ -0,0 +1,35 @@ +/** + * SPDX-FileCopyrightText: 2023 Ferdinand Thiessen + * + * SPDX-License-Identifier: AGPL-3.0-or-later + */ + +import { build } from 'vite' +// ok as this is just for tests +// eslint-disable-next-line n/no-extraneous-import +import type { RollupOutput, OutputChunk } from 'rollup' +import { describe, it, expect } from 'vitest' +import { createAppConfig } from '../lib/appConfig' +import { fileURLToPath } from 'url' +import { resolve } from 'path' + +const __dirname = fileURLToPath(new URL('.', import.meta.url)) + +describe('app config', () => { + it('replaces process.env', async () => { + const root = resolve(__dirname, '../__fixtures__/app_process_env') + + const resolved = await createAppConfig({ + main: resolve(root, 'main.js'), + })({ command: 'build', mode: 'production' }) + + const result = await build({ + ...resolved, + root, + }) + const { output } = result as RollupOutput + const code = (output[0] as OutputChunk).code + expect(code.includes('process.env')).toBe(false) + expect(code).toMatchSnapshot() + }) +}) diff --git a/lib/baseConfig.ts b/lib/baseConfig.ts index 34372500..58371392 100644 --- a/lib/baseConfig.ts +++ b/lib/baseConfig.ts @@ -129,15 +129,6 @@ export function createBaseConfig(options: BaseOptions = {}): UserConfigFn { // Remove unneeded whitespace options?.minify ? minifyPlugin() : undefined, ], - define: { - // process env replacement (keep order of this rules) - 'globalThis.process.env.NODE_ENV': JSON.stringify(mode), - 'globalThis.process.env.': '({}).', - 'global.process.env.NODE_ENV': JSON.stringify(mode), - 'global.process.env.': '({}).', - 'process.env.NODE_ENV': JSON.stringify(mode), - 'process.env.': '({}).', - }, esbuild: { legalComments: 'inline', target: browserslistToEsbuild(),