-
-
Notifications
You must be signed in to change notification settings - Fork 419
Expand file tree
/
Copy pathvite.config.js
More file actions
151 lines (139 loc) · 3.75 KB
/
Copy pathvite.config.js
File metadata and controls
151 lines (139 loc) · 3.75 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
import { defineConfig } from 'vite';
import * as path from 'path';
import * as fs from 'fs';
import glob from 'glob';
import nodePolyfills from '@rolldown/plugin-node-polyfills';
import { createVtkPlugins } from './Utilities/build/plugins.mjs';
import {
ignoreSourceFile,
flattenIndexEntry,
copyEsmAssetsPlugin,
copyUmdAssetsPlugin,
generateDtsReferencesPlugin,
cleanupAssetsPlugin,
injectEsmCssPlugin,
inlineUmdCssPlugin,
} from './Utilities/build/vtk-plugins.mjs';
const pkgJSON = JSON.parse(fs.readFileSync('./package.json', 'utf-8'));
const projectRoot = path.resolve('.');
const esmOutputDir = path.resolve('dist', 'esm');
const umdOutputDir = path.resolve('dist', 'umd');
const entryPoints = [
path.join('Sources', 'macros.js'),
path.join('Sources', 'vtk.js'),
path.join('Sources', 'favicon.js'),
...glob.sync('Sources/**/*.js').filter((file) => !ignoreSourceFile(file)),
];
const esmEntries = {};
entryPoints.forEach((entry) => {
esmEntries[entry.replace(/^Sources[/\\]/, '')] = entry;
});
const dependencies = Object.keys(pkgJSON.dependencies || {});
const peerDependencies = Object.keys(pkgJSON.peerDependencies || {});
function createSharedConfig() {
return {
resolve: {
alias: {
'vtk.js': projectRoot,
},
},
css: {
modules: {
localsConvention: 'camelCaseOnly',
},
},
worker: {
rollupOptions: {
output: {
sourcemap: false,
},
},
},
};
}
function createEsmConfig() {
return {
...createSharedConfig(),
build: {
outDir: esmOutputDir,
emptyOutDir: true,
minify: false,
sourcemap: true,
cssCodeSplit: true,
lib: {
entry: esmEntries,
formats: ['es'],
},
rollupOptions: {
input: esmEntries,
preserveEntrySignatures: 'strict',
external: [...dependencies, ...peerDependencies].map(
(name) => new RegExp(`^${name}(/|$)`)
),
output: {
format: 'es',
preserveModules: true,
preserveModulesRoot: 'Sources',
entryFileNames(chunkInfo) {
let name = chunkInfo.name;
if (name.endsWith('.js')) name = name.slice(0, -3);
return `${flattenIndexEntry(name)}.js`;
},
assetFileNames(assetInfo) {
const name = assetInfo.names?.[0] || assetInfo.name || '';
return name.replace(/^Sources\//, '');
},
},
},
},
plugins: [
{ ...nodePolyfills(), enforce: 'pre' },
...createVtkPlugins(),
injectEsmCssPlugin(),
copyEsmAssetsPlugin({ esmOutputDir }),
generateDtsReferencesPlugin({ esmOutputDir }),
cleanupAssetsPlugin(esmOutputDir),
],
};
}
function createUmdConfig() {
const umdEntry = path.resolve('Utilities/build/umd-entry.js');
return {
...createSharedConfig(),
build: {
outDir: umdOutputDir,
emptyOutDir: true,
sourcemap: true,
cssCodeSplit: false,
lib: {
entry: umdEntry,
name: 'vtk',
formats: ['umd'],
fileName: () => 'vtk.js',
},
rollupOptions: {
input: umdEntry,
output: {
exports: 'default',
},
},
},
plugins: [
{ ...nodePolyfills(), enforce: 'pre' },
...createVtkPlugins(),
inlineUmdCssPlugin(),
copyUmdAssetsPlugin({ umdOutputDir, projectRoot }),
cleanupAssetsPlugin(umdOutputDir),
],
};
}
const buildTarget = process.env.BUILD_TARGET || 'esm';
let selectedConfig;
if (buildTarget === 'esm') {
selectedConfig = createEsmConfig();
} else if (buildTarget === 'umd') {
selectedConfig = createUmdConfig();
} else {
throw new Error(`Unsupported BUILD_TARGET: ${buildTarget}`);
}
export default defineConfig(selectedConfig);