import fs from "node:fs"; import path from "node:path"; import { defineConfig } from "vite"; import dts from "vite-plugin-dts"; import glsl from "vite-plugin-glsl"; const assetsDirectory = "examples/assets"; const localAssetsDirectoryExist = fs.existsSync(assetsDirectory); if (!localAssetsDirectoryExist) { console.log( "************************************************************************", ); console.log(" Examples assets will be fetched from an external server."); console.log( " To work offline you can download them: npm run assets:download", ); console.log( "************************************************************************", ); } export default defineConfig(({ mode }) => { const isMinify = mode === "production"; const isFirstPass = mode === "production"; return { appType: "mpa", plugins: [ glsl({ include: ["**/*.glsl"], }), dts({ outDir: "dist/types" }), { name: "serve-three-alias", configureServer(server) { const urlPath = "/examples/js/vendor/three/"; const filePath = path.resolve("node_modules/three/"); server.middlewares.use(urlPath, (req, res, next) => { if (fs.existsSync(filePath)) { res.setHeader("Content-Type", "application/javascript"); fs.createReadStream(filePath).pipe(res); } else { res.statusCode = 404; res.end("three.module.js not found"); } }); console.log(`📦 Dev alias active: ${urlPath} → ${filePath}`); }, }, ], build: { minify: isMinify, lib: { entry: path.resolve(__dirname, "src/index.ts"), name: "forge", formats: ["es", "cjs"], fileName: (format) => { const base = format === "es" ? "forge.module" : `forge.${format}`; return isMinify ? `${base}.min.js` : `${base}.js`; }, }, sourcemap: true, rollupOptions: { external: ["three"], output: { globals: { three: "THREE", }, }, }, emptyOutDir: isFirstPass, }, worker: { plugins: () => [ glsl({ include: ["**/*.glsl"], }), ], }, server: { watch: { usePolling: true, }, port: 8080, }, optimizeDeps: { force: true, exclude: ["three"], // prevent Vite pre-bundling }, define: { forgeLocalAssets: localAssetsDirectoryExist, }, }; });