alloc / saus

Vite SSR/SSG framework that aspires to be a layer for opinionated web frameworks to build upon
Other
38 stars 1 forks source link

Add compile-time evaluation #46

Open aleclarson opened 2 years ago

aleclarson commented 2 years ago

Useful for compile-time data generation used in declaring routes, paths, etc.

// src/node/routes.ts
import { preval } from 'saus'

// Run this module at compile-time. Unwrap its default export. Top-level await is allowed.
const result = preval(import('./path/to/module'))

In the future, we could support inline function that references outside its scope.

// src/node/routes.ts
import { preval } from 'saus'
import fs from 'fs'

const result = preval(() => {
  return fs.readdirSync('./data')
})

…or even a simple expression:

// src/node/routes.ts
import { preval } from 'saus'
import fs from 'fs'

const result = preval(fs.readdirSync('./data'))

The result would be passed through our dataToEsm helper.