mirror of
https://github.com/terribleplan/next.js.git
synced 2024-01-19 02:48:18 +00:00
Add passEnv config option
This commit is contained in:
parent
0e4a89934c
commit
e077ab869f
8
packages/next-server/lib/pass-env.ts
Normal file
8
packages/next-server/lib/pass-env.ts
Normal file
|
@ -0,0 +1,8 @@
|
|||
export default function passEnv() {
|
||||
let passEnv: { [x: string]: string | undefined } | undefined
|
||||
if (process.env.__NEXT_ENV_PASS) process.env.__NEXT_ENV_PASS.split(',').forEach((key) => {
|
||||
passEnv ? passEnv[key] = process.env[key] : passEnv = {[key]: process.env[key]}
|
||||
})
|
||||
|
||||
return passEnv
|
||||
}
|
|
@ -9,6 +9,7 @@ import Loadable from '../lib/loadable'
|
|||
import LoadableCapture from '../lib/loadable-capture'
|
||||
import {getDynamicImportBundles, Manifest as ReactLoadableManifest, ManifestItem} from './get-dynamic-import-bundles'
|
||||
import {getPageFiles, BuildManifest} from './get-page-files'
|
||||
import passEnv from '../lib/pass-env'
|
||||
|
||||
type Enhancer = (Component: React.ComponentType) => React.ComponentType
|
||||
type ComponentsEnhancer = {enhanceApp?: Enhancer, enhanceComponent?: Enhancer}|Enhancer
|
||||
|
@ -95,6 +96,7 @@ function renderDocument(Document: React.ComponentType, {
|
|||
buildId, // buildId is used to facilitate caching of page bundles, we send it to the client so that pageloader knows where to load bundles
|
||||
assetPrefix: assetPrefix === '' ? undefined : assetPrefix, // send assetPrefix to the client side when configured, otherwise don't sent in the resulting HTML
|
||||
runtimeConfig, // runtimeConfig if provided, otherwise don't sent in the resulting HTML
|
||||
passEnv: passEnv(), // when setting config option passEnv
|
||||
nextExport, // If this is a page exported by `next export`
|
||||
dynamicIds: dynamicImportsIds.length === 0 ? undefined : dynamicImportsIds,
|
||||
err: (err) ? serializeError(dev, err) : undefined, // Error if one happened, otherwise don't sent in the resulting HTML
|
||||
|
|
|
@ -302,6 +302,7 @@ export default async function getBaseWebpackConfig (dir, {dev = false, isServer
|
|||
}
|
||||
}, {}) : {},
|
||||
{
|
||||
'process.env.__NEXT_ENV_PASS': JSON.stringify(config.passEnv.join(',')),
|
||||
'process.crossOrigin': JSON.stringify(config.crossOrigin),
|
||||
'process.browser': JSON.stringify(!isServer)
|
||||
}
|
||||
|
|
|
@ -21,6 +21,15 @@ if (!window.Promise) {
|
|||
|
||||
const data = JSON.parse(document.getElementById('__NEXT_DATA__').textContent)
|
||||
window.__NEXT_DATA__ = data
|
||||
if (data && typeof data.passEnv === 'object') {
|
||||
Object.keys(data.passEnv).forEach((key) => {
|
||||
if (typeof global.process === 'undefined') {
|
||||
global.process = {env: {}}
|
||||
}
|
||||
|
||||
process.env[key] = data.passEnv[key]
|
||||
})
|
||||
}
|
||||
|
||||
const {
|
||||
props,
|
||||
|
|
Loading…
Reference in a new issue