master
AdityaAngajala v0.12.8 160fd3c
import { defineConfig } from 'vite';
import { svelte } from '@sveltejs/vite-plugin-svelte';
import { readFileSync } from 'fs';
import { transform } from 'esbuild';

// Vite's library mode leaves whitespace in ES output (to keep pure
// annotations for consumers' tree-shaking); this bundle is the end product,
// so every chunk is fully minified after the build: about 30% smaller.
const minifyChunks = () => ({
  name: 'adom-minify-chunks',
  async renderChunk(code: string) {
    // The map rides along so the hidden build maps (below) still point at
    // the source after minification.
    const out = await transform(code, { minify: true, format: 'esm', target: 'es2020', legalComments: 'none', sourcemap: 'external' });
    return { code: out.code, map: out.map || null };
  },
});

// Babylon 9's @babylonjs/inspector module has circular imports that resolve
// to TDZ when collapsed into a single IIFE scope (regardless of minifier).
// Output ESM instead — module boundaries preserve the original import order
// so the inspector initializes cleanly. Consumers load via
// `<script type="module" src="adom-3d-viewer-babylon9.esm.js"></script>`.
export default defineConfig({
  plugins: [svelte({ compilerOptions: { css: 'injected' } }), minifyChunks()],
  // @babylonjs/inspector v9 pulls in React, which references the Node-only
  // `process.env.NODE_ENV` global at runtime. Vite injects this automatically
  // in `serve` and standard `build` modes but NOT in `lib` mode, so without
  // a define the inspector crashes with "process is not defined" the first
  // time the user opens it. Replacing the env lookup at bundle time strips
  // every `if (process.env.NODE_ENV !== 'production') warn(...)` branch
  // from React, which incidentally removes the only `process` references.
  define: {
    'process.env.NODE_ENV': JSON.stringify('production'),
    // Single source of truth for the runtime `version` field: package.json.
    // Read at config load so a version bump needs no code edit.
    __VIEWER_VERSION__: JSON.stringify(
      JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8')).version,
    ),
  },
  build: {
    lib: {
      entry: 'standalone.ts',
      formats: ['es'],
      name: 'Adom3DViewerBabylon9',
      fileName: () => 'adom-3d-viewer-babylon9.esm.js',
    },
    rollupOptions: {
      external: [],
      output: {
        // Allow `await import(...)` to produce separate chunks. The inspector
        // lazy-import lands in its own scope and avoids the
        // "object is not extensible" error when collapsed into the main bundle.
        inlineDynamicImports: false,
        chunkFileNames: 'chunk-[hash].js',
      },
    },
    outDir: process.env.VIEWER_DIST || 'dist-standalone',
    minify: 'esbuild',
    // Hidden: the maps are written for turning a reported position
    // (chunk-x.js:1:23456) back into a source line, never referenced or served.
    sourcemap: "hidden",
    chunkSizeWarningLimit: 50000,
    target: 'es2020',
  },
  resolve: {
    dedupe: ['svelte', '@babylonjs/core'],
  },
});