-
-
Notifications
You must be signed in to change notification settings - Fork 37
/
build.ts
89 lines (80 loc) · 2.51 KB
/
build.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
import { build } from "tsup";
import { fileURLToPath } from "url";
import { resolve, dirname } from "pathe";
import { readFile, writeFile, copyFile, mkdir } from "fs/promises";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
(async function buildItAll() {
// Build the main entry
await build({
entry: ["src/index.ts"],
format: ["cjs", "esm"],
splitting: false,
sourcemap: true,
clean: true,
dts: true,
outDir: "dist",
outExtension: ({ format }) => ({ js: format === "cjs" ? ".cjs" : ".mjs" }),
});
// Build the vue entry
await build({
entry: ["src/vue/index.ts"],
format: ["cjs", "esm"],
external: ["vue", "../index", "../utils"],
splitting: false,
sourcemap: true,
clean: true,
dts: true,
outDir: "dist/vue",
outExtension: ({ format }) => ({ js: format === "cjs" ? ".cjs" : ".mjs" }),
});
// Build the react entry
await build({
entry: ["src/react/index.ts"],
format: ["cjs", "esm"],
external: ["react", "react-dom", "../index", "../utils"],
splitting: false,
sourcemap: true,
clean: true,
dts: true,
outDir: "dist/react",
outExtension: ({ format }) => ({ js: format === "cjs" ? ".cjs" : ".mjs" }),
});
async function replaceImports(fileName) {
const format = fileName.endsWith("mjs") ? "mjs" : "cjs";
const file = await readFile(resolve(__dirname, `${fileName}`), "utf8");
const updatedFile = file.replace(
/\.\.\/(index|utils)/g,
`../index.${format}`
);
await writeFile(resolve(__dirname, `${fileName}`), updatedFile, "utf8");
}
await replaceImports("dist/vue/index.mjs");
await replaceImports("dist/vue/index.cjs");
await replaceImports("dist/react/index.mjs");
await replaceImports("dist/react/index.cjs");
console.log("Rewriting package.json...");
const packageJson = {
name: "@formkit/drag-and-drop",
...JSON.parse(await readFile(resolve(__dirname, `package.json`), "utf8")),
};
delete packageJson.devDependencies;
delete packageJson.private;
const updatedFile = JSON.stringify(packageJson, null, 2);
await writeFile(
resolve(__dirname, `./dist/package.json`),
updatedFile,
"utf8"
);
console.log("Copy README.md and LICENSE");
await writeFile(
resolve(__dirname, `./dist/README.md`),
await readFile(resolve(__dirname, `./README.md`), "utf8"),
"utf8"
);
await writeFile(
resolve(__dirname, `./dist/LICENSE`),
await readFile(resolve(__dirname, `./LICENSE`), "utf8"),
"utf8"
);
})();