Repository navigation
Expand file tree
/
Copy pathvite.config.esm.js
More file actions
106 lines (91 loc) · 3.9 KB
/
Copy pathvite.config.esm.js
File metadata and controls
106 lines (91 loc) · 3.9 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
import { defineConfig } from 'vite';
import { resolve, dirname } from 'node:path';
import glob from 'glob';
import { fileURLToPath } from 'node:url';
import { logger, sharedOnwarn } from './vite/shared.js';
import { rewriteEditorWorkerImport } from './vite/plugins/rewrite-editor-worker-import.js';
import { fixCrossChunkPaths } from './vite/plugins/fix-cross-chunk-paths.js';
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
// Dynamically discover all plugins and presets (using glob v7 API)
const pluginFiles = glob.sync('src/plugins/*/index.{js,ts}');
const presetFiles = glob.sync('src/presets/*/index.{js,ts}');
const entries = {
'swagger-editor': resolve(__dirname, 'src/App.tsx'),
};
pluginFiles.forEach((file) => {
const key = file.replace(/^src\//, '').replace(/\.(js|ts)$/, '');
entries[key] = resolve(__dirname, file);
});
presetFiles.forEach((file) => {
const key = file.replace(/^src\//, '').replace(/\.(js|ts)$/, '');
entries[key] = resolve(__dirname, file);
});
// Main ESM lib — plugins and presets as separate entry points, workers excluded.
// Workers are built separately as self-contained ESM bundles (see below).
export const mainConfig = defineConfig({
configFile: false,
customLogger: logger,
mode: 'production',
publicDir: false,
plugins: [rewriteEditorWorkerImport()],
build: {
lib: {
entry: entries,
formats: ['es'],
fileName: (format, entryName) => `${entryName}.js`,
},
outDir: 'dist/esm',
sourcemap: true,
emptyOutDir: true,
cssCodeSplit: false,
rollupOptions: {
// id — raw import specifier or resolved path
// isResolved — true only after Vite has resolved the path; bare specifiers
// arrive here with isResolved:false and their original form.
external: (id, _importer, isResolved) => {
// Keep swagger-ui CSS bundled — guard both the bare specifier and the resolved
// absolute path, otherwise the node_modules safety net below externalizes it
// causing the CI runner's absolute path to be baked into the built ESM file
if (id === 'swagger-ui-react/swagger-ui.css') return false;
if (isResolved && id.includes('swagger-ui-react') && id.endsWith('swagger-ui.css'))
return false;
// Rollup checks external() BEFORE calling resolveId hooks for bare specifiers.
// Allow ?worker imports through so rewriteEditorWorkerImport can intercept them.
if (id.endsWith('?worker')) return false;
// Plugin/preset cross-references — externalized so each chunk stays separate
if (/^plugins\/.+\/index/.test(id) || /^presets\/.+\/index/.test(id)) return true;
// Safety net: already-resolved absolute node_modules paths
if (isResolved && id.includes('node_modules')) return true;
// Bare npm specifiers (not relative/absolute/virtual) — externalize BEFORE
// Vite resolves them to internal file paths like reselect/dist/reselect.mjs.
// This preserves canonical package names in the output.
if (!isResolved && !id.startsWith('.') && !id.startsWith('/') && !id.startsWith('\0')) {
return true;
}
return false;
},
output: {
entryFileNames: '[name].js',
assetFileNames: 'swagger-editor.css',
// Only plugin/preset cross-references need path rewriting.
// Bare npm specifiers are already externalized with their canonical names.
paths: (id) => {
if (id.startsWith('plugins/') || id.startsWith('presets/')) {
return id.endsWith('.js') ? `./${id}` : `./${id}.js`;
}
return id;
},
},
plugins: [fixCrossChunkPaths()],
onwarn: sharedOnwarn,
},
},
resolve: {
alias: {
plugins: resolve(__dirname, 'src/plugins'),
presets: resolve(__dirname, 'src/presets'),
},
},
});
export default mainConfig;