Build Environment

Access to build environment can help web components supercharge developer experience. For example, access to git history of HTML files allows web components to automatically deduce the author or publish date of a page. pree provides such access to web components during pre-rendering, through APIs accessible at /@env/ path:

const res = await fetch('/@env/git/commits/first/my-page.html')
const commit = await res.json()

const author = commit.author_name
const published = new Date(commit.date)

For example, this custom component adds the GitHub link in the footer of this page:

import { define, onFirstRender } from 'https://esm.sh/minicomp'
import { html, ref } from 'https://esm.sh/rehtm'

define('gh-link', () => {
  const anchor = ref()

  const load = async () => {
    const res = await fetch('/@env/git/remote/info')
    const info = await res.json()

    anchor.current.href = `https://${info.host}/${info.full_name}`
  }

  onFirstRender(() => load())

  return html`
    <link rel="stylesheet" href="https://unpkg.com/nokss/dist/bundles/md.css">
    <a ref="${anchor}" target="_blank"><slot>GitHub</slot></a>
  `
})

Provided APIs

Build-env APIs are only available during pre-rendering, i.e. during execution of pree build or pree view commands. They should not be invoked in the final website: either load components and scripts using them as build-only (see this), or check for their availability via window.BUILD_ENV_API variable:

if (window.BUILD_ENV_API) {
  // use the APIs
}
Build Environment APIs

/@env/conf

pree's configuration

JSON

Example
{
  "src": "docs",
  "dest": "site",
  "prod": false,
  ...
}

/@env/conf/:var

specific configuration variable

JSON

Example

/@env/conf/src

"docs"

/@env/vars/:var

specific environment variable

text

Example

/@env/vars/PATH

/usr/local/bin:/usr/bin:/bin

/@env/git/remote/info

remote git repository info

JSON

Example
{
  "host": "github.com",
  "full_name": "loreanvictor/pree",
  ...
}

/@env/git/remote/url

remote git repository URL

text

Example
git@github.com:loreanvictor/pree.git

/@env/git/commits/first

first commit of the project

JSON

Example
{
  "hash": "d22c84...",
  "author_name": "Eugene Ghanizadeh Khoub",
  "author_email": "ghanizadeh.eugene@gmail.com",
  "date": "2024-01-26T13:49:32+01:00",
  "message": "Initial commit"
}

/@env/git/commits/last

last commit of the project

JSON

Example
{
  "hash": "051kl3z...",
  "author_name": "Eugene Ghanizadeh Khoub",
  "author_email": "ghanizadeh.eugene@gmail.com",
  "date": "2024-02-04T14:21:50+01:00",
  "message": "Some polush"
}

/@env/git/commits/first/:PATH

first commit of given file

JSON

Example

/@env/git/commits/first/docs/usage.html

{
  "hash": "3653af...",
  "author_name": "Eugene Ghanizadeh Khoub",
  ...
}

/@env/git/commits/last/:PATH

last commit of given file

JSON

Example

/@env/git/commits/last/docs/usage.html

{
  "hash": "72g421...",
  "author_name": "Eugene Ghanizadeh Khoub",
  ...
}

/@env/git/commits/all

all commits of the project

JSON

Example

/@env/git/commits/all

[
  { "hash": "3653af...", ...},
  { "hash": "d22c84...", ...},
  ...
]

/@env/git/commits/all/:PATH

all commits of given file

JSON

Example

/@env/git/commits/all/docs/usage.html

[
  { "hash": "3653af...", ...},
  { "hash": "d22c84...", ...},
  ...
]

/@env/git/show/:OBJECT

git show given object

file format

Example

/@env/git/show/HEAD:docs/index.html

---
layout: ./_layout.html
---

<div slot="background">
  <use-html src="./_lines.html"></use-html>
</div>

...

/@env/files/list

A list of files of the project

JSON

Example
[
  ".eslintrc",
  ".pree.yml",
  "LICENSE",
  "docs/index.html",
  "docs/usage/index.html",
  ...
]

/@env/files/list/:PATTERN

A list of files matching given pattern

JSON

Example

/@env/files/list/docs/**/*.html

[
  "docs/index.html",
  "docs/usage/index.html",
  ...
]

/@env/files/read/:PATH

Read the content of a file

file format

Example

/@env/files/read/package.json

{
  "name": "pree",
  "version": "0.1.10",
  ...
}


pree logo