-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathvite.config.js
More file actions
141 lines (133 loc) · 4.3 KB
/
vite.config.js
File metadata and controls
141 lines (133 loc) · 4.3 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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import fs from 'fs/promises';
import path from 'node:path';
import { fileURLToPath } from 'url';
import { globSync } from 'glob';
import * as packageJson from './package.json';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
/**
* Vite configuration
*/
export default defineConfig({
...(
process.env.NETLIFY ? {
build: {
rolldownOptions: {
external: ['__tests__/*', '__mocks__/*'],
input: Object.fromEntries(
globSync('./demo/*.html').map((file) => [
// This remove `src/` as well as the file extension from each
// file, so e.g. src/nested/foo.js becomes nested/foo
path.relative(
'demo',
file.slice(0, file.length - path.extname(file).length),
),
// This expands the relative paths to absolute paths, so e.g.
// src/nested/foo becomes /project/src/nested/foo.js
fileURLToPath(new URL(file, import.meta.url)),
]),
),
},
sourcemap: true,
},
} : {
build: {
lib: {
entry: './src/index.js',
fileName: (format) => (format === 'es' ? 'mirador.es.js' : undefined),
formats: ['es'],
name: 'Mirador',
},
rolldownOptions: {
external: (id, parentId) => {
const peers = Object.keys(packageJson.peerDependencies);
return peers.indexOf(id) > -1
|| peers.some((peer) => id.startsWith(`${peer}/`))
|| id.startsWith('__tests__/')
|| id.startsWith('__mocks__/');
},
output: {
assetFileNames: 'mirador.[ext]',
exports: 'named',
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
},
},
sourcemap: true,
},
}
),
define: {
'process.env': {},
},
plugins: [
react(),
// Copy fixtures to dist for Netlify
process.env.NETLIFY && {
closeBundle: async () => {
const fixturesSource = path.resolve(__dirname, '__tests__/fixtures');
const fixturesDest = path.resolve(__dirname, 'dist/__tests__/fixtures');
await fs.cp(fixturesSource, fixturesDest, { recursive: true });
console.log('[copy] Copied fixtures to dist');
},
name: 'copy-fixtures',
},
{
/**
* Middleware to rewrite HTML URLs to point to the deep path
*/
configureServer(server) {
server.middlewares.use(async (req, res, next) => {
// Strip query parameters
const originalUrl = req.url.split('?')[0];
// Handle root URL directly
if (originalUrl === '/') {
req.url = '/demo/index.html'; // eslint-disable-line no-param-reassign
return next();
}
// Skip if path already rewritten or includes file extensions other than .html
if (originalUrl.startsWith('/demo/')) return next();
if (path.extname(originalUrl) && path.extname(originalUrl) !== '.html') return next();
// Add .html extension if needed
const pathWithExtension = path.extname(originalUrl)
? originalUrl
: `${originalUrl}.html`;
const deepPath = path.join(
'demo',
decodeURIComponent(pathWithExtension),
);
try {
// Check if this is a file we own (not HMR-related vite files, for example)
await fs.access(deepPath);
req.url = `/demo${pathWithExtension}`; // eslint-disable-line no-param-reassign
} catch {
// Not ours / does not exist — skip rewrite
}
return next();
});
},
name: 'html-url-rewrite',
},
],
resolve: {
alias: {
'@tests/': fileURLToPath(new URL('./__tests__', import.meta.url)),
},
},
server: {
fs: {
allow: [
path.resolve(__dirname, 'src'),
path.resolve(__dirname, 'demo'),
path.resolve(__dirname, '__tests__/integration/'),
path.resolve(__dirname, '__tests__/fixtures'),
], // allow serving from here
},
middlewareMode: false,
open: '/index.html',
port: '4444',
},
});