-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvite.config.js
More file actions
87 lines (81 loc) · 2.52 KB
/
vite.config.js
File metadata and controls
87 lines (81 loc) · 2.52 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
import { defineConfig } from 'vite';
import { resolve } from 'node:path';
import fs from 'node:fs';
// Read package.json for banner information
const pkg = JSON.parse(fs.readFileSync('./package.json', 'utf8'));
const banner = `/**
* OpenCage Data Geocoding Control v${pkg.version} - ${new Date().toISOString().split('T')[0]}
* Copyright (c) ${new Date().getFullYear()}, ${pkg.author.name}
* ${pkg.author.email}
* ${pkg.author.url}
*
* Licensed under the ${pkg.license} license.
* Demo: ${pkg.homepage}
* Source: ${pkg.repository.url}
*/
`;
const footer = ``;
/**
* https://github.com/vitejs/vite/issues/8412#issuecomment-2377366474
*/
const RollupBannerPlugin = {
name: 'banner',
enforce: 'post',
generateBundle(options, bundle) {
for (const module of Object.values(bundle)) {
if (module.type === 'chunk') {
const shebang = module.code.match(/^#![^\n]*\n/);
if (shebang) {
module.code =
shebang[0] + banner + module.code.slice(shebang[0].length) + footer;
} else {
module.code = banner + module.code + footer;
}
}
}
},
};
export default defineConfig(({ mode }) => {
const isProduction = mode === 'production';
return {
build: {
outDir: 'dist',
emptyOutDir: false, // Don't empty on each build since we're doing multiple builds
lib: {
entry: resolve(__dirname, 'src/js/L.Control.OpenCageGeocoding.js'),
name: 'leaflet-control-opencage-geocoding',
formats: isProduction ? ['umd'] : ['umd', 'es'],
fileName: (format) => {
if (format === 'es') {
return `js/L.Control.OpenCageGeocoding.esm.js`;
}
return `js/L.Control.OpenCageGeocoding.${isProduction ? 'min' : 'dev'}.js`;
},
},
rollupOptions: {
external: ['leaflet'],
plugins: [RollupBannerPlugin],
output: {
globals: {
leaflet: 'L',
},
exports: 'named',
assetFileNames: (assetInfo) => {
if (assetInfo.name.endsWith('.css')) {
return `css/L.Control.OpenCageGeocoding.${isProduction ? 'min' : 'dev'}.css`;
}
if (assetInfo.name.match(/\.(png|gif|jpg|jpeg|svg)$/)) {
return 'images/[name][extname]';
}
return 'assets/[name][extname]';
},
},
},
minify: isProduction,
sourcemap: !isProduction,
copyPublicDir: false,
},
publicDir: false,
assetsInclude: ['**/*.png', '**/*.gif'],
};
});