-
Notifications
You must be signed in to change notification settings - Fork 2
/
vite.config.ts
98 lines (95 loc) · 3.28 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
/**
* 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 { string as importString } from "rollup-plugin-string";
import {
paranextProvidedModules,
webViewGlob,
webViewTsxImportRegex,
webViewTempDir,
getWebViewTsxPaths,
insertWebViewTempDir,
webViewTempGlob,
getFileExtensionByModuleFormat,
} from "./vite.util";
/** List of TypeScript WebView files transpiled in the first build step */
const tsxWebViews = getWebViewTsxPaths();
// https://vitejs.dev/config/
export default defineConfig({
plugins: [
// Redirect WebView imports to their version built in the first build step
importManager({
// Need to include all files that could import WebViews
include: "**/*.{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,
},
};
}),
}),
// Import web view files as strings to pass on the papi
// importString plugin must be after any other plugins that need to transpile these files
{
...importString({
include: [webViewGlob, webViewTempGlob],
}),
enforce: "post",
},
],
build: {
// This project is a library as it is being used in Paranext
lib: {
// The main entry file of the extension
entry: path.resolve(__dirname, "../lib/main.ts"),
// The output file name for the extension (file extension is appended)
fileName: (moduleFormat, entryName) =>
`paranext-extension-template.${getFileExtensionByModuleFormat(
moduleFormat
)}`,
// Output to cjs format as that's what Paranext supports
formats: ["cjs"],
},
rollupOptions: {
// Do not bundle papi because it will be imported in Paranext
external: paranextProvidedModules,
},
// Generate sourcemaps as separate files since VSCode can load them directly
sourcemap: true,
},
/* resolve: {
alias: [
{
find: webViewTsxImportRegex,
// Pass the whole import string into the resolver
replacement: '$&',
customResolver(source, importer) {
const webViewFileInfo = path.parse(source);
const importerInfo = path.parse(importer);
const finalPath = path.resolve(
importerInfo.dir,
webViewFileInfo.dir,
webViewTempDir,
// If there is no file extension (parsed as the extension being .web-view), put js on it
webViewFileInfo.ext === '.web-view' ? `${webViewFileInfo.base}.js` : webViewFileInfo.base
);
console.log(`${source} -> ${finalPath}`);
return finalPath;
},
},
],
}, */
});