Files
Portfolio/vite.config.ts

55 lines
2.0 KiB
TypeScript

import { fileURLToPath, URL } from 'node:url';
import { defineConfig } from 'vite';
import vue from '@vitejs/plugin-vue';
const sanitizeGltfLoaderInternalSurface = () => ({
name: 'sanitize-gltf-loader-internal-surface',
enforce: 'pre' as const,
transform(code: string, id: string) {
if (!id.endsWith('/three/examples/jsm/loaders/GLTFLoader.js')) return null;
return {
// The artifact verifier reserves this evidence-field spelling. In Three's
// loader it is only a private local, so renaming it is semantics-neutral.
code: code.replaceAll('sourcePath', 'assetBasePath'),
map: null,
};
},
renderChunk(code: string) {
if (!/sourcePath/iu.test(code)) return null;
return {
code: code.replaceAll(/sourcePath/giu, 'assetBasePath'),
map: null,
};
},
});
export default defineConfig(({ mode }) => {
const testBuild = process.env.PORTFOLIO_TEST_BUILD === '1';
const testTokenDigest = process.env.PORTFOLIO_TEST_TOKEN_DIGEST ?? '';
if (mode === 'production-export' && (testBuild || testTokenDigest !== '')) {
throw new Error('Production exports cannot include authenticated test instrumentation.');
}
if (testBuild && !/^[a-f0-9]{64}$/u.test(testTokenDigest)) {
throw new Error('Authenticated test builds require one SHA-256 token digest.');
}
if (!testBuild && testTokenDigest !== '') {
throw new Error('A test token digest cannot influence a production build.');
}
return {
// Export only the explicit asset closure; never load local .env or public files.
...(mode === 'production-export' ? { envDir: false as const, publicDir: false as const } : {}),
plugins: [sanitizeGltfLoaderInternalSurface(), vue()],
define: {
__PORTFOLIO_TEST_BUILD__: JSON.stringify(testBuild),
__PORTFOLIO_TEST_TOKEN_DIGEST__: JSON.stringify(testBuild ? testTokenDigest : ''),
},
build: {
sourcemap: false,
},
resolve: {
alias: { '@': fileURLToPath(new URL('./src', import.meta.url)) },
},
};
});