import { build } from 'vite'; import { readFile, writeFile } from 'node:fs/promises'; import { fileURLToPath } from 'node:url'; import { createRequire } from 'node:module'; import { execFileSync } from 'node:child_process'; import path from 'node:path'; const root = fileURLToPath(new URL('./', import.meta.url)); const require = createRequire(import.meta.url); const manifest = JSON.parse(await readFile(path.join(root, 'package.json'), 'utf8')); const externals = Object.keys({ ...manifest.dependencies, ...manifest.peerDependencies }); await build({ configFile: false, root, publicDir: false, build: { target: 'es2022', outDir: 'dist', minify: false, lib: { entry: path.join(root, 'src/index.ts'), formats: ['es'], fileName: () => 'index.js' }, rolldownOptions: { external: id => externals.some(dependency => id === dependency || id.startsWith(`${dependency}/`)), onwarn(warning, warn) { // Restore the client boundary on the final public entry below. if (warning.code === 'MODULE_LEVEL_DIRECTIVE') return; warn(warning); }, }, }, }); const typescriptManifest = require.resolve('typescript/package.json'); const typescript = JSON.parse(await readFile(typescriptManifest, 'utf8')); execFileSync(process.execPath, [ path.resolve(path.dirname(typescriptManifest), typescript.bin.tsc), '-p', path.join(root, 'tsconfig.build.json'), ], { stdio: 'inherit' }); const entry = path.join(root, 'dist/index.js'); await writeFile(entry, `"use client";\n${await readFile(entry, 'utf8')}`);