Unverified Commit 46e32ae1 authored by Balázs Orbán's avatar Balázs Orbán Committed by GitHub

feat: copy `.env` file in standalone mode (#34143)

Ref: #33897

I took a naive approach and simply added `.env` to the files that need to be copied.

Do we want to include `.env.production` as well? Ref: https://nextjs.org/docs/basic-features/environment-variables#loading-environment-variables

(I haven't tested what happens if the file does not exist on copy.)

Other files like `.env.*local` or `.env.{test,development}` don't make sense to copy.

## Bug

- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Errors have helpful link attached, see `contributing.md`

## Feature

- [ ] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR.
- [ ] Related issues linked using `fixes #number`
- [ ] Integration tests added
- [ ] Documentation added
- [ ] Telemetry added. In case of a feature if it's used or not.
- [ ] Errors have helpful link attached, see `contributing.md`

## Documentation / Examples

- [ ] Make sure the linting passes by running `yarn lint`
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent 8dfaaf55
...@@ -45,7 +45,7 @@ const nextBabelLoaderOuter = function nextBabelLoaderOuter( ...@@ -45,7 +45,7 @@ const nextBabelLoaderOuter = function nextBabelLoaderOuter(
nextBabelLoader.call(this, loaderSpan, inputSource, inputSourceMap) nextBabelLoader.call(this, loaderSpan, inputSource, inputSourceMap)
) )
.then( .then(
([transformedSource, outputSourceMap]) => ([transformedSource, outputSourceMap]: any) =>
callback?.(null, transformedSource, outputSourceMap || inputSourceMap), callback?.(null, transformedSource, outputSourceMap || inputSourceMap),
(err) => { (err) => {
callback?.(err) callback?.(err)
......
...@@ -427,7 +427,7 @@ export default async function build( ...@@ -427,7 +427,7 @@ export default async function build(
pages404: boolean pages404: boolean
basePath: string basePath: string
redirects: Array<ReturnType<typeof buildCustomRoute>> redirects: Array<ReturnType<typeof buildCustomRoute>>
rewrites: rewrites?:
| Array<ReturnType<typeof buildCustomRoute>> | Array<ReturnType<typeof buildCustomRoute>>
| { | {
beforeFiles: Array<ReturnType<typeof buildCustomRoute>> beforeFiles: Array<ReturnType<typeof buildCustomRoute>>
...@@ -2036,6 +2036,12 @@ export default async function build( ...@@ -2036,6 +2036,12 @@ export default async function build(
for (const file of [ for (const file of [
...requiredServerFiles.files, ...requiredServerFiles.files,
path.join(config.distDir, SERVER_FILES_MANIFEST), path.join(config.distDir, SERVER_FILES_MANIFEST),
...loadedEnvFiles.reduce<string[]>((acc, envFile) => {
if (['.env', '.env.production'].includes(envFile.path)) {
acc.push(envFile.path)
}
return acc
}, []),
]) { ]) {
const filePath = path.join(dir, file) const filePath = path.join(dir, file)
await promises.copyFile( await promises.copyFile(
......
...@@ -28,7 +28,7 @@ function generateClientManifest( ...@@ -28,7 +28,7 @@ function generateClientManifest(
compilation: any, compilation: any,
assetMap: BuildManifest, assetMap: BuildManifest,
rewrites: CustomRoutes['rewrites'] rewrites: CustomRoutes['rewrites']
): string { ): string | undefined {
const compilationSpan = spans.get(compilation) || spans.get(compiler) const compilationSpan = spans.get(compilation) || spans.get(compiler)
const genClientManifestSpan = compilationSpan?.traceChild( const genClientManifestSpan = compilationSpan?.traceChild(
'NextJsBuildManifest-generateClientManifest' 'NextJsBuildManifest-generateClientManifest'
......
...@@ -85,7 +85,7 @@ export class Span { ...@@ -85,7 +85,7 @@ export class Span {
this.attrs[key] = String(value) this.attrs[key] = String(value)
} }
traceFn(fn: any) { traceFn<T>(fn: () => T): T {
try { try {
return fn() return fn()
} finally { } finally {
......
...@@ -31,6 +31,13 @@ describe('should set-up next', () => { ...@@ -31,6 +31,13 @@ describe('should set-up next', () => {
'data.txt': new FileRef( 'data.txt': new FileRef(
join(__dirname, 'required-server-files/data.txt') join(__dirname, 'required-server-files/data.txt')
), ),
'.env': new FileRef(join(__dirname, 'required-server-files/.env')),
'.env.local': new FileRef(
join(__dirname, 'required-server-files/.env.local')
),
'.env.production': new FileRef(
join(__dirname, 'required-server-files/.env.production')
),
}, },
nextConfig: { nextConfig: {
eslint: { eslint: {
...@@ -722,4 +729,14 @@ describe('should set-up next', () => { ...@@ -722,4 +729,14 @@ describe('should set-up next', () => {
const $ = cheerio.load(html) const $ = cheerio.load(html)
expect($('#slug-page').text()).toBe('[slug] page') expect($('#slug-page').text()).toBe('[slug] page')
}) })
it('should copy and read .env file', async () => {
const res = await fetchViaHTTP(appPort, '/api/env')
const envVariables = await res.json()
expect(envVariables.env).not.toBeUndefined()
expect(envVariables.envProd).not.toBeUndefined()
expect(envVariables.envLocal).toBeUndefined()
})
}) })
FOO=bar
\ No newline at end of file
LOCAL_SECRET=local-secret
\ No newline at end of file
PROD_SECRET=prod-secret
\ No newline at end of file
export default function handler(_, res) {
res.json({
env: process.env.FOO,
envLocal: process.env.LOCAL_SECRET,
envProd: process.env.PROD_SECRET,
})
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment