Skip to content

Commit 2aa0f5c

Browse files
authored
feat: use Vite for env variable loading (#9)
+ Static variables are injected at build time by Vite. + dotenv and lodash are no longer required.
1 parent 7679da3 commit 2aa0f5c

File tree

6 files changed

+5
-53
lines changed

6 files changed

+5
-53
lines changed

README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,9 @@ Values can be added to the `.env` file or defined as environment
110110
variables. If environment variables are defined they will overwrite the values
111111
in the .env file.
112112

113+
See [$env/static/private](https://kit.svelte.dev/docs/modules#$env-static-private)
114+
in the SvelteKit documentation for further details.
115+
113116
## Destroy Command
114117

115118
A script is provided to destroy the infrastructure, with the following

package.json

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@
4141
},
4242
"devDependencies": {
4343
"@types/folder-hash": "^4.0.4",
44-
"@types/lodash": "^4.17.1",
4544
"@types/mime-types": "^2.1.4",
4645
"@types/minimist": "^1.2.5",
4746
"@types/node": "20.12.12",
@@ -57,8 +56,6 @@
5756
"@aws-sdk/client-cloudfront": "^3.577.0",
5857
"@pulumi/aws": "^6.36.0",
5958
"@pulumi/pulumi": "^3.116.1",
60-
"dotenv": "^16.4.5",
61-
"lodash": "^4.17.21",
6259
"mime-types": "^2.1.35",
6360
"sveltekit-adapter-aws-base": "^3.0.1",
6461
"yargs": "^17.7.2"

stacks/server/index.ts

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,8 @@
11
import * as pulumi from '@pulumi/pulumi'
22

33
import { getLambdaRole, buildLambda } from './resources.js'
4-
import { getEnvironment } from '../utils.js'
54

65
const pulumiConfig = new pulumi.Config()
7-
const projectPath = pulumiConfig.require('projectPath')
86
const serverPath = pulumiConfig.require('serverPath')
97
const optionsPath = pulumiConfig.require('optionsPath')
108
const memorySizeStr = pulumiConfig.require('memorySize')
@@ -24,13 +22,12 @@ if (!serverInvokeMode) {
2422
}
2523

2624
const iamForLambda = getLambdaRole()
27-
const environment = getEnvironment(projectPath)
2825

2926
const serverURL = buildLambda(
3027
'LambdaServer',
3128
iamForLambda,
3229
serverPath,
33-
environment.parsed,
30+
undefined,
3431
memorySize,
3532
serverInvokeMode,
3633
)

stacks/utils.ts

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,3 @@
1-
import * as path from 'path'
2-
3-
import { config, DotenvConfigOutput } from 'dotenv'
4-
import { assign, keys, pick } from 'lodash'
5-
6-
export function getEnvironment(projectPath: string): DotenvConfigOutput {
7-
const dotenv = config({ path: path.join(projectPath, '.env') })
8-
const parsed = assign(
9-
{},
10-
dotenv.parsed,
11-
pick(process.env, keys(dotenv.parsed))
12-
)
13-
return { parsed: parsed } as DotenvConfigOutput
14-
}
15-
161
export class NameRegister {
172
private static singleton: NameRegister
183
private _names: string[] = []

tests/stacks.server.index.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ describe('stacks/server/index.ts', () => {
6868
expect(resources.getLambdaRole).toHaveBeenCalledTimes(1)
6969
expect(resources.buildLambda).toHaveBeenCalledTimes(2)
7070

71-
expect(mockBuildLambda.mock.calls[0][3]).toStrictEqual({ MOCK: '' })
71+
expect(mockBuildLambda.mock.calls[0][3]).toStrictEqual(undefined)
7272
expect(mockBuildLambda.mock.calls[0][4]).toStrictEqual(256)
7373
expect(mockBuildLambda.mock.calls[1][3]).toStrictEqual({
7474
ALLOWED_ORIGINS: '[example.com]',

tests/stacks.utils.test.ts

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,3 @@
1-
import * as fs from 'fs'
2-
import * as path from 'path'
3-
4-
import { getTempDir } from './utils.js'
5-
61
describe('stacks/utils.ts', () => {
72
let envOrig: string
83
let utils: typeof import('../stacks/utils.js')
@@ -17,31 +12,6 @@ describe('stacks/utils.ts', () => {
1712
process.env = JSON.parse(envOrig)
1813
})
1914

20-
it('getEnvironment (without process.env)', () => {
21-
const tmpDir = getTempDir()
22-
const data = 'MOCK=mymock'
23-
24-
fs.writeFileSync(path.join(tmpDir, '.env'), data)
25-
26-
const environment = utils.getEnvironment(tmpDir)
27-
expect(environment.parsed).toEqual({ MOCK: 'mymock' })
28-
29-
fs.rmSync(tmpDir, { recursive: true, force: true })
30-
})
31-
32-
it('getEnvironment (with process.env)', () => {
33-
process.env['MOCK'] = 'anothermock'
34-
const tmpDir = getTempDir()
35-
const data = 'MOCK=mymock'
36-
37-
fs.writeFileSync(path.join(tmpDir, '.env'), data)
38-
39-
const environment = utils.getEnvironment(tmpDir)
40-
expect(environment.parsed).toEqual({ MOCK: 'anothermock' })
41-
42-
fs.rmSync(tmpDir, { recursive: true, force: true })
43-
})
44-
4515
it('NameRegister.registerName', () => {
4616
const nameRegister = utils.NameRegister.getInstance()
4717
const expected = 'a'

0 commit comments

Comments
 (0)