addons/isl/vite.config.tsblame
View source
b69ab311/**
b69ab312 * Copyright (c) Meta Platforms, Inc. and affiliates.
b69ab313 *
b69ab314 * This source code is licensed under the MIT license found in the
b69ab315 * LICENSE file in the root directory of this source tree.
b69ab316 */
b69ab317
b69ab318import react from '@vitejs/plugin-react';
b69ab319import fs from 'node:fs';
b69ab3110import path, {resolve} from 'node:path';
b69ab3111import {defineConfig} from 'vite';
b69ab3112import styleX from 'vite-plugin-stylex';
b69ab3113import viteTsconfigPaths from 'vite-tsconfig-paths';
b69ab3114
b69ab3115// Normalize `c:\foo\index.html` to `c:/foo/index.html`.
b69ab3116// This affects Rollup's `facadeModuleId` (which expects the `c:/foo/bar` format),
b69ab3117// and is important for Vite to replace the script tags in HTML files.
b69ab3118// See https://github.com/vitejs/vite/blob/7440191715b07a50992fcf8c90d07600dffc375e/packages/vite/src/node/plugins/html.ts#L804
b69ab3119// Without this, building on Windows might produce HTML entry points with
b69ab3120// missing `<script>` tags, resulting in a blank page.
b69ab3121function normalizeInputPath(inputPath: string) {
b69ab3122 return process.platform === 'win32' ? resolve(inputPath).replace(/\\/g, '/') : inputPath;
b69ab3123}
b69ab3124
b69ab3125// TODO: this could be a glob on src/platform/*.html
b69ab3126const platforms = {
b69ab3127 main: normalizeInputPath('index.html'),
b69ab3128 androidStudio: normalizeInputPath('androidStudio.html'),
b69ab3129 androidStudioRemote: normalizeInputPath('androidStudioRemote.html'),
b69ab3130 webview: normalizeInputPath('webview.html'),
b69ab3131 chromelikeApp: normalizeInputPath('chromelikeApp.html'),
b69ab3132 visualStudio: normalizeInputPath('visualStudio.html'),
b69ab3133 obsidian: normalizeInputPath('obsidian.html'),
b69ab3134};
b69ab3135
b69ab3136export default defineConfig({
b69ab3137 base: '',
b69ab3138 plugins: [
b69ab3139 react({
b69ab3140 babel: {
b69ab3141 plugins: [
b69ab3142 [
b69ab3143 'jotai/babel/plugin-debug-label',
b69ab3144 {
b69ab3145 customAtomNames: [
b69ab3146 'atomFamilyWeak',
b69ab3147 'atomLoadableWithRefresh',
b69ab3148 'atomWithOnChange',
b69ab3149 'atomWithRefresh',
b69ab3150 'atomLoadableWithRefresh',
b69ab3151 'atomResetOnCwdChange',
b69ab3152 'atomResetOnDepChange',
b69ab3153 'configBackedAtom',
b69ab3154 'jotaiAtom',
b69ab3155 'lazyAtom',
b69ab3156 'localStorageBackedAtom',
b69ab3157 ],
b69ab3158 },
b69ab3159 ],
b69ab3160 'jotai/babel/plugin-react-refresh',
b69ab3161 ],
b69ab3162 },
b69ab3163 }),
b69ab3164 styleX(),
b69ab3165 viteTsconfigPaths(),
b69ab3166 // The manifest vite generates doesn't include web worker js files.
b69ab3167 // Just output a simple list of all files that are produced,
b69ab3168 // and the server can serve those known files.
b69ab3169 {
b69ab3170 name: 'emit-file-list',
b69ab3171 apply: 'build',
b69ab3172 writeBundle(options: {dir: string | undefined}, bundle: Record<string, unknown>) {
b69ab3173 const outputDir = options.dir || 'dist';
b69ab3174 const fileList = Object.keys(bundle)
b69ab3175 .filter(file => file !== '.vite/manifest.json')
b69ab3176 .map(p => p.replace(/\\/g, '/'));
b69ab3177 fs.writeFileSync(
b69ab3178 path.join(outputDir, 'assetList.json'),
b69ab3179 JSON.stringify(fileList, undefined, 2),
b69ab3180 );
b69ab3181 },
b69ab3182 },
b69ab3183 ],
b69ab3184 build: {
b69ab3185 outDir: 'build',
b69ab3186 rollupOptions: {
b69ab3187 input: platforms,
b69ab3188 },
b69ab3189 },
b69ab3190 server: {
b69ab3191 // No need to open the browser, it's opened by `yarn serve` in `isl-server`.
b69ab3192 open: false,
b69ab3193 port: 3000,
b69ab3194 },
b69ab3195});