From 55de8561cf36530110112744690b92bb94882c30 Mon Sep 17 00:00:00 2001 From: Ferdinand Thiessen Date: Wed, 4 Oct 2023 03:38:40 +0200 Subject: [PATCH] fix: Remove `define` of `process.env` from base config Removing `process.env` is only required for apps, as we now build apps not in lib mode it is removed automatically by vite Signed-off-by: Ferdinand Thiessen --- .gitignore | 5 +++ .reuse/dep5 | 2 +- __fixtures__/app_process_env/main.js | 6 ++++ .../__snapshots__/appconfig.spec.ts.snap | 7 ++++ __tests__/appconfig.spec.ts | 35 +++++++++++++++++++ lib/baseConfig.ts | 9 ----- 6 files changed, 54 insertions(+), 10 deletions(-) create mode 100644 __fixtures__/app_process_env/main.js create mode 100644 __tests__/__snapshots__/appconfig.spec.ts.snap create mode 100644 __tests__/appconfig.spec.ts diff --git a/.gitignore b/.gitignore index ef229ed..787ecd1 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,8 @@ +# SPDX-FileCopyrightText: 2023 Ferdinand Thiessen +# SPDX-License-Identifier: AGPL-3.0-or-later + +__fixtures__/*/js coverage/ dist/ node_modules/ +js/ \ No newline at end of file diff --git a/.reuse/dep5 b/.reuse/dep5 index b720a45..5b1e63e 100644 --- a/.reuse/dep5 +++ b/.reuse/dep5 @@ -3,6 +3,6 @@ Upstream-Name: nextcloud-vite-config Upstream-Contact: Ferdinand Thiessen Source: https://github.com/susnux/nextcloud-vite-config -Files: package-lock.json package.json tsconfig.json .gitignore banner-template.txt README.md CHANGELOG.md +Files: package-lock.json package.json tsconfig.json banner-template.txt README.md CHANGELOG.md __tests__/__snapshots__/* Copyright: Ferdinand Thiessen License: AGPL-3.0-or-later diff --git a/__fixtures__/app_process_env/main.js b/__fixtures__/app_process_env/main.js new file mode 100644 index 0000000..be302c3 --- /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 0000000..e7c381b --- /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 0000000..88f4175 --- /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 3437250..5837139 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(),