Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Remove define of process.env from base config #34

Merged
merged 1 commit into from
Oct 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# SPDX-FileCopyrightText: 2023 Ferdinand Thiessen <opensource@fthiessen.de>
# SPDX-License-Identifier: AGPL-3.0-or-later

__fixtures__/*/js
coverage/
dist/
node_modules/
js/
2 changes: 1 addition & 1 deletion .reuse/dep5
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ Upstream-Name: nextcloud-vite-config
Upstream-Contact: Ferdinand Thiessen <rpm@fthiessen.de>
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 <rpm@fthiessen.de>
License: AGPL-3.0-or-later
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
Loading