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: { index: path.join(root, 'src/index.ts'), 'hm-dialog': path.join(root, 'src/hm-dialog.tsx') }, formats: ['es'], fileName: (_format, name) => `${name}.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' }); for (const name of ['index', 'hm-dialog']) { const entry = path.join(root, `dist/${name}.js`); const content = await readFile(entry, 'utf8'); await writeFile(entry, content.startsWith('"use client";') ? content : `"use client";\n${content}`); }