-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.config.js
More file actions
47 lines (45 loc) · 2.13 KB
/
Copy pathvite.config.js
File metadata and controls
47 lines (45 loc) · 2.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import {readFileSync} from 'node:fs';
import {defineConfig} from 'vite';
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';
import cssInjectedByJsPlugin from 'vite-plugin-css-injected-by-js';
// The bundle is named for the package version so hosts can pin a release and
// bust caches on upgrade. semantic-release writes this field before tagging,
// so a build after a release picks up the new version automatically.
const {version} = JSON.parse(readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
/*
The widget ships as a single self-mounting IIFE that LibreTexts injects via a
<script> tag: CSS is inlined into the JS (no separate stylesheet) and React is
bundled in (no externals). `npm run build` produces build/sidebar-<version>.min.js.
`npm run dev` serves index.html — a standalone harness that stubs the host
globals — with React Fast Refresh.
*/
export default defineConfig(({command}) => ({
plugins: [
react(),
// Tailwind v4 generates the utility CSS (Davis is styled with Tailwind utilities);
// css-injected-by-js then inlines the single emitted stylesheet into the IIFE.
// Order matters: Tailwind must run before the inliner.
tailwindcss(),
cssInjectedByJsPlugin(),
],
// Lib mode leaves process.env.NODE_ENV untouched (a library shouldn't pin
// its consumer's env), which ships React/MUI dev branches. This is a
// self-contained widget, so hardcode production for the build only. Dev
// must stay "development" for React warnings and Fast Refresh.
define: command === 'build' ? {'process.env.NODE_ENV': JSON.stringify('production')} : {},
build: {
target: 'es2020',
outDir: 'build',
emptyOutDir: true,
// Keep all CSS (Tailwind + Davis + our rules) in one asset so the inliner
// folds it into the single sidebar.min.js. No stray .css must escape.
cssCodeSplit: false,
lib: {
entry: 'src/pages/index.tsx',
formats: ['iife'],
name: 'LibreTextsSidebar',
fileName: () => `sidebar-${version}.min.js`,
},
},
}));