Skip to content

Commit 32c3c4b

Browse files
committed
fix: process mutated env by plugins in dev server
1 parent 504e7f3 commit 32c3c4b

File tree

2 files changed

+62
-2
lines changed

2 files changed

+62
-2
lines changed

src/utils/run-build.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
import { promises as fs } from 'fs'
22
import path, { join } from 'path'
33

4+
import { NetlifyConfig } from '@netlify/build'
5+
46
import BaseCommand from '../commands/base-command.js'
57
import { $TSFixMe } from '../commands/types.js'
68
import { getBootstrapURL } from '../lib/edge-functions/bootstrap.js'
@@ -87,7 +89,7 @@ export const runNetlifyBuild = async ({
8789
edgeFunctionsBootstrapURL: await getBootstrapURL(),
8890
}
8991

90-
const devCommand = async (settingsOverrides = {}) => {
92+
const devCommand = async ({ netlifyConfig, settingsOverrides }: { netlifyConfig?: NetlifyConfig; settingsOverrides?: Partial<ServerSettings> } = {}) => {
9193
let cwd = command.workingDir
9294

9395
if (!options.cwd && command.project.workspace?.packages.length) {
@@ -99,6 +101,11 @@ export const runNetlifyBuild = async ({
99101
...settings,
100102
...settingsOverrides,
101103
...(options.skipWaitPort ? { skipWaitPort: true } : {}),
104+
env: {
105+
...settings.env,
106+
...settingsOverrides?.env,
107+
...netlifyConfig?.build.environment
108+
}
102109
},
103110
cwd,
104111
})
@@ -140,7 +147,7 @@ export const runNetlifyBuild = async ({
140147
if (!options.dir && netlifyConfig?.build?.publish) {
141148
settingsOverrides.dist = netlifyConfig.build.publish
142149
}
143-
await devCommand(settingsOverrides)
150+
await devCommand({ netlifyConfig, settingsOverrides })
144151

145152
return { configPath: tempConfigPath }
146153
}

tests/integration/commands/dev/dev.test.ts

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,59 @@ describe.concurrent('command/dev', () => {
730730
)
731731
})
732732
})
733+
734+
test('ensures dev server plugins can mutate env', async (t) => {
735+
await withSiteBuilder(t, async (builder) => {
736+
await builder
737+
.withNetlifyToml({
738+
config: {
739+
plugins: [{ package: './plugins/plugin' }],
740+
dev: {
741+
command: 'node index.mjs',
742+
targetPort: 4444
743+
},
744+
},
745+
})
746+
.withBuildPlugin({
747+
name: 'plugin',
748+
plugin: {
749+
async onPreDev({ netlifyConfig }) {
750+
netlifyConfig.build.environment.SOME_ENV = 'value'
751+
},
752+
},
753+
})
754+
.withContentFile({
755+
path: 'index.mjs',
756+
content: `
757+
import process from 'process';
758+
import http from 'http';
759+
760+
const server = http.createServer((req, res) => {
761+
res.write(process.env.SOME_ENV)
762+
res.end();
763+
})
764+
765+
server.listen(4444)
766+
`,
767+
})
768+
.build()
769+
770+
await withDevServer(
771+
{
772+
cwd: builder.directory,
773+
},
774+
async (server) => {
775+
const output = server.outputBuffer.map((buff) => buff.toString()).join('\n')
776+
t.expect(output).toContain('Netlify configuration property "build.environment.SOME_ENV" value changed.')
777+
t.expect(output).toContain('Server now ready')
778+
779+
const res = await fetch(new URL('/', server.url))
780+
t.expect(res.status).toBe(200)
781+
t.expect(await res.text()).toBe('value')
782+
},
783+
)
784+
})
785+
})
733786
})
734787
})
735788
})

0 commit comments

Comments
 (0)