Skip to content

Commit 09c2081

Browse files
authored
feat: remove pika and replace with esbuild + tsc (#260)
* feat: remove pika and replace with esbuild + tsc * style: prettier
1 parent 8bfa636 commit 09c2081

File tree

4 files changed

+145
-19
lines changed

4 files changed

+145
-19
lines changed

index.js

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ const command = require("./lib/command");
1010
const createBranchProtection = require("./lib/create-branch-protection");
1111
const createCoc = require("./lib/create-coc");
1212
const createContributing = require("./lib/create-contributing");
13+
const createEsbuildScript = require("./lib/create-esbuild-script");
1314
const createIssueTemplates = require("./lib/create-issue-templates");
1415
const createLicense = require("./lib/create-license");
1516
const createPackageJson = require("./lib/create-package-json");
@@ -184,8 +185,8 @@ module.exports = async function main() {
184185
const dependencies = [];
185186
const devDependencies = [
186187
"@octokit/tsconfig",
187-
"@pika/pack",
188-
"@pika/plugin-ts-standard-pkg",
188+
"esbuild",
189+
"glob",
189190
"@types/jest",
190191
"@types/node",
191192
"jest",
@@ -196,12 +197,6 @@ module.exports = async function main() {
196197
"typescript",
197198
];
198199

199-
if (answers.supportsBrowsers) {
200-
devDependencies.push("@pika/plugin-build-web");
201-
}
202-
if (answers.supportsNode) {
203-
devDependencies.push("@pika/plugin-build-node");
204-
}
205200
if (answers.isPlugin || answers.isAuthenticationStrategy) {
206201
devDependencies.push("@octokit/core");
207202
}
@@ -241,10 +236,18 @@ module.exports = async function main() {
241236
JSON.stringify({
242237
extends: "@octokit/tsconfig",
243238
include: ["src/**/*"],
239+
compilerOptions: {
240+
declaration: true,
241+
outDir: "pkg/dist-types",
242+
emitDeclarationOnly: true,
243+
sourceMap: true,
244+
},
244245
})
245246
);
246247
await command(`git add tsconfig.json`);
247-
await command(`git commit -m 'build(typescript): configuration for pika'`);
248+
await command(
249+
`git commit -m 'build(typescript): configuration for esbuild'`
250+
);
248251

249252
console.log("create smoke test");
250253

@@ -418,6 +421,7 @@ module.exports = async function main() {
418421
}
419422
}
420423

424+
await createEsbuildScript(answers);
421425
await command(`git add src`);
422426
await command(`git commit -m 'feat: initial version'`);
423427

lib/create-esbuild-script.js

Lines changed: 129 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,129 @@
1+
module.exports = createEsbuildScript;
2+
3+
const writePrettyFile = require("./write-pretty-file");
4+
5+
async function createEsbuildScript(answers) {
6+
const nodeBuildOptions = ` // Build a CJS Node.js bundle
7+
await esbuild.build({
8+
entryPoints,
9+
outdir: "pkg/dist-node",
10+
bundle: true,
11+
platform: "node",
12+
target: "node18",
13+
format: "cjs",
14+
...sharedOptions,
15+
})`;
16+
const browserBuildOptions = ` // Build an ESM browser bundle
17+
await esbuild.build({
18+
entryPoints,
19+
outdir: "pkg/dist-web",
20+
bundle: true,
21+
platform: "browser",
22+
format: "esm",
23+
...sharedOptions,
24+
});`;
25+
const dualBuildOptions = ` await Promise.all([
26+
// Build a CJS Node.js bundle
27+
esbuild.build({
28+
entryPoints,
29+
outdir: "pkg/dist-node",
30+
bundle: true,
31+
platform: "node",
32+
target: "node18",
33+
format: "cjs",
34+
...sharedOptions,
35+
}),
36+
// Build an ESM browser bundle
37+
esbuild.build({
38+
entryPoints,
39+
outdir: "pkg/dist-web",
40+
bundle: true,
41+
platform: "browser",
42+
format: "esm",
43+
...sharedOptions,
44+
}),
45+
]);`;
46+
47+
await writePrettyFile(
48+
"scripts/esbuild.mjs",
49+
`// @ts-check
50+
import esbuild from "esbuild";
51+
import { copyFile, readFile, writeFile, rm } from "fs/promises";
52+
import { glob } from "glob";
53+
54+
/**
55+
* @type {esbuild.BuildOptions}
56+
*/
57+
const sharedOptions = {
58+
sourcemap: "external",
59+
sourcesContent: true,
60+
minify: false,
61+
allowOverwrite: true,
62+
packages: "external",
63+
};
64+
65+
async function main() {
66+
// Start with a clean slate
67+
await rm("pkg", { recursive: true, force: true });
68+
// Build the source code for a neutral platform as ESM
69+
await esbuild.build({
70+
entryPoints: await glob(["./src/*.ts", "./src/**/*.ts"]),
71+
outdir: "pkg/dist-src",
72+
bundle: false,
73+
platform: "neutral",
74+
format: "esm",
75+
...sharedOptions,
76+
sourcemap: false,
77+
});
78+
79+
// Remove the types file from the dist-src folder
80+
const typeFiles = await glob([
81+
"./pkg/dist-src/**/types.js.map",
82+
"./pkg/dist-src/**/types.js",
83+
]);
84+
for (const typeFile of typeFiles) {
85+
await rm(typeFile);
86+
}
87+
88+
const entryPoints = ["./pkg/dist-src/index.js"];\n
89+
${
90+
answers.supportsBrowsers && answers.supportsNode
91+
? dualBuildOptions
92+
: answers.supportsNode
93+
? nodeBuildOptions
94+
: answers.supportsBrowsers
95+
? browserBuildOptions
96+
: ""
97+
}\n
98+
// Copy the README, LICENSE to the pkg folder
99+
await copyFile("LICENSE", "pkg/LICENSE");
100+
await copyFile("README.md", "pkg/README.md");
101+
102+
// Handle the package.json
103+
let pkg = JSON.parse((await readFile("package.json", "utf8")).toString());
104+
// Remove unnecessary fields from the package.json
105+
delete pkg.scripts;
106+
delete pkg.prettier;
107+
delete pkg.release;
108+
delete pkg.jest;
109+
await writeFile(
110+
"pkg/package.json",
111+
JSON.stringify(
112+
{
113+
...pkg,
114+
files: ["dist-*/**", "bin/**"],
115+
main: "dist-node/index.js",
116+
module: "dist-web/index.js",
117+
types: "dist-types/index.d.ts",
118+
source: "dist-src/index.js",
119+
sideEffects: false,
120+
},
121+
null,
122+
2
123+
)
124+
);
125+
}
126+
main();
127+
`
128+
);
129+
}

lib/create-package-json.js

Lines changed: 1 addition & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ async function createPackageJson(answers) {
1212
version: "0.0.0-development",
1313
description: answers.description,
1414
scripts: {
15-
build: "pika-pack build",
15+
build: "node scripts/build.mjs && tsc -p tsconfig.json",
1616
lint: "prettier --check '{src,test}/**/*' README.md package.json",
1717
"lint:fix": "prettier --write '{src,test}/**/*' README.md package.json",
1818
pretest: "npm run -s lint",
@@ -36,9 +36,6 @@ async function createPackageJson(answers) {
3636
},
3737
},
3838
},
39-
"@pika/pack": {
40-
pipeline: [["@pika/plugin-ts-standard-pkg"]],
41-
},
4239
release: {
4340
branches: [
4441
"+([0-9]).x",
@@ -75,12 +72,6 @@ async function createPackageJson(answers) {
7572
};
7673
}
7774

78-
if (answers.supportsNode) {
79-
pkg["@pika/pack"].pipeline.push(["@pika/plugin-build-node"]);
80-
}
81-
if (answers.supportsBrowsers) {
82-
pkg["@pika/pack"].pipeline.push(["@pika/plugin-build-web"]);
83-
}
8475
if (answers.isPlugin) {
8576
Object.assign(pkg.peerDependencies, OCTOKIT_PLUGIN_PEER_DEPENDENCIES);
8677
}

lib/write-pretty-file.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ async function writePrettyFile(path, content) {
2020
".md": "markdown",
2121
".ts": "typescript",
2222
".yml": "yaml",
23+
".js": "babel",
24+
".mjs": "babel",
2325
}[ext];
2426

2527
if (!parser) throw new Error(`Define parser for ${path}`);

0 commit comments

Comments
 (0)