-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
42 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
10 changes: 0 additions & 10 deletions
10
...platform-shared/src/node/project/file-presets/files/app/core/setPublicRuntimeConfig.js.ts
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 36 additions & 11 deletions
47
packages/platform-shared/src/shared/shuvi-singleton-runtimeConfig.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,53 @@ | ||
import { IRuntimeConfig } from './runtimeConfigTypes'; | ||
|
||
let runtimeConfig: IRuntimeConfig | null; | ||
let publicRuntimeConfig: IRuntimeConfig | null; | ||
const KEY_SERVER_RUNTIME_CONFIG = Symbol.for('shuvi_server_runtime_config'); | ||
const KEY_PUBLIC_RUNTIME_CONFIG = Symbol.for('shuvi_client_runtime_config'); | ||
|
||
const isServer = typeof window === 'undefined'; | ||
|
||
let publicRuntimeConfig: IRuntimeConfig | undefined | null; | ||
let serverRuntimeConfig: IRuntimeConfig | undefined | null; | ||
|
||
/** | ||
* getRuntimeConfig function | ||
* | ||
* @returns runtimeConfig | ||
* @returns serverRuntimeConfig | ||
*/ | ||
export function getRuntimeConfig() { | ||
return { | ||
...(runtimeConfig || {}), | ||
...(publicRuntimeConfig || {}) | ||
...(getServerRuntimeConfig() || {}), | ||
...(getPublicRuntimeConfig() || {}) | ||
}; | ||
} | ||
|
||
export function getPublicRuntimeConfig(): IRuntimeConfig | null { | ||
return publicRuntimeConfig; | ||
export function getPublicRuntimeConfig(): IRuntimeConfig | undefined | null { | ||
if (isServer) { | ||
return (globalThis as any)[KEY_PUBLIC_RUNTIME_CONFIG]; | ||
} else { | ||
return publicRuntimeConfig; | ||
} | ||
} | ||
|
||
export function setPublicRuntimeConfig(config: IRuntimeConfig | null) { | ||
if (isServer) { | ||
(globalThis as any)[KEY_PUBLIC_RUNTIME_CONFIG] = config; | ||
} else { | ||
publicRuntimeConfig = config; | ||
} | ||
} | ||
|
||
export function setRuntimeConfig(config: IRuntimeConfig) { | ||
runtimeConfig = config; | ||
export function getServerRuntimeConfig(): IRuntimeConfig | undefined | null { | ||
if (isServer) { | ||
return (globalThis as any)[KEY_SERVER_RUNTIME_CONFIG]; | ||
} else { | ||
return serverRuntimeConfig; | ||
} | ||
} | ||
|
||
export function setPublicRuntimeConfig(config: IRuntimeConfig) { | ||
publicRuntimeConfig = config; | ||
export function setServerRuntimeConfig(config: IRuntimeConfig | null) { | ||
if (isServer) { | ||
(globalThis as any)[KEY_SERVER_RUNTIME_CONFIG] = config; | ||
} else { | ||
serverRuntimeConfig = config; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters