-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathvite.config.base.ts
More file actions
167 lines (160 loc) · 6.46 KB
/
vite.config.base.ts
File metadata and controls
167 lines (160 loc) · 6.46 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
164
165
166
167
/*
* SonarQube
* Copyright (C) 2009-2025 SonarSource Sàrl
* 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 { nxViteTsPaths } from '@nx/vite/plugins/nx-tsconfig-paths.plugin';
import legacy from '@vitejs/plugin-legacy';
import react from '@vitejs/plugin-react';
import autoprefixer from 'autoprefixer';
import path, { resolve } from 'path';
import postCssCalc from 'postcss-calc';
import license from 'rollup-plugin-license';
import { UserConfig } from 'vite';
import { analyzer } from 'vite-bundle-analyzer';
import macrosPlugin from 'vite-plugin-babel-macros';
import requireTransform from 'vite-plugin-require-transform';
import babelConfig from './babel.config';
import { ALLOWED_LICENSE_TEXT, ALLOWED_LICENSES, generateLicenseText } from './config/license';
import packageJson from './package.json';
export const DEFAULT_DEV_SERVER_PORT = 3000;
export const DEFAULT_WS_PROXY_PORT = 3010;
export const port = Number(process.env.PORT || DEFAULT_DEV_SERVER_PORT);
export const proxyTarget = (process.env.PROXY || 'http://localhost:9000').replace(/\/$/, '');
export const isProduction = process.env.NODE_ENV === 'production';
export const analyzeBundle = process.env.BUNDLE_ANALYSIS === 'true' || false;
export const projectRoot = process.cwd();
export const workspaceRoot = __dirname;
// https://vitejs.dev/config/
export const baseViteConfig = {
build: {
outDir: resolve(projectRoot, 'build/webapp'),
rollupOptions: {
// we define all the places where a user can land that requires its own bundle entry point.
// one main entry point by default that can be overriden by projects
input: {
main: resolve(projectRoot, 'index.html'),
},
output: {
// in order to override the default `build/webapp/assets/` directory we provide our own configuration
// to place content directly into `build/webapp/` directory
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'],
echoes: ['@sonarsource/echoes-react'],
datefns: ['date-fns'],
lodash: ['lodash/lodash.js'],
highlightjs: [
'highlight.js',
'highlightjs-apex',
'highlightjs-cobol',
'highlightjs-sap-abap',
],
},
},
plugins: [
// a tool used to concatenate all of our 3rd party licenses together for legal reasons
license({
thirdParty: {
allow: {
test: ({ license, licenseText }) =>
ALLOWED_LICENSES.includes(license ?? '') ||
ALLOWED_LICENSE_TEXT.some((text) => (licenseText ?? '').includes(text)),
failOnUnlicensed: false, // default is false. valid-url package has missing license
failOnViolation: true, // Fail if a dependency specifies a license that does not match requirements
},
output: {
// Output file into build/webapp directory which is included in the build output
file: path.join(projectRoot, 'build/webapp', 'vendor.LICENSE.txt'),
template(dependencies) {
return dependencies.map((dependency) => generateLicenseText(dependency)).join('\n');
},
},
},
}),
],
},
sourcemap: isProduction, // enable source maps for production
},
css: {
postcss: {
plugins: [autoprefixer, postCssCalc],
},
},
// By default vite doesn't pass along any env variable so we do it here. (for MSW and env code)
define: {
'process.env': {
NODE_ENV: process.env.NODE_ENV,
WITH_MOCK_API: process.env.WITH_MOCK_API,
},
},
optimizeDeps: {
esbuildOptions: {
target: 'es2020',
},
},
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: [
// setup additional vite aliases to resolve dependencies between mono-repo packages
nxViteTsPaths(),
// additional plugins to allow for the transformation of our existing code to what vite is expecting.
requireTransform({}),
// create a polyfill chunk for browsers we support that are missing modern JS features
legacy({
modernTargets: packageJson.browserslist,
polyfills: false,
modernPolyfills: true,
renderLegacyChunks: false,
}),
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(),
analyzeBundle &&
analyzer({
fileName: resolve(projectRoot, 'build/bundle-metrics.json'),
analyzerMode: 'json',
}),
],
// This is the static folder we have to copy to build/webapp 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, jest
src: path.resolve(projectRoot, 'src'),
},
},
server: {
allowedHosts: ['.ngrok-free.app', '.ngrok.io'],
port,
},
} satisfies UserConfig;