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
164
165
166
167
168
|
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
import react from '@vitejs/plugin-react';
import autoprefixer from 'autoprefixer';
import browserslistToEsbuild from 'browserslist-to-esbuild';
import path, { resolve } from 'path';
import postCssCalc from 'postcss-calc';
import { visualizer } from 'rollup-plugin-visualizer';
import tailwind from 'tailwindcss';
import { defineConfig, loadEnv } from 'vite';
import macrosPlugin from 'vite-plugin-babel-macros';
import requireTransform from 'vite-plugin-require-transform';
import babelConfig from './babel.config';
import { viteDevServerHtmlPlugin } from './config/vite-dev-server-html-plugin.mjs';
const DEFAULT_DEV_SERVER_PORT = 3000;
const DEFAULT_WS_PROXY_PORT = 3010;
const port = process.env.PORT || DEFAULT_DEV_SERVER_PORT;
const proxyTarget = (process.env.PROXY || 'http://localhost:9000').replace(/\/$/, '');
const isProxied = !proxyTarget.includes('localhost');
const isProduction = process.env.NODE_ENV === 'production';
const analyzeBundle = process.env.BUNDLE_ANALYSIS || false;
// https://vitejs.dev/config/
export default ({ mode }) => {
process.env = { ...process.env, ...loadEnv(mode, process.cwd()) };
return defineConfig({
experimental: {
// The WEB_CONTEXT string is replaced at runtime by the SQ backend web server
// (WebPagesCache.java) with the instance configured context path
renderBuiltUrl(filename, { hostId, hostType, type }) {
if (hostType === 'html') {
// All the files that are added to the (index.)html file are prefixed with WEB_CONTEXT/
return 'WEB_CONTEXT/' + filename;
} else if (hostType === 'js') {
// All the files that are lazy loaded from a js chunk are prefixed with the WEB_CONTEXT
// thanks to the __assetsPath function that's defined in index.html.
return { runtime: `window.__assetsPath(${JSON.stringify(filename)})` };
} else {
// Other files (css, images, etc.) are loaded relatively to the current url,
// automatically taking into accound the WEB_CONTEXT
return { relative: filename };
}
},
},
build: {
// reads from the package.json's browserslist to determine transforms required to support those browsers
target: browserslistToEsbuild(),
outDir: 'build/webapp',
rollupOptions: {
// we define all the places where a user can land that requires its own bundle entry point.
// we only have one main entry point which is the index.html for now
input: {
main: resolve(__dirname, 'index.html'),
},
output: {
// in order to override the default `build/webapp/assets/` directory we provide our own configuration
assetFileNames: '[ext]/[name]-[hash][extname]',
chunkFileNames: 'js/[name]-[hash].js',
entryFileNames: 'js/[name]-[hash].js',
// manual chunk splitting strategy. The packages will be split to its own js package
// We also have one more advantage with manual chunks which is caching. Unless we update
// the version of following packages, we would have caching on these chunks as the hash
// remains the same in successive builds as the package isn't changed
manualChunks: {
// vendor js chunk will contain only react dependencies
vendor: ['react', 'react-router-dom', 'react-dom'],
lodash: ['lodash/lodash.js'],
highlightjs: [
'highlight.js',
'highlightjs-apex',
'highlightjs-cobol',
'highlightjs-sap-abap',
],
},
},
plugins: [],
},
sourcemap: isProduction, // enable source maps for production
},
css: {
postcss: {
plugins: [tailwind('./tailwind.config.js'), autoprefixer, postCssCalc],
},
},
// by default vite doesn't pass along the process.env so we do it here. (for MSW and env code)
define: {
'process.env': {
NODE_ENV: process.env.NODE_ENV,
},
},
optimizeDeps: {
esbuildOptions: {
target: 'es2020',
},
},
commonjsOptions: {
// we don't want to wrap common js modules in { default: ... } when importing from an es module
defaultIsModuleExports: false,
},
esbuild: {
banner: '/*! licenses: /vendor.LICENSE.txt */',
legalComments: 'none',
// https://github.com/vitejs/vite/issues/8644#issuecomment-1159308803
logOverride: { 'this-is-undefined-in-esm': 'silent' },
},
plugins: [
// additional plugins to allow for the transformation of our existing code to what vite is expecting.
requireTransform({}),
react({
babel: babelConfig,
}),
// we use this to support `twin.macro` (macro is a term for a generic babel plugin used at runtime)
// More Info: https://www.npmjs.com/package/babel-plugin-macros
macrosPlugin(),
viteDevServerHtmlPlugin(),
analyzeBundle &&
visualizer({
filename: './build/bundle-analyzer.html',
template: 'treemap',
gzipSize: true,
}),
],
// This is the public folder we have to copy to public folder after build
publicDir: 'public',
resolve: {
alias: {
// src resolution is only applicable for html files and is only needed in vite and not
// in other configs - tsconfig and storybook
src: path.resolve(__dirname, 'src'),
'~sonar-aligned': path.resolve(__dirname, 'src/main/js/sonar-aligned'),
},
},
server: {
port,
proxy: {
'/api': {
target: proxyTarget,
changeOrigin: true,
},
'/static': {
target: proxyTarget,
changeOrigin: true,
},
},
},
});
};
|