-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathvite.config.mts
More file actions
179 lines (172 loc) · 4.58 KB
/
vite.config.mts
File metadata and controls
179 lines (172 loc) · 4.58 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
import path from 'node:path';
import { defineConfig, PluginOption } from 'vite';
import dts from 'vite-plugin-dts';
import react from '@vitejs/plugin-react';
import { reactCompilerConfig as $reactCompilerConfig } from '../graphiql-react/vite.config.mjs';
import type { PluginOptions as ReactCompilerConfig } from 'babel-plugin-react-compiler';
import packageJSON from './package.json' with { type: 'json' };
const reactCompilerConfig: Partial<ReactCompilerConfig> = {
...$reactCompilerConfig,
sources(filename) {
if (
filename.includes('__tests__') ||
/\.(spec|test)\.tsx?$/.test(filename)
) {
return false;
}
return filename.includes(`packages${path.sep}graphiql${path.sep}src`);
},
};
export const plugins: PluginOption[] = [
react({
babel: {
plugins: [['babel-plugin-react-compiler', reactCompilerConfig]],
},
}),
];
const umdConfig = defineConfig({
define: {
// graphql v17
'globalThis.process.env.NODE_ENV': 'true',
// https://github.com/graphql/graphql-js/blob/16.x.x/website/pages/docs/going-to-production.mdx
'globalThis.process': 'true',
'process.env.NODE_ENV': '"production"',
},
plugins,
css: {
transformer: 'lightningcss',
},
build: {
minify: 'terser', // produce less bundle size
sourcemap: true,
emptyOutDir: false,
lib: {
entry: 'src/cdn.ts',
/**
* The name of the exposed global variable. Required when the `formats` option includes `umd`
* or `iife`.
*/
name: 'GraphiQL',
fileName: 'index',
formats: ['umd'],
},
rollupOptions: {
external: ['react', 'react-dom'],
output: {
globals: {
react: 'React',
'react-dom': 'ReactDOM',
},
},
},
},
worker: {
format: 'es',
rollupOptions: {
output: {
entryFileNames: 'workers/[name].js',
// Just to group worker assets, add shared/internal chunks too
chunkFileNames: 'workers/[name].js',
},
},
},
});
const esmConfig = defineConfig({
build: {
cssCodeSplit: true,
minify: false,
sourcemap: true,
lib: {
entry: [
'src/index.ts',
'src/e2e.ts',
'src/setup-workers/webpack.ts',
'src/setup-workers/vite.ts',
'src/setup-workers/esm.sh.ts',
],
fileName: (_format, filePath) => `${filePath}.js`,
formats: ['es'],
cssFileName: 'style',
},
rollupOptions: {
external: [
'react/jsx-runtime',
// Exclude peer dependencies and dependencies from bundle
...Object.keys({
...packageJSON.peerDependencies,
...packageJSON.dependencies,
}),
/^@graphiql\/react\//,
],
output: {
// Separate chunks for all modules
preserveModules: true,
},
},
},
server: {
// Prevent a browser window from opening automatically
open: false,
proxy: {
'/graphql': 'http://localhost:8080',
'/subscriptions': {
target: 'ws://localhost:8081',
ws: true,
},
},
},
plugins: [
...plugins,
htmlPlugin(),
process.env.NODE_ENV === 'production' && removeImportsFromE2EFile(),
dts({
include: ['src/**'],
outDir: ['dist'],
exclude: ['**/*.spec.{ts,tsx}', '**/__tests__/'],
}),
],
worker: {
format: 'es',
},
});
function htmlPlugin(): PluginOption {
return {
name: 'html-replace-umd-with-src',
transformIndexHtml: {
order: 'pre',
handler(html) {
const start = '<!--vite-replace-start-->';
const end = '<!--vite-replace-end-->';
const contentToReplace = html.slice(
html.indexOf(start),
html.indexOf(end) + end.length,
);
return html.replace(
contentToReplace,
'<script type="module" src="/src/e2e.ts"></script>',
);
},
},
};
}
function removeImportsFromE2EFile(): PluginOption {
return {
name: 'remove-imports-from-e2e-file',
enforce: 'pre', // Ensure it runs before Vite's own transformers
transform(code: string, id: string) {
if (id.endsWith('e2e.ts')) {
const transformedCode = code
.split('\n')
.filter(line => !line.startsWith('import '))
.join('\n');
return {
code: transformedCode,
// Remove source map to clean vite warning:
// a plugin (remove-imports-from-e2e-file) was used to transform files, but didn't generate a sourcemap for the transformation
map: null,
};
}
},
};
}
export default process.env.UMD === 'true' ? umdConfig : esmConfig;