Skip to content

Commit 002754d

Browse files
authored
Turbopack: Fix setting existing env var in webpack loader (vercel#78962)
## What? We use a proxy around `process.env`, the setter was not implemented and this caused some issue in Node.js when it checks for if `process.env` is writeable. This fixes that case. Full report in vercel#78804. Fixes vercel#78804 Fixes PACK-4550 <!-- Thanks for opening a PR! Your contribution is much appreciated. To make sure your PR is handled as smoothly as possible we request that you follow the checklist sections below. Choose the right checklist for the change(s) that you're making: ## For Contributors ### Improving Documentation - Run `pnpm prettier-fix` to fix formatting issues before opening the PR. - Read the Docs Contribution Guide to ensure your contribution follows the docs guidelines: https://nextjs.org/docs/community/contribution-guide ### Adding or Updating Examples - The "examples guidelines" are followed from our contributing doc https://github.com/vercel/next.js/blob/canary/contributing/examples/adding-examples.md - Make sure the linting passes by running `pnpm build && pnpm lint`. See https://github.com/vercel/next.js/blob/canary/contributing/repository/linting.md ### Fixing a bug - Related issues linked using `fixes #number` - Tests added. See: https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ### Adding a feature - Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. (A discussion must be opened, see https://github.com/vercel/next.js/discussions/new?category=ideas) - Related issues/discussions are linked using `fixes #number` - e2e tests added (https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) - Documentation added - Telemetry added. In case of a feature if it's used or not. - Errors have a helpful link attached, see https://github.com/vercel/next.js/blob/canary/contributing.md ## For Maintainers - Minimal description (aim for explaining to someone not on the team to understand the PR) - When linking to a Slack thread, you might want to share details of the conclusion - Link both the Linear (Fixes NEXT-xxx) and the GitHub issues - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Closes NEXT- Fixes # -->
1 parent 8e4568a commit 002754d

File tree

7 files changed

+68
-0
lines changed

7 files changed

+68
-0
lines changed
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import { ReactNode } from 'react'
2+
export default function Root({ children }: { children: ReactNode }) {
3+
return (
4+
<html>
5+
<body>{children}</body>
6+
</html>
7+
)
8+
}
Lines changed: 1 addition & 0 deletions
Loading
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import Svg from './next.svg'
2+
export default function Page() {
3+
return (
4+
<>
5+
<p>hello world</p>
6+
<div id="the-svg">
7+
<Svg />
8+
</div>
9+
</>
10+
)
11+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
module.exports = (_source) => {
2+
process.env.TEST_THIS_THING = 'def'
3+
return 'export default () => "The svg rendered"'
4+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
const { join } = require('path')
2+
3+
/** @type {import('next').NextConfig} */
4+
const nextConfig = {
5+
reactStrictMode: true,
6+
turbopack: {
7+
rules: {
8+
'*.svg': {
9+
as: '*.js',
10+
loaders: [join(__dirname, './custom-loader.js')],
11+
},
12+
},
13+
},
14+
webpack(config) {
15+
config.module.rules.push({
16+
test: /\.svg$/,
17+
use: [join(__dirname, './custom-loader.js')],
18+
})
19+
20+
return config
21+
},
22+
}
23+
24+
module.exports = nextConfig
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { nextTestSetup } from 'e2e-utils'
2+
3+
describe('webpack-loader-set-environment-variable', () => {
4+
const { next } = nextTestSetup({
5+
files: __dirname,
6+
env: {
7+
TEST_THIS_THING: 'def',
8+
},
9+
})
10+
11+
// Recommended for tests that check HTML. Cheerio is a HTML parser that has a jQuery like API.
12+
it('loader that sets an environment variable should work', async () => {
13+
const $ = await next.render$('/')
14+
expect($('p').text()).toBe('hello world')
15+
expect($('#the-svg').text()).toBe('The svg rendered')
16+
})
17+
})

turbopack/crates/turbopack-node/js/src/transforms/transforms.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,9 @@ process.env = new Proxy(originalEnv, {
6767
}
6868
return Reflect.get(target, prop)
6969
},
70+
set(target, prop, value) {
71+
return Reflect.set(target, prop, value)
72+
},
7073
})
7174

7275
export function getReadEnvVariables(): string[] {

0 commit comments

Comments
 (0)