Skip to content

Commit

Permalink
feat: bundle runtimeConfig
Browse files Browse the repository at this point in the history
  • Loading branch information
liximomo committed Sep 13, 2022
1 parent a0aba39 commit aafba7d
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 40 deletions.
5 changes: 0 additions & 5 deletions packages/platform-shared/shuvi-type-extensions-runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,11 +35,6 @@ declare module '@shuvi/app/core/setRuntimeConfig' {
export default function setRuntimeConfig(config: IRuntimeConfig): void;
}

declare module '@shuvi/app/core/setPublicRuntimeConfig' {
import { IRuntimeConfig } from '@shuvi/platform-shared/shared';
export default function setPublicRuntimeConfig(config: IRuntimeConfig): void;
}

declare module '@shuvi/app/user/error' {
const Error: any;
export default Error;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,6 @@ const core = createPlugin({
},
addRuntimeService: () => [
{
// must be export separately, we need the module path to always be the
// same as what we've defined in
// "packages/toolpack/src/webpack/config/parts/external.ts"
source: resolvePkgFile('lib/shared/shuvi-singleton-runtimeConfig'),
exported: '{ getRuntimeConfig }'
},
Expand Down
11 changes: 4 additions & 7 deletions packages/platform-shared/src/node/platform/runtimeFiles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { IPluginContext } from '@shuvi/service';
import { fileUtils } from '@shuvi/service/lib/project';
import {
setRuntimeConfig,
setServerRuntimeConfig,
setPublicRuntimeConfig
} from '../../shared/shuvi-singleton-runtimeConfig';
import { createProjectContext } from '../project/projectContext';
Expand Down Expand Up @@ -42,12 +42,9 @@ export const getPresetRuntimeFilesCreator =
break;
}
}
if (serverKeys) {
setRuntimeConfig(serverRuntimeConfig);
}
if (publicKeys) {
setPublicRuntimeConfig(publicRuntimeConfig);
}

setServerRuntimeConfig(serverKeys ? serverRuntimeConfig : null);
setPublicRuntimeConfig(publicKeys ? publicRuntimeConfig : null);

const context = createProjectContext();
context.userModule = {
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { resolvePkgFile } from '../../../../../paths';
export default () =>
defineFile({
content: () =>
`export { setRuntimeConfig as default } from '${resolvePkgFile(
`export { setPublicRuntimeConfig as default } from '${resolvePkgFile(
'lib/shared/shuvi-singleton-runtimeConfig'
)}'`
});
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;
}
}
4 changes: 1 addition & 3 deletions packages/toolpack/src/webpack/config/parts/external.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,7 @@ export function nodeExternals({
}

const notExternalModules: Test[] = [];
const externalModules: Test[] = [
/platform-shared[/\\](esm|lib)[/\\]shared[/\\]shuvi-singleton-runtimeConfig/
];
const externalModules: Test[] = [];

// make sure we don't externalize anything that is
// supposed to be transpiled
Expand Down

0 comments on commit aafba7d

Please sign in to comment.