(nuxt) use dotenv closure to ensure that env variables on the server are set during production runtime

This commit is contained in:
Valentyne Stigloher 2024-08-14 15:29:29 +02:00
parent 8c554a38fd
commit 051784b5da
6 changed files with 45 additions and 40 deletions

View File

@ -1,5 +1,4 @@
import './src/dotenv.ts';
import dotenv from './server/dotenv.ts';
import { loadSuml } from './server/loader.ts';
import fs from 'node:fs';
import path from 'path';
@ -9,8 +8,6 @@ import replacePlugin from '@rollup/plugin-replace';
import yamlPlugin from '@rollup/plugin-yaml';
import { sentryVitePlugin } from '@sentry/vite-plugin';
import type { RouteMeta } from 'vue-router';
import { buildList } from './src/helpers.ts';
import buildLocaleList from './src/buildLocaleList.ts';
import type { Config } from './locale/config.ts';
import type { Translations } from './locale/translations.ts';
import mdPlugin from './plugins/rollup/md.ts';
@ -28,11 +25,12 @@ declare module 'nuxt/app' {
}
}
dotenv();
const config = loadSuml('config') as Config;
const translations = loadSuml('translations') as Translations;
const locale = config.locale;
const locales = buildLocaleList(locale);
const title = translations.title;
const description = translations.description;
const colour = '#C71585';
@ -49,26 +47,6 @@ const version = fs.existsSync(versionFile) ? fs.readFileSync(versionFile).toStri
const publicKeyFile = `${__dirname}/keys/public.pem`;
const publicKey = fs.existsSync(publicKeyFile) ? fs.readFileSync(publicKeyFile).toString('utf-8') : undefined;
const allLocalesUrls = buildList(function*() {
if (process.env.NODE_ENV === 'development') {
if (process.env.BASE_URL) {
yield process.env.BASE_URL;
}
yield 'http://pronouns.test:3000';
yield 'http://localhost:3000';
} else if (process.env.ENV === 'test') {
if (process.env.BASE_URL) {
yield process.env.BASE_URL;
}
} else {
yield 'https://pronouns.page';
for (const localeDescription of Object.values(locales)) {
yield localeDescription.url;
}
}
});
process.env.ALL_LOCALES_URLS = allLocalesUrls.join(',');
const esBuildOptions = {
supported: {
'top-level-await': true,
@ -166,7 +144,7 @@ export default defineNuxtConfig({
env: process.env.ENV,
baseUrl: process.env.BASE_URL,
homeUrl: process.env.HOME_URL || 'https://pronouns.page',
allLocalesUrls,
allLocalesUrls: process.env.ALL_LOCALES_URLS!.split(','),
version,
publicKey,
turnstileSiteKey: process.env.TURNSTILE_SITEKEY,

View File

@ -7,11 +7,7 @@ cd "$(dirname "$0")" || exit
nvm_bin=~/.nvm/versions/node/"$(<.nvmrc)"/bin
export PATH=$nvm_bin:$PATH
if [ "$1" = "start" ]; then
# load .env variables and start server
set -a
source .env
set +a
exec node .output/server/index.mjs
exec node .output/server/index.mjs -- "$(dirname "$0")" "$PORT"
else
bin_file=./node_modules/.bin/"$1"
if [ -f "$bin_file" ]; then

View File

@ -1,9 +1,37 @@
import dotenv from 'dotenv';
import buildLocaleList from '../src/buildLocaleList.ts';
import { buildList } from '../src/helpers.ts';
import type { Config } from '../locale/config.ts';
import { rootDir } from './paths.ts';
import { loadSuml } from './loader.ts';
const __dirname = new URL('.', import.meta.url).pathname;
export default () => {
dotenv.config({ path: `${rootDir}/.env` });
if (process.env.__INCLUDE) {
dotenv.config({ path: `${rootDir}/${process.env.__INCLUDE}` });
}
process.env.CLOUDFRONT = `https://${process.env.AWS_CLOUDFRONT_ID}.cloudfront.net`;
dotenv.config({ path: `${__dirname}/../.env` });
if (process.env.__INCLUDE) {
dotenv.config({ path: `${__dirname}/../${process.env.__INCLUDE}` });
}
process.env.CLOUDFRONT = `https://${process.env.AWS_CLOUDFRONT_ID}.cloudfront.net`;
const config = loadSuml('config') as Config;
const locales = buildLocaleList(config.locale);
const allLocalesUrls = buildList(function*() {
if (process.env.NODE_ENV === 'development') {
if (process.env.BASE_URL) {
yield process.env.BASE_URL;
}
yield 'http://pronouns.test:3000';
yield 'http://localhost:3000';
} else if (process.env.ENV === 'test') {
if (process.env.BASE_URL) {
yield process.env.BASE_URL;
}
} else {
yield 'https://pronouns.page';
for (const localeDescription of Object.values(locales)) {
yield localeDescription.url;
}
}
});
process.env.ALL_LOCALES_URLS = allLocalesUrls.join(',');
};

View File

@ -1,8 +1,11 @@
// load this module before other modules which require these global variables
import dotenv from './dotenv.ts';
import { loadSuml } from './loader.ts';
import type { Config } from '../locale/config.ts';
import type { Translations } from '../locale/translations.ts';
dotenv();
export const config = loadSuml('config') as Config;
global.config = config;
global.translations = loadSuml('translations') as Translations;

View File

@ -1,5 +1,3 @@
import './setup.ts';
import express from 'express';
import type { Request, Response, NextFunction } from 'express';
import { useBase } from 'h3';

View File

@ -1,6 +1,8 @@
import * as Sentry from '@sentry/node';
import '../src/dotenv.ts';
import dotenv from './dotenv.ts';
dotenv();
Sentry.init({
dsn: process.env.SENTRY_DSN,