22/// <reference types="vitest" />
33
44import {
5+ deduplicateAssets ,
56 extensionDev ,
67 htmlTrustedPrelude ,
78 jsTrustedPrelude ,
@@ -14,8 +15,10 @@ import { viteStaticCopy } from 'vite-plugin-static-copy';
1415import type { Target } from 'vite-plugin-static-copy' ;
1516
1617import {
18+ rootDir ,
19+ kernelBrowserRuntimeSrcDir ,
20+ outDir ,
1721 sourceDir ,
18- buildDir ,
1922 trustedPreludes ,
2023} from './scripts/build-constants.mjs' ;
2124
@@ -25,15 +28,11 @@ import {
2528 */
2629const staticCopyTargets : readonly ( string | Target ) [ ] = [
2730 // The extension manifest
28- 'manifest.json' ,
29- // External modules
30- 'env/dev-console.js' ,
31- 'env/background-trusted-prelude.js' ,
32- '../../kernel-shims/dist/endoify.js' ,
33- {
34- src : '../../kernel-browser-runtime/dist/static/*' ,
35- dest : './browser-runtime' ,
36- } ,
31+ path . resolve ( sourceDir , 'manifest.json' ) ,
32+ // Trusted prelude-related
33+ path . resolve ( sourceDir , 'env/dev-console.js' ) ,
34+ path . resolve ( sourceDir , 'env/background-trusted-prelude.js' ) ,
35+ path . resolve ( rootDir , 'kernel-shims/dist/endoify.js' ) ,
3736] ;
3837
3938// https://vitejs.dev/config/
@@ -45,16 +44,26 @@ export default defineConfig(({ mode }) => {
4544 }
4645
4746 return {
48- root : sourceDir ,
47+ root : rootDir ,
4948
5049 build : {
50+ assetsDir : '' ,
5151 emptyOutDir : true ,
52- outDir : buildDir ,
52+ // Disable Vite's module preload, which may cause SES-dependent code to run before lockdown.
53+ modulePreload : false ,
54+ outDir,
5355 rollupOptions : {
5456 input : {
5557 background : path . resolve ( sourceDir , 'background.ts' ) ,
5658 offscreen : path . resolve ( sourceDir , 'offscreen.html' ) ,
5759 popup : path . resolve ( sourceDir , 'popup.html' ) ,
60+ // kernel-browser-runtime
61+ 'kernel-worker' : path . resolve (
62+ kernelBrowserRuntimeSrcDir ,
63+ 'kernel-worker' ,
64+ 'kernel-worker.ts' ,
65+ ) ,
66+ vat : path . resolve ( kernelBrowserRuntimeSrcDir , 'vat' , 'iframe.html' ) ,
5867 } ,
5968 output : {
6069 entryFileNames : '[name].js' ,
@@ -82,10 +91,30 @@ export default defineConfig(({ mode }) => {
8291 silent : isDev ,
8392 } ) ,
8493 viteChecker ( { typescript : { tsconfigPath : 'tsconfig.build.json' } } ) ,
85- // Import sourcemaps from our own libraries
86- // For whatever reason, the types don't match, but it works
94+ // Deduplicate sqlite-wasm assets
95+ deduplicateAssets ( {
96+ assetFilter : ( fileName ) =>
97+ fileName . includes ( 'sqlite3-' ) &&
98+ ! fileName . includes ( 'sqlite3-opfs-async-proxy' ) ,
99+ expectedCount : 2 ,
100+ } ) ,
101+ // Would you believe that there's no other way to do this?
102+ {
103+ name : 'move-html-files-to-root' ,
104+ generateBundle : {
105+ order : 'post' ,
106+ handler ( _ , bundle ) {
107+ for ( const chunk of Object . values ( bundle ) ) {
108+ if ( ! chunk . fileName . endsWith ( '.html' ) ) {
109+ continue ;
110+ }
111+ chunk . fileName = path . basename ( chunk . fileName ) ;
112+ }
113+ } ,
114+ } ,
115+ } ,
87116 // Open the extension in the browser when watching
88- isWatching && extensionDev ( { extensionPath : buildDir } ) ,
117+ isWatching && extensionDev ( { extensionPath : outDir } ) ,
89118 ] ,
90119 } ;
91120} ) ;
0 commit comments