-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.helpers.mjs
More file actions
143 lines (126 loc) · 4.75 KB
/
build.helpers.mjs
File metadata and controls
143 lines (126 loc) · 4.75 KB
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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import { existsSync as pathExists } from "node:fs";
import fs from "node:fs/promises";
import path from "node:path";
async function ensureExtensionDirectoryExists(appDir, extensionDirectoryName) {
if (appDir.trim() !== "" && pathExists(appDir)) {
const extDir = `${appDir}/${extensionDirectoryName}`;
if (!pathExists(extDir)) {
await fs.mkdir(extDir);
}
return extDir;
}
}
async function copyExtensionAssetsToApplication(appExtensionDirPath, outDir) {
const extensionName = outDir.split("/").pop();
const deployedExtensionPath = `${appExtensionDirPath}/${extensionName}`;
if (pathExists(deployedExtensionPath)) {
await fs.rm(deployedExtensionPath, { recursive: true, force: true });
}
await fs.mkdir(deployedExtensionPath);
await fs.cp(outDir, deployedExtensionPath, { recursive: true });
}
export const copyToAppPlugin = (appDir, outDir, extensionDirectoryName) => ({
name: "copy-to-app",
setup(build) {
build.onEnd(async result => {
if (!result.errors.length) {
const appExtensionDirPath = await ensureExtensionDirectoryExists(appDir, extensionDirectoryName);
if (appExtensionDirPath) {
copyExtensionAssetsToApplication(appExtensionDirPath, outDir);
} else {
console.error("Could not find Mendix application directory:", appDir);
console.info("Skipping copying the extension to application directory");
}
}
});
}
});
export const copyCssPlugin = (outDir, files) => ({
name: "copy-css",
setup(build) {
build.onEnd(async result => {
if (!result.errors.length) {
await Promise.all(
files.map(async ({ from, to }) => {
const destination = `${outDir}/${to ?? path.basename(from)}`;
try {
await fs.copyFile(from, destination);
} catch (error) {
console.error(`Failed to copy ${from} to ${destination}`, error);
}
})
);
}
});
}
});
export const copyManifestPlugin = outDir => ({
name: "copy-manifest",
setup(build) {
build.onEnd(async result => {
if (!result.errors.length) {
try {
await fs.copyFile("src/manifest.json", `${outDir}/manifest.json`);
} catch (error) {
console.error("Make sure that manifest.json file is present in src/ folder", error);
}
}
});
}
});
export const bundleCssPlugin = (outDir, cssFileName = "style.css", { inject = false } = {}) => {
const collectedCss = new Map();
return {
name: "bundle-css",
setup(build) {
build.onLoad({ filter: /\.css$/ }, async args => {
const css = await fs.readFile(args.path, "utf8");
collectedCss.set(args.path, css);
if (inject) {
return {
contents: `
(function() {
if (!document.querySelector('link[href="${cssFileName}"]')) {
const link = document.createElement("link");
link.rel = "stylesheet";
link.href = "${cssFileName}";
document.head.appendChild(link);
}
})();
`,
loader: "js"
};
}
return { contents: "", loader: "js" };
});
build.onEnd(async result => {
if (!result.errors.length && collectedCss.size > 0) {
const bundledCss = Array.from(collectedCss.values()).join("\n\n");
await fs.mkdir(outDir, { recursive: true });
await fs.writeFile(`${outDir}/${cssFileName}`, bundledCss);
console.log(`Bundled ${collectedCss.size} CSS file(s) into ${cssFileName}`);
}
});
}
};
};
export const commonConfig = {
target: "es2023",
platform: "browser",
format: "esm",
bundle: true,
splitting: true,
treeShaking: true,
logLevel: "info",
assetNames: "assets/[ext]/[name]-[hash]",
external: ["@mendix/component-framework", "@mendix/model-access-sdk"],
loader: {
".png": "file",
".svg": "file",
".gif": "file",
".ttf": "file",
".woff": "file",
".woff2": "file"
},
sourcemap: true
};