Skip to content

Commit

Permalink
Merge pull request #34 from nextcloud-libraries/fix/remove-process-en…
Browse files Browse the repository at this point in the history
…v-defines
  • Loading branch information
skjnldsv authored Oct 4, 2023
2 parents d575353 + 55de856 commit 28a71a8
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 10 deletions.
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 <[email protected]>
# 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 <[email protected]>
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 <[email protected]>
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 <[email protected]>
*
* SPDX-License-Identifier: AGPL-3.0-or-later
*/
window.my_timezone = process.env.TZ
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 <[email protected]>
*
* 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 28a71a8

Please sign in to comment.