astro-build-time-constants
Version:
Astro integration to define build-time constants: dates, custom properties,...
99 lines (78 loc) • 2.21 kB
Markdown
# astro-build-time-constants
This Astro integration generates the file ```src/astro-build-time-constants.ts```,
which contains constants related to build time, typically:
```ts
export const digits2 = (number: number) => number < 10 ? '0' + number : '' + number
export const astroBuildTimeConstants = {
internal: {
epoch: 1758444256,
seconds: 15,
minutes: 44,
hours: 10,
fullYear: 2025,
month: 9,
date: 21,
iso: "2025-09-21T08:44:15.899Z",
},
custom: {
"myParam": "myValue",
"myObject": {
"myValue": 10
}
},
}
```
Internal objects contains built-in constants, such as the build date.
Custom object is the object passed as a parameter of ```buildTimeConstants()```
when initializing the integration in ```astro.config.mjs```. This ease usage
of custom configuration parameters
Usage in an astro components is then typically
```jsx
import { astroBuildTimeConstants } from '../astro-build-time-constants'
<p>
Built on {astroBuildTimeConstants.internal.iso}
</p>
<p>
My parameter is {astroBuildTimeConstants.custom.myParam}
</p>
```
## Installation
### Quick install
To install astro-build-time-constants, run the following from your project directory and follow the prompts:
```bash
npx astro add astro-build-time-constants
```
### Manual install
First, install the astro-build-time-constants package using your package manager. If you're using npm, run this in the terminal:
```bash
npm install astro-build-time-constants
```
Then, apply this integration to your ```astro.config.mjs``` file using the integrations property:
```js
import { defineConfig } from 'astro/config';
import buildTimeConstants from 'astro-build-time-constants'
export default defineConfig({
integrations: [
buildTimeConstants()
],
});
```
# Adding custom properties
Custom properties can be added as the ```buildTimeConstants()``` arguments, such as
```js
export default defineConfig({
...
integrations: [
...
buildTimeConstants( {
myParam: "myValue",
myObject: {
myValue: 10,
}
}),
...
]
});
```