Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
11 changes: 9 additions & 2 deletions src/utils/run-build.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { promises as fs } from 'fs'
import path, { join } from 'path'

import { NetlifyConfig } from '@netlify/build'

import BaseCommand from '../commands/base-command.js'
import { $TSFixMe } from '../commands/types.js'
import { getBootstrapURL } from '../lib/edge-functions/bootstrap.js'
Expand Down Expand Up @@ -87,7 +89,7 @@ export const runNetlifyBuild = async ({
edgeFunctionsBootstrapURL: await getBootstrapURL(),
}

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

if (!options.cwd && command.project.workspace?.packages.length) {
Expand All @@ -99,6 +101,11 @@ export const runNetlifyBuild = async ({
...settings,
...settingsOverrides,
...(options.skipWaitPort ? { skipWaitPort: true } : {}),
env: {
...settings.env,
...settingsOverrides?.env,
...netlifyConfig?.build.environment
}
},
cwd,
})
Expand Down Expand Up @@ -140,7 +147,7 @@ export const runNetlifyBuild = async ({
if (!options.dir && netlifyConfig?.build?.publish) {
settingsOverrides.dist = netlifyConfig.build.publish
}
await devCommand(settingsOverrides)
await devCommand({ netlifyConfig, settingsOverrides })

return { configPath: tempConfigPath }
}
Expand Down
53 changes: 53 additions & 0 deletions tests/integration/commands/dev/dev.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -730,6 +730,59 @@ describe.concurrent('command/dev', () => {
)
})
})

test('ensures dev server plugins can mutate env', async (t) => {
await withSiteBuilder(t, async (builder) => {
await builder
.withNetlifyToml({
config: {
plugins: [{ package: './plugins/plugin' }],
dev: {
command: 'node index.mjs',
targetPort: 4444
},
},
})
.withBuildPlugin({
name: 'plugin',
plugin: {
async onPreDev({ netlifyConfig }) {
netlifyConfig.build.environment.SOME_ENV = 'value'
},
},
})
.withContentFile({
path: 'index.mjs',
content: `
import process from 'process';
import http from 'http';

const server = http.createServer((req, res) => {
res.write(process.env.SOME_ENV)
res.end();
})

server.listen(4444)
`,
})
.build()

await withDevServer(
{
cwd: builder.directory,
},
async (server) => {
const output = server.outputBuffer.map((buff) => buff.toString()).join('\n')
t.expect(output).toContain('Netlify configuration property "build.environment.SOME_ENV" value changed.')
t.expect(output).toContain('Server now ready')

const res = await fetch(new URL('/', server.url))
t.expect(res.status).toBe(200)
t.expect(await res.text()).toBe('value')
},
)
})
})
})
})
})
Loading