| b69ab31 | | | 1 | /** |
| b69ab31 | | | 2 | * Copyright (c) Meta Platforms, Inc. and affiliates. |
| b69ab31 | | | 3 | * |
| b69ab31 | | | 4 | * This source code is licensed under the MIT license found in the |
| b69ab31 | | | 5 | * LICENSE file in the root directory of this source tree. |
| b69ab31 | | | 6 | */ |
| b69ab31 | | | 7 | |
| b69ab31 | | | 8 | import react from '@vitejs/plugin-react'; |
| b69ab31 | | | 9 | import fs from 'node:fs'; |
| b69ab31 | | | 10 | import path, {resolve} from 'node:path'; |
| b69ab31 | | | 11 | import {defineConfig} from 'vite'; |
| b69ab31 | | | 12 | import styleX from 'vite-plugin-stylex'; |
| b69ab31 | | | 13 | import viteTsconfigPaths from 'vite-tsconfig-paths'; |
| b69ab31 | | | 14 | |
| b69ab31 | | | 15 | // Normalize `c:\foo\index.html` to `c:/foo/index.html`. |
| b69ab31 | | | 16 | // This affects Rollup's `facadeModuleId` (which expects the `c:/foo/bar` format), |
| b69ab31 | | | 17 | // and is important for Vite to replace the script tags in HTML files. |
| b69ab31 | | | 18 | // See https://github.com/vitejs/vite/blob/7440191715b07a50992fcf8c90d07600dffc375e/packages/vite/src/node/plugins/html.ts#L804 |
| b69ab31 | | | 19 | // Without this, building on Windows might produce HTML entry points with |
| b69ab31 | | | 20 | // missing `<script>` tags, resulting in a blank page. |
| b69ab31 | | | 21 | function normalizeInputPath(inputPath: string) { |
| b69ab31 | | | 22 | return process.platform === 'win32' ? resolve(inputPath).replace(/\\/g, '/') : inputPath; |
| b69ab31 | | | 23 | } |
| b69ab31 | | | 24 | |
| b69ab31 | | | 25 | // TODO: this could be a glob on src/platform/*.html |
| b69ab31 | | | 26 | const platforms = { |
| b69ab31 | | | 27 | main: normalizeInputPath('index.html'), |
| b69ab31 | | | 28 | androidStudio: normalizeInputPath('androidStudio.html'), |
| b69ab31 | | | 29 | androidStudioRemote: normalizeInputPath('androidStudioRemote.html'), |
| b69ab31 | | | 30 | webview: normalizeInputPath('webview.html'), |
| b69ab31 | | | 31 | chromelikeApp: normalizeInputPath('chromelikeApp.html'), |
| b69ab31 | | | 32 | visualStudio: normalizeInputPath('visualStudio.html'), |
| b69ab31 | | | 33 | obsidian: normalizeInputPath('obsidian.html'), |
| b69ab31 | | | 34 | }; |
| b69ab31 | | | 35 | |
| b69ab31 | | | 36 | export default defineConfig({ |
| b69ab31 | | | 37 | base: '', |
| b69ab31 | | | 38 | plugins: [ |
| b69ab31 | | | 39 | react({ |
| b69ab31 | | | 40 | babel: { |
| b69ab31 | | | 41 | plugins: [ |
| b69ab31 | | | 42 | [ |
| b69ab31 | | | 43 | 'jotai/babel/plugin-debug-label', |
| b69ab31 | | | 44 | { |
| b69ab31 | | | 45 | customAtomNames: [ |
| b69ab31 | | | 46 | 'atomFamilyWeak', |
| b69ab31 | | | 47 | 'atomLoadableWithRefresh', |
| b69ab31 | | | 48 | 'atomWithOnChange', |
| b69ab31 | | | 49 | 'atomWithRefresh', |
| b69ab31 | | | 50 | 'atomLoadableWithRefresh', |
| b69ab31 | | | 51 | 'atomResetOnCwdChange', |
| b69ab31 | | | 52 | 'atomResetOnDepChange', |
| b69ab31 | | | 53 | 'configBackedAtom', |
| b69ab31 | | | 54 | 'jotaiAtom', |
| b69ab31 | | | 55 | 'lazyAtom', |
| b69ab31 | | | 56 | 'localStorageBackedAtom', |
| b69ab31 | | | 57 | ], |
| b69ab31 | | | 58 | }, |
| b69ab31 | | | 59 | ], |
| b69ab31 | | | 60 | 'jotai/babel/plugin-react-refresh', |
| b69ab31 | | | 61 | ], |
| b69ab31 | | | 62 | }, |
| b69ab31 | | | 63 | }), |
| b69ab31 | | | 64 | styleX(), |
| b69ab31 | | | 65 | viteTsconfigPaths(), |
| b69ab31 | | | 66 | // The manifest vite generates doesn't include web worker js files. |
| b69ab31 | | | 67 | // Just output a simple list of all files that are produced, |
| b69ab31 | | | 68 | // and the server can serve those known files. |
| b69ab31 | | | 69 | { |
| b69ab31 | | | 70 | name: 'emit-file-list', |
| b69ab31 | | | 71 | apply: 'build', |
| b69ab31 | | | 72 | writeBundle(options: {dir: string | undefined}, bundle: Record<string, unknown>) { |
| b69ab31 | | | 73 | const outputDir = options.dir || 'dist'; |
| b69ab31 | | | 74 | const fileList = Object.keys(bundle) |
| b69ab31 | | | 75 | .filter(file => file !== '.vite/manifest.json') |
| b69ab31 | | | 76 | .map(p => p.replace(/\\/g, '/')); |
| b69ab31 | | | 77 | fs.writeFileSync( |
| b69ab31 | | | 78 | path.join(outputDir, 'assetList.json'), |
| b69ab31 | | | 79 | JSON.stringify(fileList, undefined, 2), |
| b69ab31 | | | 80 | ); |
| b69ab31 | | | 81 | }, |
| b69ab31 | | | 82 | }, |
| b69ab31 | | | 83 | ], |
| b69ab31 | | | 84 | build: { |
| b69ab31 | | | 85 | outDir: 'build', |
| b69ab31 | | | 86 | rollupOptions: { |
| b69ab31 | | | 87 | input: platforms, |
| b69ab31 | | | 88 | }, |
| b69ab31 | | | 89 | }, |
| b69ab31 | | | 90 | server: { |
| b69ab31 | | | 91 | // No need to open the browser, it's opened by `yarn serve` in `isl-server`. |
| b69ab31 | | | 92 | open: false, |
| b69ab31 | | | 93 | port: 3000, |
| b69ab31 | | | 94 | }, |
| b69ab31 | | | 95 | }); |