-
Notifications
You must be signed in to change notification settings - Fork 293
/
Copy pathvite.config.ts
104 lines (98 loc) · 2.9 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
/// <reference types="vitest" />
import { join } from 'path';
import { defineConfig } from 'vite';
import dts from 'vite-plugin-dts';
// eslint-disable-next-line @nx/enforce-module-boundaries
import { viteTsConfigPaths } from '../../vite-extensions/vite-ts-config-paths';
// eslint-disable-next-line @nx/enforce-module-boundaries
import { getExternalModules } from '../../vite-extensions/vite-external-modules';
export default defineConfig(({ command }) => {
return {
cacheDir: '../../../node_modules/.vite/php-wasm',
plugins: [
viteTsConfigPaths({
root: '../../../',
}),
dts({
entryRoot: 'src',
tsconfigPath: join(__dirname, 'tsconfig.lib.json'),
pathsToAliases: false,
}),
{
name: 'ignore-wasm-imports',
load(id: string): any {
if (id?.endsWith('.wasm')) {
return {
code: 'export default {}',
map: null,
};
}
},
},
/**
* Vite can't extract static asset in the library mode:
* https://github.com/vitejs/vite/issues/3295
*
* This workaround replaces the actual php_5_6.js modules paths used
* in the dev mode with their filenames. Then, the filenames are marked
* as external further down in this config. As a result, the final
* bundle contains literal `import('php_5_6.js')` and
* `import('php_5_6.wasm')` statements which allows the consumers to use
* their own loaders.
*
* This keeps the dev mode working AND avoids inlining 5mb of
* wasm via base64 in the final bundle.
*/
{
name: 'preserve-php-loaders-imports',
resolveDynamicImport(specifier): string | void {
if (
command === 'build' &&
typeof specifier === 'string' &&
specifier.match(/php_\d_\d\.js$/)
) {
/**
* The ../ is weird but necessary to make the final build say
* import("./php/jspi/php_8_2.js")
* and not
* import("php/jspi/php_8_2.js")
*
* The slice(-3) will ensure the 'php/jspi/`
* portion of the path is preserved.
*/
return '../' + specifier.split('/').slice(-3).join('/');
}
},
},
],
// Configuration for building your library.
// See: https://vitejs.dev/guide/build.html#library-mode
build: {
lib: {
// Could also be a dictionary or array of multiple entry points.
entry: 'src/index.ts',
name: 'php-wasm-web',
fileName: 'index',
formats: ['es'],
},
sourcemap: true,
rollupOptions: {
// Don't bundle the PHP loaders in the final build. See
// the preserve-php-loaders-imports plugin above.
external: [/php_\d_\d.js$/, ...getExternalModules()],
output: {
// Ensure the PHP loaders are not hashed in the final build.
entryFileNames: '[name].js',
},
},
},
test: {
globals: true,
cache: {
dir: '../../../node_modules/.vitest',
},
environment: 'node',
include: ['src/**/*.{test,spec}.{js,mjs,cjs,ts,mts,cts,jsx,tsx}'],
},
};
});