Skip to content
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
6 changes: 5 additions & 1 deletion src/configure.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ describe('configureRuntimeEnv()', () => {

it('should call the helper methods with options', () => {
configureRuntimeEnv({
rootdirectory: 'rootdirectory/',
subdirectory: 'subdirectory/',
});

Expand All @@ -51,7 +52,10 @@ describe('configureRuntimeEnv()', () => {
{
NEXT_PUBLIC_FOO: 'foo',
},
'subdirectory/',
{
rootdirectory: 'rootdirectory/',
subdirectory: 'subdirectory/',
},
);
});
});
7 changes: 6 additions & 1 deletion src/configure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,11 @@ import { getPublicEnv } from './helpers/get-public-env';
import { writeBrowserEnv } from './helpers/write-browser-env';

export type ConfigureRuntimeEnvOptions = {
/**
* The rootdirectory of `/public` where the `__ENV.js` file should be written
* eg. `rootdirectory/`to.
*/
rootdirectory?: string;
/**
* The subdirectory of `/public` where the `__ENV.js` file should be written
* eg. `subdirectory/`to.
Expand All @@ -25,5 +30,5 @@ export type ConfigureRuntimeEnvOptions = {
export function configureRuntimeEnv(options?: ConfigureRuntimeEnvOptions) {
const publicEnv = getPublicEnv();

writeBrowserEnv(publicEnv, options?.subdirectory);
writeBrowserEnv(publicEnv, options);
}
19 changes: 18 additions & 1 deletion src/helpers/write-browser-env.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,30 @@ describe('writeBrowserEnv()', () => {
fs.rmSync(file);
});

it('should write to a rootdirectory', () => {
const fileInRootdirectory = `${base}/rootdirectory/public/__ENV.js`;
const messageWithRootdirectory = `- ${chalk.green(
`ready`,
)} [next-runtime-env] wrote browser runtime environment variables to '${fileInRootdirectory}'.`;

writeBrowserEnv({}, { rootdirectory: 'rootdirectory/' });

expect(infoSpy).toHaveBeenCalledWith(messageWithRootdirectory);

const content = fs.readFileSync(fileInRootdirectory).toString();

expect(content).toEqual('window.__ENV = {};');

fs.rmSync(fileInRootdirectory);
});

it('should write to a subdirectory', () => {
const fileInSubdirectory = `${path}/subdirectory/__ENV.js`;
const messageWithSubdirectory = `- ${chalk.green(
`ready`,
)} [next-runtime-env] wrote browser runtime environment variables to '${fileInSubdirectory}'.`;

writeBrowserEnv({}, 'subdirectory/');
writeBrowserEnv({}, { subdirectory: 'subdirectory/' });

expect(infoSpy).toHaveBeenCalledWith(messageWithSubdirectory);

Expand Down
8 changes: 6 additions & 2 deletions src/helpers/write-browser-env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@ import path from 'path';
import { type ProcessEnv } from '../typings/process-env';
import * as log from '../utils/log';
import { getBrowserEnvScript } from './get-browser-env-script';
import { ConfigureRuntimeEnvOptions } from '../configure';

/**
* Writes the environment variables to the public __ENV.js file and make them
* accessible under `window.__ENV`.
*/
export function writeBrowserEnv(env: ProcessEnv, subdirectory = '') {
export function writeBrowserEnv(
env: ProcessEnv,
options?: ConfigureRuntimeEnvOptions,
) {
const base = fs.realpathSync(process.cwd());
const file = `${base}/public/${subdirectory}__ENV.js`;
const file = `${base}/${options?.rootdirectory ?? ''}public/${options?.subdirectory ?? ''}__ENV.js`;

const content = getBrowserEnvScript(env);

Expand Down
1 change: 1 addition & 0 deletions undefinedpublic/subdirectory/__ENV.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.__ENV = {};
1 change: 1 addition & 0 deletions undefinedpublic/undefined__ENV.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
window.__ENV = {"NEXT_PUBLIC_FOO":"foo","NEXT_PUBLIC_BAR":"bar","NEXT_PUBLIC_BAZ":"baz"};