This repository has been archived by the owner on May 5, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path__run.js
84 lines (74 loc) · 2.54 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
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
import { updateSveltePreprocessArgs, updateViteConfig } from "../../adder-tools.js";
import { addImport, setDefault } from "../../ast-tools.js";
/** @type {import("../..").AdderRun<import("./__info.js").Options>} */
export const run = async ({ folderInfo, install, updateJavaScript }) => {
await updateSveltePreprocessArgs({
folderInfo,
mutateSveltePreprocessArgs() {},
updateJavaScript,
});
await updateViteConfig({
mutateViteConfig(viteConfig, containingFile, cjs) {
let vitePluginCoffeeImportedAs = "coffee";
addImport({ require: vitePluginCoffeeImportedAs, cjs, default: vitePluginCoffeeImportedAs, package: "vite-plugin-coffee", 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 vitePluginCoffeeFunctionCall;
for (const element of pluginsList.elements) {
if (!element) continue;
if (element.type !== "CallExpression") continue;
if (element.callee.type !== "Identifier") continue;
if (element.callee.name !== vitePluginCoffeeImportedAs) continue;
vitePluginCoffeeFunctionCall = element;
}
// Add an vite-plugin-coffee() call to the Vite plugins list if missing
if (!vitePluginCoffeeFunctionCall) {
vitePluginCoffeeFunctionCall = {
type: "CallExpression",
callee: {
type: "Identifier",
name: vitePluginCoffeeImportedAs,
},
arguments: [
{
type: "ObjectExpression",
properties: [],
},
],
optional: false,
};
pluginsList.elements.push(vitePluginCoffeeFunctionCall);
}
let vitePluginCoffeeArgs = vitePluginCoffeeFunctionCall.arguments[0];
if (!vitePluginCoffeeArgs) {
vitePluginCoffeeArgs = {
type: "ObjectExpression",
properties: [],
};
vitePluginCoffeeFunctionCall.arguments.push(vitePluginCoffeeArgs);
}
if (vitePluginCoffeeArgs.type !== "ObjectExpression") throw new Error("vite-plugin-coffee arguments must be an object");
setDefault({
object: vitePluginCoffeeArgs,
property: "jsx",
default: {
type: "Literal",
value: false,
},
});
},
updateJavaScript,
folderInfo,
});
if (!folderInfo.kit) await install({ package: "@sveltejs/vite-plugin-svelte" });
await install({ package: "coffeescript" });
await install({ package: "vite-plugin-coffee" });
};