-
-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathvite.config.js
More file actions
143 lines (137 loc) · 3.68 KB
/
vite.config.js
File metadata and controls
143 lines (137 loc) · 3.68 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
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
import { resolve } from 'path';
export default defineConfig(async () => {
const format = process.env.BUILD_FORMAT || 'es';
const isBrowserBundle = format === 'browser';
const viteFormat = isBrowserBundle ? 'es' : format;
let outputDir;
if (format === 'umd') {
outputDir = 'dist/umd';
} else if (format === 'cjs') {
outputDir = 'dist/cjs';
} else if (isBrowserBundle) {
outputDir = 'dist/browser';
} else {
outputDir = 'dist/es';
}
const externalDependencies = [
'@hashgraphonline/conversational-agent',
'@hashgraph/proto',
'@hashgraph/sdk',
'fetch-retry',
];
const plugins = [
dts({
insertTypesEntry: true,
include: ['src/**/*.ts'],
exclude: ['**/*.d.ts', '**/__tests__/**', '**/__mocks__/**'],
outputDir: outputDir,
}),
];
if (format === 'umd') {
const { nodePolyfills } = await import('vite-plugin-node-polyfills');
plugins.push(
nodePolyfills({
globals: {
Buffer: true,
global: true,
process: true,
},
protocolImports: true,
modules: {
buffer: true,
},
}),
);
}
return {
plugins,
build: {
outDir: outputDir,
lib: {
entry: resolve(
__dirname,
isBrowserBundle ? 'src/browser.ts' : 'src/index.ts',
),
name: format === 'umd' ? 'StandardsSDK' : undefined,
fileName: fmt => {
if (isBrowserBundle) {
return 'standards-sdk.browser.js';
}
return `standards-sdk.${fmt === 'cjs' ? 'cjs' : fmt + '.js'}`;
},
formats: [viteFormat],
},
rollupOptions: {
external: id => {
// Always externalize Node.js built-in modules
if (
id === 'fs' ||
id === 'path' ||
id === 'crypto' ||
id === 'stream' ||
id === 'buffer'
) {
return true;
}
if (id.startsWith('@kiloscribe/inscription-sdk')) {
return false;
}
if (format === 'umd') {
return false;
}
if (isBrowserBundle) {
return externalDependencies.some(
dep => id === dep || id.startsWith(dep + '/'),
);
}
return (
externalDependencies.some(
dep => id === dep || id.startsWith(dep + '/'),
) ||
(!id.startsWith('.') &&
!id.startsWith('/') &&
!id.includes(__dirname))
);
},
output:
format === 'cjs'
? {
exports: 'named',
format: 'cjs',
inlineDynamicImports: true,
manualChunks: undefined,
}
: {
globals: id => id,
preserveModules: format === 'es',
preserveModulesRoot: format === 'es' ? 'src' : undefined,
exports: 'named',
inlineDynamicImports: format === 'umd' || isBrowserBundle,
manualChunks: undefined,
name: format === 'umd' ? 'StandardsSDK' : undefined,
},
},
minify: 'terser',
sourcemap: true,
target: 'es2020',
},
define: {
VITE_BUILD_FORMAT: JSON.stringify(format),
...(format === 'cjs' ? { Buffer: 'globalThis.Buffer' } : {}),
},
resolve: {
alias: {
util: 'util',
},
},
ssr: {
noExternal: [
'@hashgraphonline/hashinal-wc',
'@kiloscribe/inscription-sdk',
],
external: externalDependencies,
},
};
});