Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(env): server/public variables (#10881)
Co-authored-by: Bjorn Lu <bjornlu.dev@gmail.com> Co-authored-by: Emanuele Stoppa <my.burning@gmail.com>
- Loading branch information
1 parent
eec5403
commit 87ea8ab
Showing
10 changed files
with
125 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
12 changes: 12 additions & 0 deletions
12
packages/astro/test/fixtures/astro-env-server-fail/astro.config.mjs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { defineConfig, envField } from 'astro/config'; | ||
|
||
// https://astro.build/config | ||
export default defineConfig({ | ||
experimental: { | ||
env: { | ||
schema: { | ||
PUBLIC_FOO: envField.string({ context: "server", access: "public", optional: true, default: "ABC" }), | ||
} | ||
} | ||
} | ||
}); |
8 changes: 8 additions & 0 deletions
8
packages/astro/test/fixtures/astro-env-server-fail/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
{ | ||
"name": "@test/astro-env-server-fail", | ||
"version": "0.0.0", | ||
"private": true, | ||
"dependencies": { | ||
"astro": "workspace:*" | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/astro-env-server-fail/src/pages/index.astro
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
<script> | ||
import { PUBLIC_FOO } from "astro:env/server" | ||
</script> |
3 changes: 3 additions & 0 deletions
3
packages/astro/test/fixtures/astro-env-server-fail/tsconfig.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
{ | ||
"extends": "astro/tsconfigs/base" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
import assert from 'node:assert/strict'; | ||
import { before, describe, it } from 'node:test'; | ||
import { loadFixture } from '../../test-utils.js'; | ||
import { EnvServerOnlyModule } from '../../../dist/core/errors/errors-data.js'; | ||
import { AstroError } from '../../../dist/core/errors/errors.js'; | ||
|
||
describe('astro:env public variables', () => { | ||
/** @type {Awaited<ReturnType<typeof loadFixture>>} */ | ||
let fixture; | ||
|
||
describe('Client variables', () => { | ||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/astro-env/', | ||
}); | ||
await fixture.build(); | ||
}); | ||
|
||
it('builds without throwing', async () => { | ||
assert.equal(true, true); | ||
}); | ||
|
||
it('includes client/public env in build', async () => { | ||
let indexHtml = await fixture.readFile('/index.html'); | ||
|
||
assert.equal(indexHtml.includes('ABC'), true); | ||
assert.equal(indexHtml.includes('DEF'), true); | ||
}); | ||
|
||
it('does not include server/public env in build', async () => { | ||
let indexHtml = await fixture.readFile('/index.html'); | ||
|
||
assert.equal(indexHtml.includes('GHI'), false); | ||
}); | ||
}); | ||
|
||
describe('Server variables', () => { | ||
before(async () => { | ||
fixture = await loadFixture({ | ||
root: './fixtures/astro-env-server-fail/', | ||
}); | ||
}); | ||
|
||
it('throws if server module is called on the client', async () => { | ||
const error = await fixture.build().catch(err => err); | ||
assert.equal(error instanceof AstroError, true) | ||
assert.equal(error.name, EnvServerOnlyModule.name); | ||
}); | ||
}); | ||
}); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.