-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.ts
101 lines (94 loc) · 3.2 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
90
91
92
93
94
95
96
97
98
99
100
101
import { rollup } from "rollup";
import { builtinModules } from "node:module";
import { basename, dirname, join } from "node:path";
import { fileURLToPath } from "node:url";
import { readFileSync } from "node:fs";
import { nodeResolve } from "@rollup/plugin-node-resolve";
import dts from "rollup-plugin-dts";
import { swc } from "rollup-plugin-swc3";
import { preparePublishPackage } from "../../base/scripts/src/utils/publish.js";
import { z } from "@island.is/pipes-core";
import { copyFile, mkdir } from "node:fs/promises";
const currentPath = fileURLToPath(dirname(import.meta.url));
const packageJSON = JSON.parse(readFileSync(join(currentPath, "package.json"), "utf-8"));
const external = Object.keys({ ...packageJSON.dependencies, ...packageJSON.peerDependencies });
const createConfig = (input: string) => {
const externalPackages = [...builtinModules, ...builtinModules.map((e) => `node:${e}`), ...external];
return {
input: input,
plugins: [nodeResolve()],
external(id) {
return externalPackages.includes(id) || id.includes("node_modules");
},
};
};
const mainConfig = (input: string, output: string) => {
const baseConfig = createConfig(input);
(baseConfig as any).output = {
sourcemap: false,
file: output,
format: "esm",
};
baseConfig.plugins = [
...baseConfig.plugins,
swc({
sourceMaps: false,
minify: false,
}),
];
return baseConfig as typeof baseConfig & {
output: { sourcemap: false; file: string; format: "commonjs" | "module" };
};
};
const dtsConfig = (input: string) => {
const baseConfig = createConfig(input);
baseConfig.plugins = [...baseConfig.plugins, dts({ respectExternal: false })];
return baseConfig;
};
const build = async (input: string, output: string, type: string) => {
const dts = dtsConfig(input);
const main = mainConfig(input, output);
await Promise.all([
(async () => {
const bundle = await rollup(main);
await bundle.write(main.output);
await bundle.close();
})(),
(async () => {
const bundle = await rollup(dts);
await bundle.write({ file: type });
await bundle.close();
})(),
]);
};
export const copyFiles = async () => {
const currentURL = dirname(fileURLToPath(import.meta.url));
const toURL = join(currentURL, "dist");
await mkdir(toURL, { recursive: true });
const files: { from: string; to: string }[] = packageJSON.pipes.publishFiles
.filter((e: string) => !e.startsWith("dist"))
.map((e: string) => {
return { from: join(currentURL, e), to: join(toURL, e) };
});
await Promise.all(
files.map(({ from, to }) => {
copyFile(from, to);
}),
);
};
const mainPath = join("dist", "dist");
const mainFile = basename(packageJSON.source).replace(".tsx", ".js").replace(".ts", ".js");
const mainFilePath = join(mainPath, mainFile);
const typeFilePath = join(mainPath, mainFile).replace(".js", ".d.ts");
const promises = [build(packageJSON.source, mainFilePath, typeFilePath), copyFiles()];
const version = z
.string()
.default(packageJSON.version, {
env: "RELEASE_VERSION",
arg: {
long: "releaseVersion",
},
})
.parse(undefined);
await Promise.all(promises);
await preparePublishPackage(process.cwd(), version);