|
| 1 | +/* |
| 2 | + * Copyright (c) 2021-2023 Datalayer, Inc. |
| 3 | + * |
| 4 | + * MIT License |
| 5 | + */ |
| 6 | + |
| 7 | +import { defineConfig } from 'vite'; |
| 8 | +import react from '@vitejs/plugin-react'; |
| 9 | +import { treatAsCommonjs } from 'vite-plugin-treat-umd-as-commonjs'; |
| 10 | +import * as path from 'node:path'; |
| 11 | +import { fileURLToPath } from 'node:url'; |
| 12 | + |
| 13 | +const __filename = fileURLToPath(import.meta.url); |
| 14 | +const __dirname = path.dirname(__filename); |
| 15 | + |
| 16 | +// Entry points for examples (dev mode) |
| 17 | +const ENTRY_POINTS: Record<string, string> = { |
| 18 | + app: './src/app/App.tsx', |
| 19 | + cell: './src/examples/Cell.tsx', |
| 20 | + console: './src/examples/Console.tsx', |
| 21 | + filebrowser: './src/examples/FileBrowser.tsx', |
| 22 | + notebook: './src/examples/Notebook.tsx', |
| 23 | + notebookthemecolormode: './src/examples/NotebookThemeColormode.tsx', |
| 24 | + terminal: './src/examples/Terminal.tsx', |
| 25 | + viewer: './src/examples/Viewer.tsx', |
| 26 | +}; |
| 27 | + |
| 28 | +// Get current entry from environment or default |
| 29 | +const CURRENT_ENTRY = process.env.VITE_ENTRY || 'notebookthemecolormode'; |
| 30 | + |
| 31 | +const IS_PRODUCTION = process.env.NODE_ENV === 'production'; |
| 32 | +const IS_LOCAL_JUPYTER_SERVER = process.env.LOCAL_JUPYTER_SERVER === 'true'; |
| 33 | +const IS_NO_CONFIG = process.env.NO_CONFIG === 'true'; |
| 34 | +const BUILD_LIB = process.env.BUILD_LIB === 'true'; |
| 35 | + |
| 36 | +export default defineConfig(({ mode, command }) => { |
| 37 | + const isServe = command === 'serve'; |
| 38 | + const isBuild = command === 'build'; |
| 39 | + |
| 40 | + // Library build configuration |
| 41 | + if (isBuild && BUILD_LIB) { |
| 42 | + return { |
| 43 | + build: { |
| 44 | + lib: { |
| 45 | + entry: path.resolve(__dirname, 'src/index.ts'), |
| 46 | + name: 'JupyterReact', |
| 47 | + formats: ['es', 'cjs'], |
| 48 | + fileName: format => `index.${format === 'es' ? 'js' : 'cjs'}`, |
| 49 | + }, |
| 50 | + outDir: 'lib', |
| 51 | + sourcemap: true, |
| 52 | + rollupOptions: { |
| 53 | + external: [ |
| 54 | + 'react', |
| 55 | + 'react-dom', |
| 56 | + 'react/jsx-runtime', |
| 57 | + /^@jupyterlab\/.*/, |
| 58 | + /^@lumino\/.*/, |
| 59 | + /^@jupyter\/.*/, |
| 60 | + /^@jupyter-widgets\/.*/, |
| 61 | + /^@jupyterlite\/.*/, |
| 62 | + ], |
| 63 | + output: { |
| 64 | + preserveModules: true, |
| 65 | + preserveModulesRoot: 'src', |
| 66 | + assetFileNames: assetInfo => { |
| 67 | + if (assetInfo.name?.endsWith('.css')) { |
| 68 | + return '[name][extname]'; |
| 69 | + } |
| 70 | + return 'assets/[name]-[hash][extname]'; |
| 71 | + }, |
| 72 | + }, |
| 73 | + }, |
| 74 | + }, |
| 75 | + plugins: [react(), treatAsCommonjs()], |
| 76 | + }; |
| 77 | + } |
| 78 | + |
| 79 | + // Development server and example build configuration |
| 80 | + return { |
| 81 | + root: __dirname, |
| 82 | + server: { |
| 83 | + port: 3208, |
| 84 | + host: true, |
| 85 | + hmr: { |
| 86 | + overlay: false, |
| 87 | + }, |
| 88 | + proxy: IS_LOCAL_JUPYTER_SERVER |
| 89 | + ? { |
| 90 | + '/api': { |
| 91 | + target: 'http://localhost:8686', |
| 92 | + ws: true, |
| 93 | + changeOrigin: true, |
| 94 | + }, |
| 95 | + '/terminals': { |
| 96 | + target: 'http://localhost:8686', |
| 97 | + ws: true, |
| 98 | + changeOrigin: true, |
| 99 | + }, |
| 100 | + } |
| 101 | + : undefined, |
| 102 | + }, |
| 103 | + plugins: [ |
| 104 | + react(), |
| 105 | + treatAsCommonjs(), |
| 106 | + { |
| 107 | + name: 'raw-css-as-string', |
| 108 | + enforce: 'pre', |
| 109 | + async resolveId(source, importer) { |
| 110 | + if (source.endsWith('.raw.css') && !source.includes('?raw')) { |
| 111 | + const resolved = await this.resolve(source + '?raw', importer, { |
| 112 | + skipSelf: true, |
| 113 | + }); |
| 114 | + if (resolved) return resolved.id; |
| 115 | + return null; |
| 116 | + } |
| 117 | + return null; |
| 118 | + }, |
| 119 | + }, |
| 120 | + { |
| 121 | + name: 'fix-text-query', |
| 122 | + enforce: 'pre', |
| 123 | + async resolveId(source, importer) { |
| 124 | + if (source.includes('?text')) { |
| 125 | + const fixed = source.replace('?text', '?raw'); |
| 126 | + const resolved = await this.resolve(fixed, importer, { |
| 127 | + skipSelf: true, |
| 128 | + }); |
| 129 | + if (resolved) { |
| 130 | + return resolved.id; |
| 131 | + } |
| 132 | + return fixed; |
| 133 | + } |
| 134 | + return null; |
| 135 | + }, |
| 136 | + }, |
| 137 | + { |
| 138 | + name: 'handle-theme-css', |
| 139 | + enforce: 'pre', |
| 140 | + transform(code, id) { |
| 141 | + if (id.includes('style/theme.css')) { |
| 142 | + return { |
| 143 | + code: `export default ${JSON.stringify(code)};`, |
| 144 | + map: null, |
| 145 | + }; |
| 146 | + } |
| 147 | + }, |
| 148 | + }, |
| 149 | + { |
| 150 | + name: 'copy-assets', |
| 151 | + generateBundle(options, bundle) { |
| 152 | + // Copy pypi and schema files |
| 153 | + for (const [fileName, source] of Object.entries(bundle)) { |
| 154 | + if (fileName.includes('pypi/') || fileName.includes('schema/')) { |
| 155 | + // These will be handled by rollup asset configuration |
| 156 | + } |
| 157 | + } |
| 158 | + }, |
| 159 | + }, |
| 160 | + ], |
| 161 | + assetsInclude: ['**/*.whl', '**/*.raw.css', '**/*.ipynb', '**/*.json'], |
| 162 | + resolve: { |
| 163 | + alias: [ |
| 164 | + { |
| 165 | + find: /^~(.*)$/, |
| 166 | + replacement: '$1', |
| 167 | + }, |
| 168 | + { |
| 169 | + find: 'stream', |
| 170 | + replacement: 'stream-browserify', |
| 171 | + }, |
| 172 | + ], |
| 173 | + }, |
| 174 | + define: { |
| 175 | + global: 'globalThis', |
| 176 | + 'process.env': process.env, |
| 177 | + __webpack_public_path__: '""', |
| 178 | + }, |
| 179 | + worker: { |
| 180 | + format: 'es', |
| 181 | + }, |
| 182 | + optimizeDeps: { |
| 183 | + include: ['react', 'react-dom', '@primer/react', 'styled-components'], |
| 184 | + exclude: [ |
| 185 | + '@jupyterlab/application', |
| 186 | + '@jupyterlab/apputils', |
| 187 | + '@jupyterlite/server', |
| 188 | + '@jupyterlite/pyodide-kernel', |
| 189 | + ], |
| 190 | + esbuildOptions: { |
| 191 | + loader: { |
| 192 | + '.whl': 'text', |
| 193 | + }, |
| 194 | + }, |
| 195 | + }, |
| 196 | + build: { |
| 197 | + outDir: 'dist', |
| 198 | + rollupOptions: { |
| 199 | + input: isServe |
| 200 | + ? undefined |
| 201 | + : { |
| 202 | + main: path.resolve(__dirname, 'index.html'), |
| 203 | + ...Object.fromEntries( |
| 204 | + Object.entries(ENTRY_POINTS).map(([name, entry]) => [ |
| 205 | + name, |
| 206 | + path.resolve(__dirname, entry), |
| 207 | + ]) |
| 208 | + ), |
| 209 | + }, |
| 210 | + output: { |
| 211 | + assetFileNames: assetInfo => { |
| 212 | + if (assetInfo.name && /pypi\//.test(assetInfo.name)) { |
| 213 | + return 'pypi/[name][extname]'; |
| 214 | + } |
| 215 | + if (assetInfo.name && /schema\//.test(assetInfo.name)) { |
| 216 | + return 'schema/[name][extname]'; |
| 217 | + } |
| 218 | + return 'assets/[name]-[hash][extname]'; |
| 219 | + }, |
| 220 | + chunkFileNames: 'chunks/[name]-[hash].js', |
| 221 | + entryFileNames: '[name].js', |
| 222 | + }, |
| 223 | + }, |
| 224 | + }, |
| 225 | + }; |
| 226 | +}); |
0 commit comments