Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support missing import.meta.env #413

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion packages/builder-vite/build.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export async function build(options: ExtendedOptions) {

const envsRaw = await presets.apply<Promise<EnvsRaw>>('env');
// Stringify env variables after getting `envPrefix` from the final config
const envs = stringifyProcessEnvs(envsRaw, finalConfig.envPrefix);
const envs = stringifyProcessEnvs(envsRaw, finalConfig.envPrefix, 'build');
// Update `define`
finalConfig.define = {
...finalConfig.define,
Expand Down
8 changes: 7 additions & 1 deletion packages/builder-vite/envs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export const allowedEnvPrefix = ['VITE_', 'STORYBOOK_'];
* Customized version of stringifyProcessEnvs from @storybook/core-common which
* uses import.meta.env instead of process.env and checks for allowed variables.
*/
export function stringifyProcessEnvs(raw: EnvsRaw, envPrefix: UserConfig['envPrefix']) {
export function stringifyProcessEnvs(raw: EnvsRaw, envPrefix: UserConfig['envPrefix'], command: 'build' | 'serve') {
const updatedRaw: EnvsRaw = {};
const envs = Object.entries(raw).reduce(
(acc: EnvsRaw, [key, value]) => {
Expand All @@ -46,5 +46,11 @@ export function stringifyProcessEnvs(raw: EnvsRaw, envPrefix: UserConfig['envPre
// const { foo } = import.meta.env;
envs['import.meta.env'] = JSON.stringify(stringifyEnvs(updatedRaw));

// Prevent build breaking on a missing var, similar to vite
// @see https://github.com/vitejs/vite/blob/908c9e4cdd2cceb0f01495e38066ffe33c21ddb8/packages/vite/src/node/plugins/define.ts#L50
if (command === 'build') {
envs['import.meta.env.'] = '({}).';
}

return envs;
}
2 changes: 1 addition & 1 deletion packages/builder-vite/vite-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ export async function createViteServer(options: ExtendedOptions, devServer: Serv

const envsRaw = await presets.apply<Promise<EnvsRaw>>('env');
// Stringify env variables after getting `envPrefix` from the final config
const envs = stringifyProcessEnvs(envsRaw, finalConfig.envPrefix);
const envs = stringifyProcessEnvs(envsRaw, finalConfig.envPrefix, 'serve');
// Update `define`
finalConfig.define = {
...finalConfig.define,
Expand Down