-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
130 lines (119 loc) · 3.97 KB
/
vite.config.ts
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
import MagicString from 'magic-string';
import { dirname, resolve } from 'node:path';
import { fileURLToPath } from 'node:url';
import type { Plugin } from 'vite';
import { defineConfig } from 'vite';
import react from '@vitejs/plugin-react';
import preserveDirectives from 'rollup-preserve-directives';
import dts from 'vite-plugin-dts';
import { libInjectCss } from 'vite-plugin-lib-inject-css';
import tsConfigPaths from 'vite-tsconfig-paths';
const __dirname = dirname(fileURLToPath(import.meta.url));
/**
* libInjectCss places it at the top of the file, this is a
* somewhat hacky way to make sure any potential `use client`
* directives are hoisted to the top of the file.
*/
function hoistUseClient(): Plugin {
let resolvedConfig;
return {
// TODO: Turn this into a proper plugin or figure out a better way to do this.
name: 'nordcom:use-client',
apply: 'build',
enforce: 'post',
configResolved(config) {
resolvedConfig = config;
},
generateBundle({ format }, bundle) {
for (const chunk of Object.values(bundle)) {
if (
chunk.type !== 'chunk' ||
!chunk.viteMetadata?.importedCss.size ||
!chunk.code.includes('use client')
) {
continue;
}
const ms = new MagicString(chunk.code);
ms.replaceAll(/['"]use client['"];?\n?/g, '');
ms.prepend(`'use client';\n`);
chunk.code = ms.toString();
if (resolvedConfig.build.sourcemap) {
chunk.map = ms.generateMap({ hires: 'boundary' });
}
}
}
};
}
export default defineConfig({
root: resolve(__dirname),
build: {
copyPublicDir: false,
cssCodeSplit: true,
cssMinify: false,
emptyOutDir: true,
minify: false,
outDir: 'dist',
sourcemap: true,
target: 'esnext',
rollupOptions: {
external: [/^@nordcom\/nordstar-/],
treeshake: false,
output: {
assetFileNames: ({ name }) => {
if (name && !['.css'].every((ext) => !name.includes(ext))) {
return 'index.css';
}
return 'assets/[name][extname]';
},
intro: (chunk) => {
if (chunk.isEntry && chunk.facadeModuleId?.endsWith('.tsx')) {
return `import 'react';`;
}
return '';
},
chunkFileNames: 'chunks/[name].[hash].js',
entryFileNames: '[name].js',
generatedCode: {
arrowFunctions: true,
constBindings: true,
objectShorthand: true,
preset: 'es2015',
reservedNamesAsProps: true,
symbols: true
},
esModule: true,
exports: 'named',
format: 'esm',
freeze: false,
hoistTransitiveImports: false,
interop: 'esModule',
minifyInternalExports: true,
preserveModules: false,
sourcemapExcludeSources: false,
strict: true,
validate: true
},
plugins: [preserveDirectives()]
}
},
esbuild: {
jsx: 'automatic'
},
plugins: [
react(),
tsConfigPaths(),
libInjectCss(),
hoistUseClient(),
dts({
clearPureImport: false,
copyDtsFiles: true,
entryRoot: 'src',
include: ['**/src'],
insertTypesEntry: true,
logLevel: 'info',
rollupTypes: false,
staticImport: true,
tsconfigPath: `${__dirname}/tsconfig.json`
})
]
});