-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.backend.config.ts
135 lines (120 loc) · 4.81 KB
/
vite.backend.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
131
132
133
134
135
import { default as ClosePlugin } from './vite-plugin-close-and-copy.js'
import { Drop } from 'esbuild'
import { UserConfig } from 'vite'
import { builtinModules } from 'module'
import { filterDependencies, getDependencies } from '../dependencies.js'
import { getLogger } from '@prosopo/common'
import { nodeResolve } from '@rollup/plugin-node-resolve'
import { nodejsPolarsDirnamePlugin } from './NodejsPolarsDirnamePlugin.js'
import { nodejsPolarsNativeFilePlugin } from './NodejsPolarsNativeFilePlugin.js'
import { wasm } from '@rollup/plugin-wasm'
import VitePluginFixAbsoluteImports from './vite-plugin-fix-absolute-imports.js'
import css from 'rollup-plugin-import-css'
import path from 'path'
const logger = getLogger(`Info`, `vite.backend.config.js`)
export default async function (
packageName: string,
packageVersion: string,
bundleName: string,
packageDir: string,
entry: string,
command?: string,
mode?: string,
optionalBaseDir = '../..'
): Promise<UserConfig> {
const isProduction = mode === 'production'
// Get all dependencies of the current package
const { dependencies: deps, optionalPeerDependencies } = await getDependencies(packageName, true)
// Assuming node_modules are at the root of the workspace
const baseDir = path.resolve(optionalBaseDir)
const nodeModulesDir = path.resolve(baseDir, 'node_modules')
// Output directory is relative to directory of the package
const outDir = path.resolve(packageDir, 'dist/bundle')
// Get rid of any dependencies we don't want to bundle
const { external, internal } = filterDependencies(deps, ['aws', 'webpack', 'vite', 'eslint'])
// Add the node builtins (path, fs, os, etc.) to the external list
const allExternal = [
...builtinModules,
...builtinModules.map((m) => `node:${m}`),
...external,
...optionalPeerDependencies,
]
logger.info(`Bundling. ${JSON.stringify(internal.slice(0, 10), null, 2)}... ${internal.length} deps`)
const nodeJsNodeFileToCopy = path.resolve(
nodeModulesDir,
'./nodejs-polars-linux-x64-gnu/nodejs-polars.linux-x64-gnu.node'
)
logger.info(`.node files to copy ${nodeJsNodeFileToCopy}`)
const define = {
'process.env.WS_NO_BUFFER_UTIL': 'true',
'process.env.WS_NO_UTF_8_VALIDATE': 'true',
'process.env.PROSOPO_PACKAGE_VERSION': JSON.stringify(packageVersion),
...(process.env.PROSOPO_DEFAULT_ENVIRONMENT && {
'process.env.PROSOPO_DEFAULT_ENVIRONMENT': JSON.stringify(process.env.PROSOPO_DEFAULT_ENVIRONMENT),
}),
...(process.env.PROSOPO_DEFAULT_NETWORK && {
'process.env.PROSOPO_DEFAULT_NETWORK': JSON.stringify(process.env.PROSOPO_DEFAULT_NETWORK),
}),
}
logger.info(`Defined vars ${JSON.stringify(define, null, 2)}`)
const entryAbsolute = path.resolve(packageDir, entry)
// drop console logs if in production mode
const drop: Drop[] | undefined = mode === 'production' ? ['console', 'debugger'] : undefined
// a list of the node files to be handled. Starts from root dir
const nodeFiles = [nodeJsNodeFileToCopy]
return {
ssr: {
noExternal: internal,
external: allExternal,
},
optimizeDeps: {
include: ['linked-dep', 'node_modules'],
esbuildOptions: {
loader: {
'.node': 'file',
},
},
},
esbuild: {
platform: 'node',
target: 'node18',
drop,
legalComments: 'none',
},
define,
build: {
outDir,
minify: isProduction,
ssr: true,
target: 'node18',
lib: {
entry: entryAbsolute,
name: bundleName,
fileName: `${bundleName}.[name].bundle.js`,
formats: ['es'],
},
modulePreload: { polyfill: false },
rollupOptions: {
treeshake: 'smallest',
external: allExternal,
watch: false,
output: {
entryFileNames: `${bundleName}.[name].bundle.js`,
},
plugins: [
css(),
wasm(),
nodeResolve(),
nodejsPolarsDirnamePlugin(logger),
nodejsPolarsNativeFilePlugin(logger, nodeFiles, outDir),
],
},
},
plugins: [
// plugin to replace stuff like import blah from string_encoder/lib/string_encoder.js with import blah from string_encoder
VitePluginFixAbsoluteImports(),
// plugin to close the bundle after build if not in serve mode
command !== 'serve' ? ClosePlugin() : undefined,
],
}
}