Repository navigation
Expand file tree
/
Copy pathbuild.ts
More file actions
169 lines (157 loc) · 4.6 KB
/
Copy pathbuild.ts
File metadata and controls
169 lines (157 loc) · 4.6 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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
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" }),
});
// Build the solid entry
await build({
entry: ["src/solid/index.ts"],
format: ["cjs", "esm"],
external: ["solid-js", "../index", "../utils"],
splitting: false,
sourcemap: true,
clean: true,
dts: true,
outDir: "dist/solid",
outExtension: ({ format }) => ({ js: format === "cjs" ? ".cjs" : ".mjs" }),
});
async function replaceImports(fileName: string) {
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");
await replaceImports("dist/solid/index.mjs");
await replaceImports("dist/solid/index.cjs");
// Marko source is compiled by the consumer's bundler.
// "../index" in dnd.marko resolves to dist/index.mjs at consumer build time.
console.log("Copying Marko integration...");
const markoDistTagsDir = resolve(__dirname, "dist/tags");
await mkdir(markoDistTagsDir, { recursive: true });
await copyFile(
resolve(__dirname, "src/marko/dnd.marko"),
resolve(markoDistTagsDir, "dnd.marko")
);
// marko.json at dist root enables <dnd> auto-discovery in consuming projects.
await writeFile(
resolve(__dirname, "dist/marko.json"),
JSON.stringify({ "tags-dir": "./tags", "script-lang": "ts" }, null, 2),
"utf8"
);
console.log("Rewriting package.json...");
const publishExports = {
"./vue": {
require: {
types: "./vue/index.d.cts",
default: "./vue/index.cjs",
},
import: {
types: "./vue/index.d.ts",
default: "./vue/index.mjs",
},
},
"./react": {
require: {
types: "./react/index.d.cts",
default: "./react/index.cjs",
},
import: {
types: "./react/index.d.ts",
default: "./react/index.mjs",
},
},
"./solid": {
require: {
types: "./solid/index.d.cts",
default: "./solid/index.cjs",
},
import: {
types: "./solid/index.d.ts",
default: "./solid/index.mjs",
},
},
"./marko": {
import: "./tags/dnd.marko",
default: "./tags/dnd.marko",
},
".": {
require: {
types: "./index.d.cts",
default: "./index.cjs",
},
import: {
types: "./index.d.ts",
default: "./index.mjs",
},
},
};
const packageJson = {
...JSON.parse(await readFile(resolve(__dirname, `package.json`), "utf8")),
main: "./index.cjs",
types: "./index.d.cts",
exports: publishExports,
};
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"
);
})();