-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvite.optional.config.js
More file actions
62 lines (60 loc) · 2.62 KB
/
Copy pathvite.optional.config.js
File metadata and controls
62 lines (60 loc) · 2.62 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
import { defineConfig } from 'vite';
import { cpSync, mkdirSync, readFileSync } from 'node:fs';
import { resolve } from 'node:path';
import terser from '@rollup/plugin-terser';
const pkg = JSON.parse(readFileSync(resolve('package.json'), 'utf8'));
const packageDist = `dist/${pkg.name.split('/').pop()}`;
const bundleDist = `${packageDist}/dist`;
const isDebug = process.env.npm_config_mode === 'debug';
const name = isDebug ? 'lume-debug' : 'lume-metrics';
const entry = isDebug ? 'src/lume-debug.js' : 'src/lume-metrics.js';
const globalName = isDebug ? 'LumeDebug' : 'LumeMetrics';
const external = isDebug ? ['@staticcanvas/logcad'] : [];
/** Vite plugin that copies optional bundles into the documentation web utility. */
const stageOptionalAssets = {
name: 'stage-optional-web-assets',
closeBundle() {
const destination = resolve('docs/static/webutil/libs');
mkdirSync(destination, { recursive: true });
cpSync(resolve(bundleDist), destination, { recursive: true });
},
};
/**
* Builds one Rollup output configuration for an optional bundle format.
* @param {string} format Rollup output format.
* @param {string} fileName Generated file name.
* @param {Array<object>} [plugins=[]] Rollup plugins.
* @returns {object} Rollup output configuration.
*/
const output = (format, fileName, plugins = []) => ({
format,
...(format === 'umd' ? { name: globalName } : {}),
...(format === 'umd' ? { exports: 'named' } : {}),
dir: bundleDist,
entryFileNames: fileName,
plugins,
...(external.length ? { globals: { '@staticcanvas/logcad': 'logcad' } } : {}),
});
export default defineConfig({
build: {
outDir: 'dist',
emptyOutDir: false,
copyPublicDir: false,
sourcemap: true,
target: 'es2015',
lib: { entry: resolve(entry), name: globalName, fileName: name },
minify: false,
rollupOptions: {
external,
output: [
output('umd', `${name}.js`, [terser({ format: { comments: true, preamble: `/*! ${name} | ${pkg.license} */` } })]),
output('umd', `${name}.min.js`, [terser({ format: { comments: /^!/ }, compress: true, mangle: true })]),
output('umd', `${name}.cjs`, [terser({ format: { comments: true, preamble: `/*! ${name} | ${pkg.license} */` } })]),
output('es', `${name}.mjs`, [terser({ format: { comments: true, preamble: `/*! ${name} | ${pkg.license} */` } })]),
output('es', `${name}.esm.js`, [terser({ format: { comments: true, preamble: `/*! ${name} | ${pkg.license} */` } })]),
output('es', `${name}.esm.min.js`, [terser({ format: { comments: /^!/ }, compress: true, mangle: true })]),
],
},
},
plugins: [stageOptionalAssets],
});