forked from enketo/enketo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vite.config.ts
167 lines (153 loc) · 5.42 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
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
import { resolve } from 'path';
import type { LibraryFormats } from 'vite';
import type { UserConfig } from 'vitest/config';
import { defineConfig } from 'vitest/config';
import config from './config/config.json';
import { define, TARGET_ENV } from './config/build.shared';
export default defineConfig(async () => {
const isWeb = TARGET_ENV === 'web';
const entryNames = isWeb ? ['transformer'] : ['app', 'transformer'];
const entry = entryNames.map((name) =>
resolve(__dirname, `./src/${name}.ts`)
);
const formats = (isWeb ? ['es'] : ['es', 'cjs']) satisfies LibraryFormats[];
const emptyOutDir = process.env.EMPTY_OUT_DIR === 'true';
const external = [
'body-parser',
'crypto',
'css.escape',
'express',
'fs',
'libxslt',
'module',
'node1-libxmljsmt-myh',
'path',
'undici',
'url',
'vite-node',
'vite',
];
const webDeps = ['language-tags', 'string-direction'];
if (!isWeb) {
external.push(...webDeps);
}
const input = isWeb
? ['./src/transformer.ts']
: ['./src/app.ts', './src/transformer.ts', './config/config.json'];
const isViteNodeRuntime = process.argv.some((arg) =>
arg.endsWith('/vite-node')
);
const isViteRuntime =
isViteNodeRuntime ||
process.argv.some((arg) => arg.endsWith(`/vitest`));
/**
* Use Vite's default {@link https://vitejs.dev/config/build-options.html#build-target `modules`} target for Vite runtimes (app.js, test) and web.
*/
const target = isWeb || isViteRuntime ? 'modules' : 'node14';
const alias = isWeb
? [
{
find: /^libxslt$/,
replacement: './src/dom/web/libxslt.ts',
},
{
find: /^enketo-transformer\/dom$/,
replacement: './src/dom/web/index.ts',
},
{
find: /^\/@fs\/(.*)/,
replacement: '$1',
},
]
: [
{
find: /^enketo-transformer\/dom$/,
replacement: './src/dom/node/index.ts',
},
];
const baseName = isWeb ? 'enketo-transformer/web' : 'enketo-transformer';
return {
assetsInclude: ['**/*.xml', '**/*.xsl'],
build: {
lib: {
entry,
formats,
name: baseName,
// Note: this is only called for Node builds.
fileName(format, entryName) {
const extension = format === 'es' ? '.js' : '.cjs';
return `${baseName}/${entryName.replace(
'src/',
''
)}${extension}`;
},
},
emptyOutDir,
minify: false,
outDir: 'dist',
rollupOptions: {
external,
input,
output: {
// This suppresses a warning for modules with both named and
// default exporrs when building for CommonJS (UMD in our
// current build). It's safe to suppress this warning because we
// have explicit tests ensuring both the default and named
// exports are consistent with the existing public API.
exports: 'named',
preserveModules: !isWeb,
},
treeshake: true,
},
sourcemap: true,
target,
},
define,
esbuild: {
define,
format: 'esm',
sourcemap: true,
},
optimizeDeps: {
exclude: external,
},
resolve: { alias },
server: {
port: config.port,
},
test: {
// Vitest uses thread-based concurrency by defualt.
// While this would significantly improve the speed
// of test runs, native Node extensions using N-API
// are often not thread safe. In this case, that
// means we cannot use concurrency for testing
// functionality which depends on libxmljs/libxslt.
//
// In Vitest 0.29.0+, the `singleThread` option must
// be set to achieve the same behavior as `threads`
// did previously. The `threads` option must *also*
// be set to prevent spinning up threads which may
// fail to close when tests complete.
threads: false,
singleThread: true,
chaiConfig: {
// Preserves previous truncation in snapshots.
// This isn't ideal, but it helps to prevent
// Vitest from creating an enormous diff of
// reordered snapshots which otherwise pass.
truncateThreshold: 40,
},
coverage: {
provider: 'istanbul',
include: ['src/**/*.ts'],
reporter: ['html', 'text-summary', 'json'],
reportsDirectory: './test-coverage',
},
globals: false,
globalSetup: isWeb ? 'test/web/setup.ts' : undefined,
include: ['test/**/*.spec.ts'],
reporters: 'verbose',
sequence: { shuffle: true },
},
} satisfies UserConfig;
});