-
Notifications
You must be signed in to change notification settings - Fork 0
/
build_npm.ts
122 lines (112 loc) · 2.97 KB
/
build_npm.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
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
// Copyright 2023-2024 ryu. All rights reserved. MIT license.
import denoJson from "./deno.json" with { type: "json" };
import * as esbuild from "esbuild";
import { createMinifier } from "@david/dts-minify";
// @ts-ignore-next-line
import { ts } from "@ts-morph/ts-morph";
import { emptyDir } from "@std/fs";
// setup
const outDir = "npm";
const fileList = ["colors"];
const entryPoints = fileList.map((file) => `${outDir}/${file}.ts`);
// package.json
const packageJson = {
name: "enogu",
version: Deno.args[0],
description: "Painting your terminal with colors",
license: "MIT",
repository: {
type: "git",
url: "git+https://github.com/ryuapp/enogu.git",
},
bugs: {
url: "https://github.com/ryuapp/enogu/issues",
},
type: "module",
main: "./cjs/colors.cjs",
module: "./esm/colors.mjs",
types: "./types/colors.d.cts",
exports: {
".": {
import: {
types: "./types/colors.d.ts",
default: "./esm/colors.mjs",
},
require: {
types: "./types/colors.d.cts",
default: "./cjs/colors.cjs",
},
},
},
devDependencies: {},
};
// output
// cleanup
await emptyDir("./npm");
await Deno.mkdirSync("npm/types");
await Deno.mkdirSync("npm/cjs");
await Deno.mkdirSync("npm/esm");
Deno.copyFileSync("LICENSE", `${outDir}/LICENSE`);
Deno.copyFileSync("src/README.md", `${outDir}/README.md`);
for (const filename of fileList) {
Deno.copyFileSync(`src/${filename}.ts`, `${outDir}/${filename}.ts`);
}
// esm
await esbuild.build({
entryPoints,
format: "esm",
outdir: `${outDir}/esm`,
sourcemap: false,
bundle: true,
minify: true,
outExtension: { ".js": ".mjs" },
legalComments: "eof",
});
esbuild.stop();
// cjs
await esbuild.build({
entryPoints,
format: "cjs",
outdir: `${outDir}/cjs`,
sourcemap: false,
bundle: true,
minify: true,
outExtension: { ".js": ".cjs" },
legalComments: "eof",
});
esbuild.stop();
// d.ts
const decoder = new TextDecoder("utf-8");
// setup (provide a TS Compiler API object)
const minifier = createMinifier(ts);
for (const file of fileList) {
const content = await Deno.readFile(`${outDir}/${file}.ts`);
const inputText = decoder.decode(content);
const minifiedText = minifier.minify(inputText, { keepJsDocs: true });
for (const ext of [".d.ts", ".d.cts"]) {
Deno.writeFile(
`${outDir}/types/${file}${ext}`,
new TextEncoder().encode(minifiedText),
);
}
}
// package.json
const devDeps: Record<string, string> = {};
const imports: Record<string, string> = denoJson.imports;
for (const alias in imports) {
const importName = imports[alias];
if (importName.startsWith("npm")) {
const name = importName.split(":")[1].split("@")[0];
const version = importName.split("@")[1];
devDeps[name] = version;
}
}
packageJson.devDependencies = devDeps;
Deno.writeFile(
`${outDir}/package.json`,
new TextEncoder().encode(JSON.stringify(packageJson, null, 2)),
);
// cleanup
for (const filename of fileList) {
Deno.removeSync(`${outDir}/${filename}.ts`);
}