Skip to content

Commit c5ce128

Browse files
authored
Merge pull request #1 from ctlinker/chore-build-artefact
chore-m/devtools: added build artefacts
2 parents 6b91ec6 + ee25ccc commit c5ce128

4 files changed

Lines changed: 238 additions & 0 deletions

File tree

dist/builder/main.d.mts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { BuildOptions } from 'esbuild';
2+
3+
type BundlingParameters = {
4+
mode: "file";
5+
targets: {
6+
entry: string;
7+
outfile: string;
8+
variant?: ("esm" | "cjs" | "iife")[];
9+
options: {
10+
tsconfig?: string;
11+
eternal?: string[];
12+
minify?: boolean;
13+
minifySyntax?: true;
14+
minifyWhitespace?: true;
15+
minifyIdentifiers?: boolean;
16+
plateform: "node" | "browser" | "neutral";
17+
};
18+
extraOpts?: Partial<BuildOptions>;
19+
}[];
20+
};
21+
declare function Bundle(config: BundlingParameters): Promise<boolean>;
22+
23+
type DtsStepParams = {
24+
mode: "file";
25+
entry: string;
26+
outfile: string;
27+
tsconfig?: string;
28+
};
29+
declare function GenerateDts(config: DtsStepParams): Promise<boolean>;
30+
31+
export { Bundle, GenerateDts };

dist/builder/main.d.ts

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
import { BuildOptions } from 'esbuild';
2+
3+
type BundlingParameters = {
4+
mode: "file";
5+
targets: {
6+
entry: string;
7+
outfile: string;
8+
variant?: ("esm" | "cjs" | "iife")[];
9+
options: {
10+
tsconfig?: string;
11+
eternal?: string[];
12+
minify?: boolean;
13+
minifySyntax?: true;
14+
minifyWhitespace?: true;
15+
minifyIdentifiers?: boolean;
16+
plateform: "node" | "browser" | "neutral";
17+
};
18+
extraOpts?: Partial<BuildOptions>;
19+
}[];
20+
};
21+
declare function Bundle(config: BundlingParameters): Promise<boolean>;
22+
23+
type DtsStepParams = {
24+
mode: "file";
25+
entry: string;
26+
outfile: string;
27+
tsconfig?: string;
28+
};
29+
declare function GenerateDts(config: DtsStepParams): Promise<boolean>;
30+
31+
export { Bundle, GenerateDts };

dist/builder/main.js

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"use strict";
2+
var __defProp = Object.defineProperty;
3+
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
4+
var __getOwnPropNames = Object.getOwnPropertyNames;
5+
var __hasOwnProp = Object.prototype.hasOwnProperty;
6+
var __export = (target, all) => {
7+
for (var name in all)
8+
__defProp(target, name, { get: all[name], enumerable: true });
9+
};
10+
var __copyProps = (to, from, except, desc) => {
11+
if (from && typeof from === "object" || typeof from === "function") {
12+
for (let key of __getOwnPropNames(from))
13+
if (!__hasOwnProp.call(to, key) && key !== except)
14+
__defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable });
15+
}
16+
return to;
17+
};
18+
var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19+
20+
// src/builder/main.ts
21+
var main_exports = {};
22+
__export(main_exports, {
23+
Bundle: () => Bundle,
24+
GenerateDts: () => GenerateDts
25+
});
26+
module.exports = __toCommonJS(main_exports);
27+
28+
// src/builder/lib/bundle.ts
29+
var import_esbuild = require("esbuild");
30+
async function Bundle(config) {
31+
if (!config.targets?.length) {
32+
console.warn("\x1B[33m\u26A0\uFE0F No bundle targets provided.\x1B[0m");
33+
return false;
34+
}
35+
for (const target of config.targets) {
36+
const variants = target.variant ?? ["esm"];
37+
for (const v of variants) {
38+
const esbuildOpts = {
39+
entryPoints: [target.entry],
40+
outfile: target.outfile.replace(/\.js$/, `.${v}.js`),
41+
format: v,
42+
platform: target.options.plateform,
43+
tsconfig: target.options.tsconfig,
44+
bundle: true,
45+
minify: !!target.options.minify,
46+
minifySyntax: target.options.minifySyntax ?? void 0,
47+
minifyWhitespace: target.options.minifyWhitespace ?? void 0,
48+
minifyIdentifiers: target.options.minifyIdentifiers ?? void 0,
49+
...target.extraOpts ?? {}
50+
};
51+
console.log(
52+
`\x1B[36m\u27A1\uFE0F Building\x1B[0m \x1B[1m${target.entry}\x1B[0m \u2192 \x1B[32m${esbuildOpts.outfile}\x1B[0m (\x1B[35m${v}\x1B[0m)`
53+
);
54+
try {
55+
await (0, import_esbuild.build)(esbuildOpts);
56+
console.log(
57+
`\x1B[32m\u2705 Successfully built\x1B[0m ${target.entry} \u2192 ${esbuildOpts.outfile}`
58+
);
59+
} catch (err) {
60+
console.error("\x1B[31m\u274C Bundle step failed:\x1B[0m", err);
61+
return false;
62+
}
63+
}
64+
}
65+
return true;
66+
}
67+
68+
// src/builder/lib/dts.ts
69+
var import_tsup = require("tsup");
70+
async function GenerateDts(config) {
71+
if (!config.entry || !config.outfile) {
72+
console.warn("\x1B[33m\u26A0\uFE0F DTS step requires entry and outfile.\x1B[0m");
73+
return false;
74+
}
75+
console.log(
76+
`\x1B[36m\u27A1\uFE0F Generating DTS\x1B[0m \x1B[1m${config.entry}\x1B[0m \u2192 \x1B[32m${config.outfile}\x1B[0m`
77+
);
78+
const tsupOptions = {
79+
entry: [config.entry],
80+
dts: true,
81+
outDir: config.outfile.replace(/\/?[^/]+$/, ""),
82+
format: ["cjs", "esm"],
83+
minify: false,
84+
clean: false,
85+
tsconfig: config.tsconfig,
86+
splitting: false
87+
// optional,
88+
};
89+
try {
90+
await (0, import_tsup.build)(tsupOptions);
91+
console.log(`\x1B[32m\u2705 Declaration file generated:\x1B[0m ${config.outfile}`);
92+
} catch (err) {
93+
console.error("\x1B[31m\u274C DTS generation failed:\x1B[0m", err);
94+
return false;
95+
}
96+
return true;
97+
}
98+
// Annotate the CommonJS export names for ESM import in node:
99+
0 && (module.exports = {
100+
Bundle,
101+
GenerateDts
102+
});

dist/builder/main.mjs

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
// src/builder/lib/bundle.ts
2+
import { build } from "esbuild";
3+
async function Bundle(config) {
4+
if (!config.targets?.length) {
5+
console.warn("\x1B[33m\u26A0\uFE0F No bundle targets provided.\x1B[0m");
6+
return false;
7+
}
8+
for (const target of config.targets) {
9+
const variants = target.variant ?? ["esm"];
10+
for (const v of variants) {
11+
const esbuildOpts = {
12+
entryPoints: [target.entry],
13+
outfile: target.outfile.replace(/\.js$/, `.${v}.js`),
14+
format: v,
15+
platform: target.options.plateform,
16+
tsconfig: target.options.tsconfig,
17+
bundle: true,
18+
minify: !!target.options.minify,
19+
minifySyntax: target.options.minifySyntax ?? void 0,
20+
minifyWhitespace: target.options.minifyWhitespace ?? void 0,
21+
minifyIdentifiers: target.options.minifyIdentifiers ?? void 0,
22+
...target.extraOpts ?? {}
23+
};
24+
console.log(
25+
`\x1B[36m\u27A1\uFE0F Building\x1B[0m \x1B[1m${target.entry}\x1B[0m \u2192 \x1B[32m${esbuildOpts.outfile}\x1B[0m (\x1B[35m${v}\x1B[0m)`
26+
);
27+
try {
28+
await build(esbuildOpts);
29+
console.log(
30+
`\x1B[32m\u2705 Successfully built\x1B[0m ${target.entry} \u2192 ${esbuildOpts.outfile}`
31+
);
32+
} catch (err) {
33+
console.error("\x1B[31m\u274C Bundle step failed:\x1B[0m", err);
34+
return false;
35+
}
36+
}
37+
}
38+
return true;
39+
}
40+
41+
// src/builder/lib/dts.ts
42+
import { build as build2 } from "tsup";
43+
async function GenerateDts(config) {
44+
if (!config.entry || !config.outfile) {
45+
console.warn("\x1B[33m\u26A0\uFE0F DTS step requires entry and outfile.\x1B[0m");
46+
return false;
47+
}
48+
console.log(
49+
`\x1B[36m\u27A1\uFE0F Generating DTS\x1B[0m \x1B[1m${config.entry}\x1B[0m \u2192 \x1B[32m${config.outfile}\x1B[0m`
50+
);
51+
const tsupOptions = {
52+
entry: [config.entry],
53+
dts: true,
54+
outDir: config.outfile.replace(/\/?[^/]+$/, ""),
55+
format: ["cjs", "esm"],
56+
minify: false,
57+
clean: false,
58+
tsconfig: config.tsconfig,
59+
splitting: false
60+
// optional,
61+
};
62+
try {
63+
await build2(tsupOptions);
64+
console.log(`\x1B[32m\u2705 Declaration file generated:\x1B[0m ${config.outfile}`);
65+
} catch (err) {
66+
console.error("\x1B[31m\u274C DTS generation failed:\x1B[0m", err);
67+
return false;
68+
}
69+
return true;
70+
}
71+
export {
72+
Bundle,
73+
GenerateDts
74+
};

0 commit comments

Comments
 (0)