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 4
/
Copy path__run.js
87 lines (77 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
85
86
87
import { setupStyleLanguage } from "../../adder-tools.js";
import { addImport, findImport, setDefaultDefaultExport, setDefault } from "../../ast-tools.js";
import { extension, postcssConfigCjsPath, stylesHint } from "./stuff.js";
/**
* @param {import("../../ast-io.js").RecastAST} postcssConfigAst
* @param {boolean} autoprefixer
* @returns {import("../../ast-io.js").RecastAST}
*/
const updatePostcssConfig = (postcssConfigAst, autoprefixer) => {
const configObject = setDefaultDefaultExport({
cjs: true,
defaultValue: {
type: "ObjectExpression",
properties: [],
},
typeScriptEstree: postcssConfigAst,
});
if (configObject.type !== "ObjectExpression") throw new Error("PostCSS config must be an object");
const pluginsList = setDefault({
default: /** @type {import("estree").ArrayExpression} */ ({
type: "ArrayExpression",
elements: [],
}),
object: configObject,
property: "plugins",
});
if (pluginsList.type === "ArrayExpression") {
if (autoprefixer) {
let autoprefixerImportedAs = findImport({ cjs: true, package: "autoprefixer", typeScriptEstree: postcssConfigAst }).require;
// Add an Autoprefixer import if it's not there
if (!autoprefixerImportedAs) {
autoprefixerImportedAs = "autoprefixer";
addImport({ require: autoprefixerImportedAs, cjs: true, package: "autoprefixer", typeScriptEstree: postcssConfigAst });
}
pluginsList.elements.push({
type: "Identifier",
name: autoprefixerImportedAs,
});
}
} else if (pluginsList.type === "ObjectExpression") {
setDefault({
default: {
type: "ObjectExpression",
properties: [],
},
object: pluginsList,
property: "autoprefixer",
});
} else {
throw new Error("`plugins` in PostCSS config must be an array or object");
}
return postcssConfigAst;
};
/** @type {import("../..").AdderRun<import("./__info.js").Options>} */
export const run = async ({ folderInfo, install, options, updateCss, updateJavaScript, updateSvelte }) => {
await setupStyleLanguage({
extension,
folderInfo,
mutateSveltePreprocessArgs() {},
stylesHint,
updateCss,
updateJavaScript,
updateSvelte,
});
await updateJavaScript({
path: postcssConfigCjsPath,
async script({ typeScriptEstree }) {
return {
typeScriptEstree: updatePostcssConfig(typeScriptEstree, options.autoprefixer),
};
},
});
await install({ package: "postcss" });
await install({ package: "postcss-load-config" });
if (!folderInfo.kit) await install({ package: "@sveltejs/vite-plugin-svelte" });
if (options.autoprefixer) await install({ package: "autoprefixer" });
};