forked from
npmx.dev/npmx.dev
[READ-ONLY]
a fast, modern browser for the npm registry
1import { defineNuxtModule, useNuxt } from 'nuxt/kit'
2import { execSync } from 'node:child_process'
3import { join } from 'node:path'
4import { existsSync, mkdirSync } from 'node:fs'
5import { isCI, isTest } from 'std-env'
6import { getEnv } from '../config/env.ts'
7
8export default defineNuxtModule({
9 meta: {
10 name: 'lunaria',
11 },
12 setup() {
13 const nuxt = useNuxt()
14
15 const lunariaDistPath = join(nuxt.options.rootDir, 'dist/lunaria/')
16
17 nuxt.options.nitro.publicAssets ||= []
18 nuxt.options.nitro.publicAssets.push({
19 dir: lunariaDistPath,
20 baseURL: '/lunaria/',
21 maxAge: 60 * 60 * 24, // 1 day
22 })
23
24 if (nuxt.options.dev || nuxt.options._prepare || nuxt.options.test || isTest) {
25 return
26 }
27
28 if (!isCI || !existsSync(lunariaDistPath)) {
29 mkdirSync(lunariaDistPath, { recursive: true })
30 nuxt.hook('nitro:build:before', async () => {
31 try {
32 execSync('node --experimental-transform-types ./lunaria/lunaria.ts', {
33 cwd: nuxt.options.rootDir,
34 })
35 } catch (e) {
36 // Always throw in local dev.
37 // In CI, only throw if building for production.
38 const { env } = await getEnv(!isCI)
39 if (env === 'dev' || env === 'release') {
40 throw e
41 }
42 }
43 })
44 }
45 },
46})