Skip to content

Commit

Permalink
🔨 将package.json中script字段里的一些内容移动到prebuild.ts
Browse files Browse the repository at this point in the history
  • Loading branch information
neila-a committed Feb 2, 2024
1 parent bc64f0f commit 432cbd7
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pids
*.pid.lock
src/app/pages.json
prebuild.js
prebuild.mjs

# Directory for instrumented libs generated by jscoverage/JSCover
lib-cov
Expand Down
20 changes: 6 additions & 14 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "verkfi",
"version": "1.6.1",
"devVersion": "761",
"devVersion": "762",
"dev": true,
"description": "Platform for Neila's something useless tools.",
"private": true,
Expand All @@ -19,20 +19,12 @@
"url": "https://neilasite.netlify.app/"
},
"scripts": {
"dev": "npm run predev && next dev",
"precommon": "npm run build:nc && npm run build:sw",
"prebuild": "npm run build:mp && node prebuild.js && npm run precommon",
"poststart": "rm prebuild.js",
"build": "next build",
"predev": "npm run build:mp && node prebuild.js dev && npm run precommon && npm run poststart",
"start": "next start",
"dev": "npm run build:pb && VERKFI_ENV=dev node prebuild.js && next dev",
"prebuild": "npm run build:pb && node prebuild.js",
"build": "npm run prebuild && build",
"build:pb": "esbuild prebuild.ts --bundle --minify --external:esbuild --platform=node --outfile=prebuild.js",
"lint": "next lint",
"build:sw": "esbuild src/app/service-worker.ts --bundle --minify > public/service-worker.js",
"build:nc": "esbuild next.config.ts --bundle --minify --platform=node > next.config.js",
"build:mp": "esbuild prebuild.ts --bundle --minify --platform=node > prebuild.js",
"analyze": "ANALYZE=true npm run build",
"analyze:server": "BUNDLE_ANALYZE=server npm run build",
"analyze:browser": "BUNDLE_ANALYZE=browser npm run build"
"start": "next start"
},
"dependencies": {
"@emotion/react": "^11.11.3",
Expand Down
38 changes: 35 additions & 3 deletions prebuild.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@
*/
import * as fs from "node:fs";
import pack from "./package.json";
import {
BuildResult,
Message,
build
} from "esbuild";
import type {
Manifest
} from "next/dist/lib/metadata/types/manifest-types";
Expand All @@ -11,7 +16,7 @@ import {
getRepoInfo
} from "./src/app/components/getRepoInfo";
const logger = new Logger({
name: "ModifyPackage",
name: "prebuild",
level: "log"
});
import Logger from "lp-logger";
Expand Down Expand Up @@ -51,9 +56,36 @@ async function publicMain() {
});
const pagesJSON = JSON.stringify(pages, null, 4);
fs.writeFileSync("src/app/pages.json", pagesJSON);
return pagesJSON
const logbuild: <T>(result: BuildResult<T>, filename: string) => void = (result, filename) => {
logger.log(`正在编译${filename}……`);
const log = (message: [Message[], string]) => {
if (message[0].length === 0) {
logger.log(`编译${filename}时未出现${message[1]}。`);
} else {
message[0].forEach(warn => logger.warn(`编译${filename}时出现${message[1]}:${JSON.stringify(warn)}`));
}
};
log([result.errors, "错误"]);
log([result.warnings, "警告"]);
},
NextConfig = await build({
entryPoints: ["next.config.ts"],
outfile: "next.config.js",
bundle: true,
minify: true,
platform: "node"
}),
ServiceWorker = await build({
entryPoints: ["src/app/service-worker.ts"],
outfile: "public/service-worker.js",
bundle: true,
minify: true
});
logbuild(NextConfig, "next.config.ts");
logbuild(ServiceWorker, "service-worker.ts");
return pagesJSON;
}
if (process.argv[2] === "dev") {
if (process.env.VERKFI_ENV === "dev") {
devMain();
}
publicMain();
Expand Down

0 comments on commit 432cbd7

Please sign in to comment.