-
Notifications
You must be signed in to change notification settings - Fork 123
Expand file tree
/
Copy pathvite.config.js
More file actions
208 lines (203 loc) · 8.38 KB
/
Copy pathvite.config.js
File metadata and controls
208 lines (203 loc) · 8.38 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
import { defineConfig, loadEnv } from 'vite';
import react from '@vitejs/plugin-react';
import { visualizer } from 'rollup-plugin-visualizer';
import path from 'path';
import { fileURLToPath } from 'url';
const __dirname = path.dirname(fileURLToPath(import.meta.url));
const analyze = process.env.ANALYZE === 'true';
// CommonJS packages in the tree whose `exports.default = Component` +
// `__esModule: true` shape doesn't round-trip cleanly through Vite/esbuild's
// auto-interop. `import X from 'pkg'` hands back the whole namespace object
// and `import { Y } from 'pkg'` resolves to undefined, because the optimised
// bundle only emits `export default require_X()` with no named re-exports.
//
// Listing them here enables the rewrite plugin below, which converts every
// `import X from 'pkg'` and `import { Y } from 'pkg'` at the consumer side
// into a namespace-destructure that handles both the plain-CJS
// (`module.exports = X`) and Babel-CJS (`exports.default = X`) shapes
// uniformly. Source files stay authored with the natural import syntax.
const cjsInteropPackages = [
'react-copy-to-clipboard',
'react-countup',
'react-data-table-component',
'react-perfect-scrollbar',
'react-slick',
];
const cjsInteropPlugin = (pkgs) => {
const specifierSet = new Set(pkgs);
const nsVar = (spec) => `__cjsns_${spec.replace(/[^a-z0-9]/gi, '_')}`;
const modVar = (spec) => `__cjsmod_${spec.replace(/[^a-z0-9]/gi, '_')}`;
// Matches:
// import Foo from 'pkg';
// import { A, B as C } from 'pkg';
// import Foo, { A } from 'pkg';
const re = /^import\s+([^;]+?)\s+from\s+['"]([^'"]+)['"];?/gm;
return {
name: 'architectui:cjs-interop',
enforce: 'pre',
transform(code, id) {
if (id.includes('/node_modules/')) return null;
if (!/\.(jsx?|tsx?)$/.test(id.split('?')[0])) return null;
if (!pkgs.some((p) => code.includes(p))) return null;
let out = '';
let lastIndex = 0;
let hit = false;
re.lastIndex = 0;
for (const m of code.matchAll(re)) {
const [full, clause, spec] = m;
if (!specifierSet.has(spec)) continue;
hit = true;
const start = m.index;
out += code.slice(lastIndex, start);
const ns = nsVar(spec);
// `mod` covers the unwrapped CJS shape for default-import resolution
// (import X from 'pkg' → X = exports.default ?? module.exports). For
// named-import resolution we spread ns.default into ns so both the
// Vite-wrapped CJS case (`ns = { default: { foo, bar } }`) and a
// mixed-exports ESM case (`ns = { foo, default: Comp }`) produce the
// right value for `{ foo } = ...`.
const mod = modVar(spec);
const namedSrc = `__cjsnamed_${spec.replace(/[^a-z0-9]/gi, '_')}`;
const parts = [
`import * as ${ns} from '${spec}';`,
`const ${mod} = ${ns} && ${ns}.default !== undefined ? ${ns}.default : ${ns};`,
`const ${namedSrc} = Object.assign({}, ${mod}, ${ns});`,
];
const trimmed = clause.trim();
// Split default + named parts: `Foo` / `{ A, B }` / `Foo, { A, B }`
let defaultName = null;
let named = null;
const braceIdx = trimmed.indexOf('{');
if (braceIdx === -1) {
defaultName = trimmed;
} else {
const defaultPart = trimmed.slice(0, braceIdx).replace(',', '').trim();
if (defaultPart) defaultName = defaultPart;
const end = trimmed.indexOf('}', braceIdx);
named = trimmed.slice(braceIdx + 1, end).trim();
}
if (defaultName) {
parts.push(
`const ${defaultName} = ${mod} && ${mod}.__esModule && ${mod}.default !== undefined ? ${mod}.default : ${mod};`
);
}
if (named) {
// Handle `A as B` → `A: B`
const rewritten = named
.split(',')
.map((n) => n.trim())
.filter(Boolean)
.map((n) => {
const asMatch = n.match(/^(\w+)\s+as\s+(\w+)$/);
return asMatch ? `${asMatch[1]}: ${asMatch[2]}` : n;
})
.join(', ');
parts.push(`const { ${rewritten} } = ${namedSrc};`);
}
out += parts.join('\n');
lastIndex = start + full.length;
}
if (!hit) return null;
out += code.slice(lastIndex);
return { code: out, map: null };
},
};
};
export default defineConfig(({ mode }) => {
const env = loadEnv(mode, process.cwd(), '');
const port = Number(env.VITE_PORT) || 3001;
const base = env.VITE_BASE || './';
return {
plugins: [
cjsInteropPlugin(cjsInteropPackages),
react(),
analyze &&
visualizer({
filename: 'build/stats.html',
open: true,
gzipSize: true,
brotliSize: true,
template: 'treemap',
}),
].filter(Boolean),
resolve: {
alias: {
src: path.resolve(__dirname, './src'),
// `react-loaders` (3.0.1, last published 2018) is a webpack-bundled UMD
// that produces default + named exports in a shape the optimised
// ESM output can't re-export cleanly. Its only job is to emit the
// DOM shape `loaders.css` animations expect, which is maybe twenty
// lines of JSX. Route the package specifier to a local component
// that does exactly that so the dep can drop out of the tree.
'react-loaders': path.resolve(__dirname, './src/components/Loader.jsx'),
// Vite 8 / rolldown wraps internmap's ESM `export class InternMap` in a
// lazy `__esmMin(cb)` initializer but d3-array's `ordinal.js` runs
// `new InternMap()` eagerly inside another wrapper — the lazy cb never
// fires before use, so production throws "InternMap is not a
// constructor". The dist/ UMD build defines the class synchronously
// and avoids the wrapping.
internmap: path.resolve(__dirname, './node_modules/internmap/dist/internmap.js'),
},
dedupe: ['react', 'react-dom', 'react/jsx-runtime', 'react/jsx-dev-runtime'],
},
optimizeDeps: {
include: [
'react',
'react-dom',
'react/jsx-runtime',
'react/jsx-dev-runtime',
// Force a proper bundle so the `InternMap` class doesn't get wrapped
// in a lazy initializer that leaves it undefined at the point
// recharts calls `new InternMap()`. (recharts bundles its own d3-array
// via victory-vendor, so d3-array isn't a top-level package to include.)
'internmap',
],
},
server: {
port,
host: true,
open: true,
},
build: {
outDir: 'build',
sourcemap: true,
target: 'es2020',
chunkSizeWarningLimit: 600,
rollupOptions: {
output: {
manualChunks: (id) => {
if (!id.includes('node_modules')) return;
if (/[\\/]node_modules[\\/](react|react-dom|react-router|react-router-dom|scheduler)[\\/]/.test(id))
return 'vendor-react';
if (/[\\/]node_modules[\\/](@reduxjs|redux|react-redux|immer|reselect)[\\/]/.test(id))
return 'vendor-redux';
if (/[\\/]node_modules[\\/](reactstrap|react-bootstrap|bootstrap|@restart)[\\/]/.test(id))
return 'vendor-bootstrap';
if (/[\\/]node_modules[\\/]@fortawesome[\\/]/.test(id)) return 'vendor-fontawesome';
if (/[\\/]node_modules[\\/](apexcharts|react-apexcharts)[\\/]/.test(id))
return 'vendor-apexcharts';
if (/[\\/]node_modules[\\/](chart\.js|react-chartjs-2)[\\/]/.test(id))
return 'vendor-chartjs';
if (/[\\/]node_modules[\\/]recharts[\\/]/.test(id)) return 'vendor-recharts';
if (/[\\/]node_modules[\\/](leaflet|react-leaflet)[\\/]/.test(id)) return 'vendor-leaflet';
if (/[\\/]node_modules[\\/](d3|d3-[^\\/]+)[\\/]/.test(id)) return 'vendor-d3';
if (/[\\/]node_modules[\\/](moment|date-fns|date-arithmetic)[\\/]/.test(id))
return 'vendor-dates';
if (/[\\/]node_modules[\\/](framer-motion|react-animations)[\\/]/.test(id))
return 'vendor-motion';
if (/[\\/]node_modules[\\/]styled-components[\\/]/.test(id)) return 'vendor-styled';
},
},
},
},
base,
css: {
preprocessorOptions: {
scss: {
quietDeps: true,
silenceDeprecations: ['import', 'global-builtin'],
},
},
},
};
});