Skip to content

Commit

Permalink
fix: Remove define of process.env from base config
Browse files Browse the repository at this point in the history
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 <opensource@fthiessen.de>
  • Loading branch information
susnux committed Oct 4, 2023
1 parent d575353 commit 1d4e208
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/*! third party licenses: js/vendor.LICENSE.txt */
window.my_timezone={}.TZ;

Check warning on line 2 in __fixtures__/app_process_env/js/@nextcloud-vite-config-main.mjs

View check run for this annotation

Codecov / codecov/patch

__fixtures__/app_process_env/js/@nextcloud-vite-config-main.mjs#L2

Added line #L2 was not covered by tests

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions __fixtures__/app_process_env/main.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/**
* SPDX-FileCopyrightText: 2023 Ferdinand Thiessen <opensource@fthiessen.de>
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
window.my_timezone = process.env.TZ

Check warning on line 6 in __fixtures__/app_process_env/main.js

View check run for this annotation

Codecov / codecov/patch

__fixtures__/app_process_env/main.js#L2-L6

Added lines #L2 - L6 were not covered by tests
7 changes: 7 additions & 0 deletions __tests__/__snapshots__/appconfig.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -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;
"
`;
35 changes: 35 additions & 0 deletions __tests__/appconfig.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/**
* SPDX-FileCopyrightText: 2023 Ferdinand Thiessen <opensource@fthiessen.de>
*
* 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()
})
})
9 changes: 0 additions & 9 deletions lib/baseConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down

0 comments on commit 1d4e208

Please sign in to comment.