-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathvite.config.ts
More file actions
163 lines (154 loc) · 4.88 KB
/
vite.config.ts
File metadata and controls
163 lines (154 loc) · 4.88 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
152
153
154
155
156
157
158
159
160
161
162
163
import inject from '@rollup/plugin-inject';
import autoprefixer from 'autoprefixer';
import cssnano from 'cssnano';
import path from 'path';
import postcssSortMediaQueries from 'postcss-sort-media-queries';
import {fileURLToPath} from 'url';
import {defineConfig, type UserConfig} from 'vite';
const allowedTargets = ['js', 'css'] as const;
type BuildTarget = (typeof allowedTargets)[number];
const isBuildTarget = (target: string | undefined): target is BuildTarget => {
return allowedTargets.includes(target as BuildTarget);
};
const __dirname = path.dirname(fileURLToPath(import.meta.url)) ?? '';
export default defineConfig(({mode}) => {
const {BUILD_TARGET} = process.env;
const target = isBuildTarget(BUILD_TARGET) ? BUILD_TARGET : 'js';
const isProduction = mode === 'production';
const isDevelopment = mode === 'development';
const IN_PATH = path.join(__dirname, 'src/main/resources/assets');
const OUT_PATH = path.join(__dirname, 'build/resources/main/assets');
const CONFIGS: Record<BuildTarget, UserConfig> = {
js: {
root: IN_PATH,
base: './',
build: {
outDir: OUT_PATH,
emptyOutDir: false,
target: 'ES2023',
minify: isProduction,
sourcemap: isDevelopment ? true : false,
...(isProduction && {
reportCompressedSize: true,
chunkSizeWarningLimit: 1000
}),
rollupOptions: {
plugins: [
inject({
$: 'jquery',
jQuery: 'jquery',
'window.jQuery': 'jquery'
}),
],
input: {
'js/home/bundle': path.join(IN_PATH, 'js/home/main.ts'),
'js/launcher/bundle': path.join(IN_PATH, 'js/launcher/main.ts'),
'js/widgets/shortcuts': path.join(IN_PATH, 'js/widgets/shortcuts.ts')
},
output: {
format: 'es',
entryFileNames: '[name].js',
chunkFileNames: 'js/chunks/[name]-[hash].js',
assetFileNames: (assetInfo) => {
const assetName = assetInfo.names?.[0] ?? '';
if (/\.(svg|png|jpg|gif|ico)$/.test(assetName)) {
return 'icons/[name][extname]';
}
if (/\.(woff|woff2|ttf|eot)$/.test(assetName)) {
return 'fonts/[name][extname]';
}
return '[name][extname]';
},
...(isProduction && {
compact: true,
generatedCode: {
constBindings: true
}
})
},
external: []
}
},
esbuild: {
minifyIdentifiers: false,
keepNames: true,
treeShaking: true,
...(isProduction && {
drop: ['console', 'debugger'],
legalComments: 'none'
})
},
resolve: {
alias: {
'@enonic/lib-admin-ui': path.join(__dirname, '.xp/dev/lib-admin-ui')
},
extensions: ['.ts', '.js']
},
...(isDevelopment && {
server: {
open: false,
hmr: true
},
clearScreen: false
}),
...(isProduction && {
logLevel: 'warn'
})
},
css: {
root: IN_PATH,
base: './',
build: {
outDir: OUT_PATH,
emptyOutDir: false,
minify: isProduction,
sourcemap: isDevelopment,
rollupOptions: {
input: {
'styles/home': path.join(IN_PATH, 'styles/home.less'),
'styles/launcher': path.join(IN_PATH, 'styles/launcher.less'),
'styles/widgets/shortcuts': path.join(IN_PATH, 'styles/widgets/shortcuts.less'),
'styles/widgets/youtube': path.join(IN_PATH, 'styles/widgets/youtube.less')
},
output: {
assetFileNames: (assetInfo) => {
const assetName = assetInfo.names?.[0] ?? '';
if (assetName.endsWith('.css')) {
const name = assetName.replace(/\.(less|css)$/, '');
return `${name}.css`;
}
if (/\.(svg|png|jpg|gif|ico)$/.test(assetName)) {
return 'icons/[name][extname]';
}
if (/\.(woff|woff2|ttf|eot)$/.test(assetName)) {
return 'fonts/[name][extname]';
}
return '[name][extname]';
}
}
}
},
resolve: {
alias: {
'~enonic-admin-artifacts': 'enonic-admin-artifacts/index.less'
},
extensions: ['.less', '.css']
},
css: {
preprocessorOptions: {
less: {
javascriptEnabled: true
}
},
postcss: {
plugins: [
autoprefixer(),
postcssSortMediaQueries({sort: 'desktop-first'}),
...(isProduction ? [cssnano({preset: ['default', {normalizeUrl: false}]})] : [])
]
}
}
}
};
return CONFIGS[target];
});