-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathvite.config.ts
More file actions
58 lines (53 loc) · 1.51 KB
/
vite.config.ts
File metadata and controls
58 lines (53 loc) · 1.51 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
import { defineConfig, type Plugin } from 'vite';
import react from '@vitejs/plugin-react';
import type { OutputBundle, OutputChunk, OutputAsset } from 'rollup';
import { resolve } from 'path';
function inlineCSS(): Plugin {
return {
name: 'inline-css',
apply: 'build' as const,
enforce: 'post' as const,
generateBundle(_options: unknown, bundle: OutputBundle) {
const cssFiles = Object.keys(bundle).filter((key) => key.endsWith('.css'));
const jsFiles = Object.keys(bundle).filter((key) => key.endsWith('.js'));
if (cssFiles.length > 0 && jsFiles.length > 0) {
const cssFile = bundle[cssFiles[0]] as OutputAsset;
const jsFile = bundle[jsFiles[0]] as OutputChunk;
const cssContent = cssFile.source;
const cssInjectionCode = `
(function() {
const style = document.createElement('style');
style.textContent = ${JSON.stringify(cssContent)};
document.head.appendChild(style);
})();
`;
jsFile.code = cssInjectionCode + jsFile.code;
delete bundle[cssFiles[0]];
}
},
};
}
export default defineConfig({
plugins: [react(), inlineCSS()],
resolve: {
alias: {
'@': resolve(__dirname, 'src'),
},
},
build: {
lib: {
entry: 'src/main.tsx',
name: 'DreameVacuumMapCard',
fileName: 'dreame-vacuum-map-card',
formats: ['es'],
},
rollupOptions: {
output: {
inlineDynamicImports: true,
},
},
},
define: {
'process.env.NODE_ENV': JSON.stringify('production'),
},
});