-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.ts
119 lines (115 loc) · 4.55 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
/**
* Second Vite build step for transpiling main and packaging into an extension
*/
import { defineConfig } from 'vite';
import path from 'path';
import { importManager } from 'rollup-plugin-import-manager';
import escapeStringRegexp from 'escape-string-regexp';
import { Target, viteStaticCopy } from 'vite-plugin-static-copy';
import { string as importString } from 'rollup-plugin-string';
import {
getInternalExtensions,
getExternalExtensions,
getFileExtensionByModuleFormat,
getStaticFileName,
getWebViewTsxPaths,
insertWebViewTempDir,
paranextProvidedModules,
sourceFolder,
staticFiles,
webViewGlob,
webViewTempGlob,
} from './vite.util';
// https://vitejs.dev/config/
const extensionConfig = defineConfig(async () => {
/** tsxWebViews - List of TypeScript WebView files transpiled in the first build step */
/** internalExtensions - List of all the internal extensions to build */
/** externalExtensions - List of all the external extensions to build */
const [tsxWebViews, internalExtensions, externalExtensions] = await Promise.all([
getWebViewTsxPaths(),
getInternalExtensions(),
getExternalExtensions(),
]);
return {
plugins: [
// Shared with https://github.com/paranext/paranext-extension-template/blob/main/vite/vite.config.ts
// Redirect WebView imports to their version built in the first build step
importManager({
// Need to include all files that could import WebViews
include: 'lib/**/*.{ts,tsx,js,jsx}',
units: tsxWebViews.map((webView) => {
const webViewInfo = path.parse(webView);
// Get the file name without the extension if it is tsx as tsx is inferred when importing
const webViewModuleName =
webViewInfo.ext === '.tsx' ? webViewInfo.name : webViewInfo.base;
return {
module:
// Match the whole module name, nothing more, nothing less
new RegExp(`${escapeStringRegexp(webViewModuleName)}$`),
actions: {
select: 'module',
rename: insertWebViewTempDir,
},
};
}),
}),
// Copy the internal extension static files from each extension folder like manifest.json
viteStaticCopy({
targets: internalExtensions.flatMap((extension) =>
staticFiles.map(
(staticFile): Target => ({
// vite-static-copy-plugin does not accept path.join here as it does not work with backslashes
src: `${sourceFolder}/${extension.dirName}/${getStaticFileName(
staticFile,
extension,
)}`,
dest: extension.dirName,
}),
),
),
}),
// Copy all external data provider files since they aren't part of normal extensions
viteStaticCopy({
targets: externalExtensions.flatMap<Target>((extension): Target => {
return {
src: `${sourceFolder}/${extension}/*.*`,
dest: `${extension}`,
};
}),
}),
// Shared with https://github.com/paranext/paranext-extension-template/blob/main/vite/vite.config.ts
// Import web view files as strings to pass on the papi
{
...importString({
include: [webViewGlob, webViewTempGlob],
}),
// importString plugin must be after any other plugins that need to transpile these files
enforce: 'post',
},
],
build: {
// This project is a library as it is being used in Paranext
lib: {
// The main entry file of each extension to build
entry: internalExtensions.map((extension) =>
path.resolve(__dirname, '..', extension.entryFilePath),
),
// The output file name for the extension (file extension is appended)
fileName: (moduleFormat, entryName) =>
path.join(entryName, `${entryName}.${getFileExtensionByModuleFormat(moduleFormat)}`),
// Output to cjs format as that's what Paranext supports. In production, es modules fail to
// shim over import and deliver papi for some reason.
formats: ['cjs'],
},
rollupOptions: {
// Do not bundle papi because it will be imported in Paranext
external: paranextProvidedModules,
},
// If watching, debounce building so we don't build off a build that was partially finished
watch: process.argv.includes('--watch') ? { buildDelay: 1000 } : null,
// Generate sourcemaps as separate files since VSCode can load them directly
sourcemap: true,
},
};
});
export default extensionConfig;