This repository has been archived by the owner on Feb 16, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
__run.js
54 lines (48 loc) · 1.81 KB
/
__run.js
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
import { updateViteConfig } from "../../adder-tools.js";
import { addImport, findImport, setDefault } from "../../ast-tools.js";
/** @type {import("../..").AdderRun<import("./__info.js").Options>} */
export const run = async ({ install, updateJavaScript, folderInfo }) => {
await updateViteConfig({
mutateViteConfig(viteConfig, containingFile, cjs) {
let viteImagetoolsImportedAs = findImport({ cjs, package: "vite-imagetools", typeScriptEstree: containingFile }).named["imagetools"];
if (!viteImagetoolsImportedAs) {
viteImagetoolsImportedAs = "imagetools";
addImport({ cjs, named: { imagetools: viteImagetoolsImportedAs }, package: "vite-imagetools", typeScriptEstree: containingFile });
}
const pluginsList = setDefault({
object: viteConfig,
default: {
type: "ArrayExpression",
elements: [],
},
property: "plugins",
});
if (pluginsList.type !== "ArrayExpression") throw new Error("`plugins` in Vite config needs to be an array");
/** @type {import("estree").CallExpression | undefined} */
let viteImagetoolsFunctionCall;
for (const element of pluginsList.elements) {
if (!element) continue;
if (element.type !== "CallExpression") continue;
if (element.callee.type !== "Identifier") continue;
if (element.callee.name !== viteImagetoolsImportedAs) continue;
viteImagetoolsFunctionCall = element;
}
// Add an imagetools() call to the Vite plugins list if missing
if (!viteImagetoolsFunctionCall) {
viteImagetoolsFunctionCall = {
type: "CallExpression",
callee: {
type: "Identifier",
name: viteImagetoolsImportedAs,
},
arguments: [],
optional: false,
};
pluginsList.elements.push(viteImagetoolsFunctionCall);
}
},
updateJavaScript,
folderInfo,
});
await install({ package: "vite-imagetools" });
};