Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

build: mirror nitro and nitropack npm packages #2497

Merged
merged 13 commits into from
Jun 13, 2024
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,10 @@ test/fixture/functions

.pnpm-store
.wrangler

# mirror pkg
.mirror

# Generated types
*.d.ts
!runtime-meta.d.ts
13 changes: 12 additions & 1 deletion build.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { defineBuildConfig } from "unbuild";
import { resolve } from "pathe";
import { fileURLToPath } from "node:url";
import { normalize } from "pathe";
import { writeFile } from "node:fs/promises";

const srcDir = fileURLToPath(new URL("src", import.meta.url));

Expand Down Expand Up @@ -50,6 +51,16 @@ export default defineBuildConfig({
])
),
},
hooks: {
async "build:prepare"(ctx) {
for (const subpath of subpaths) {
await writeFile(
`./${subpath}.d.ts`,
`export * from "./dist/${subpath}/index";`
);
}
},
},
externals: [
"nitropack",
"nitropack/runtime/meta",
Expand All @@ -59,7 +70,7 @@ export default defineBuildConfig({
],
rollup: {
output: {
chunkFileNames(chunk) {
chunkFileNames(chunk: any) {
const id = normalize(chunk.moduleIds.at(-1));
if (id.includes("/src/cli/")) {
return "cli/[name].mjs";
Expand Down
1 change: 0 additions & 1 deletion cli.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion config.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion core.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion kit.d.ts

This file was deleted.

14 changes: 6 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@
"import": "./dist/config/index.mjs"
},
".": {
"types": "./types.d.ts",
"types": "./dist/core/index.d.ts",
"import": "./dist/core/index.mjs"
},
"./core": {
"types": "./types.d.ts",
"types": "./dist/core/index.d.ts",
"import": "./dist/core/index.mjs"
},
"./kit": {
Expand Down Expand Up @@ -65,16 +65,12 @@
"./package.json": "./package.json"
},
"main": "./dist/core/index.mjs",
"types": "./types.d.ts",
"types": "./dist/core/index.d.ts",
"bin": {
"nitro": "./dist/cli/index.mjs",
"nitropack": "./dist/cli/index.mjs"
},
"files": [
"dist",
"*.d.ts",
"runtime-meta.*"
],
"files": ["dist", "*.d.ts", "runtime-meta.*"],
"scripts": {
"build": "pnpm gen-presets && unbuild",
"dev": "pnpm nitro dev playground",
Expand All @@ -84,6 +80,7 @@
"lint:fix": "eslint --cache --fix . && biome format . --write",
"nitro": "JITI_ESM_RESOLVE=1 NODE_OPTIONS=\"--enable-source-maps\" jiti ./src/cli/index.ts",
"prepack": "pnpm build",
"gen-mirror": "JITI_ESM_RESOLVE=1 pnpm jiti scripts/gen-mirror.ts",
"gen-presets": "JITI_ESM_RESOLVE=1 pnpm jiti scripts/gen-presets.ts",
"release": "pnpm test && pnpm build && changelogen --release && pnpm publish && git push --follow-tags",
"stub": "unbuild --stub",
Expand Down Expand Up @@ -188,6 +185,7 @@
"automd": "^0.3.7",
"better-sqlite3": "^11.0.0",
"changelogen": "^0.5.5",
"codeup": "^0.0.4",
"edge-runtime": "^2.5.10",
"eslint": "^9.4.0",
"eslint-config-unjs": "0.3.2",
Expand Down
19 changes: 19 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion presets.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion rollup.d.ts

This file was deleted.

8 changes: 4 additions & 4 deletions runtime-meta.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export const pkgDir: string;
export const runtimeDir: string;
export const subpaths: string[];
export const runtimeDependencies: string[];
export declare const pkgDir: string;
export declare const runtimeDir: string;
export declare const subpaths: string[];
export declare const runtimeDependencies: string[];
1 change: 0 additions & 1 deletion runtime.d.ts

This file was deleted.

1 change: 0 additions & 1 deletion schema.d.ts

This file was deleted.

92 changes: 92 additions & 0 deletions scripts/gen-mirror.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
import { subpaths } from "../build.config";
import { fileURLToPath } from "mlly";
import { PackageJson, readPackageJSON } from "pkg-types";
import { writeFile, mkdir, rm, cp } from "node:fs/promises";
import { join } from "pathe";

const copyPkgFields = [
"description",
"keywords",
"repository",
"license",
"type",
"exports",
"main",
"types",
"bin",
"files",
];

const copyFiles = ["README.md", "LICENSE"];

// 2.x: nitropack ~> nitro
const mainPkgName = "nitropack";
const mirrrorPkgName = "nitro";

// Dirs
const mainDir = fileURLToPath(new URL("..", import.meta.url));
const mirrorDir = fileURLToPath(new URL("../.mirror", import.meta.url));

async function main() {
// Init mirror dir
await rm(mirrorDir, { recursive: true }).catch(() => {});
await mkdir(mirrorDir, { recursive: true });

// Copy package.json fields
const mainPkg = await readPackageJSON(mainDir);
const mirrorPkg: PackageJson = {
name: mirrrorPkgName,
version: `0.0.0-${mainPkg.name}-${mainPkg.version}`,
peerDependencies: {
[mainPkgName]: mainPkg.version!,
},
};
for (const field of copyPkgFields) {
if (mainPkg[field]) {
mirrorPkg[field] = mainPkg[field];
}
}
await writeFile(
join(mirrorDir, "package.json"),
JSON.stringify(mirrorPkg, null, 2)
);

// Generate subpath re-exports
for (const subpath of subpaths) {
await mkdir(join(mirrorDir, "dist", subpath), { recursive: true });
await writeFile(
join(mirrorDir, "dist", subpath, "index.mjs"),
`export * from "${mainPkgName}/${subpath}";`
);
await writeFile(
join(mirrorDir, "dist", subpath, "index.d.ts"),
`export * from "${mainPkgName}/${subpath}";`
);
await writeFile(
join(mirrorDir, "dist", subpath, "index.d.mts"),
`export * from "${mainPkgName}/${subpath}";`
);
await writeFile(
join(mirrorDir, `${subpath}.d.ts`),
`export * from "./dist/${subpath}";`
);
}

// Runtime Meta
await writeFile(
join(mirrorDir, "runtime-meta.mjs"),
`export * from "${mainPkgName}/runtime/meta";`
);
await writeFile(
join(mirrorDir, "runtime-meta.d.ts"),
`export * from "${mainPkgName}/runtime/meta";`
);

// Other files
for (const file of copyFiles) {
await cp(join(mainDir, file), join(mirrorDir, file));
}
}

// eslint-disable-next-line unicorn/prefer-top-level-await
main();
2 changes: 2 additions & 0 deletions src/rollup/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -379,6 +379,7 @@ export const plugins = [
"#build": buildDir,
"#nitro-internal-virtual/error-handler": nitro.options.errorHandler,
"#internal/nitro": join(runtimeDir, "_compat"),
"nitro/runtime": runtimeDir,
"nitropack/runtime": runtimeDir,
"~": nitro.options.srcDir,
"@/": nitro.options.srcDir,
Expand Down Expand Up @@ -445,6 +446,7 @@ export const plugins = [
"~~",
"@@/",
"virtual:",
"nitro/runtime",
"nitropack/runtime",
dirname(nitro.options.entry),
...(nitro.options.experimental.wasm
Expand Down
20 changes: 17 additions & 3 deletions test/fixture/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,40 @@
// (currently manually synced with top level tsconfig.json paths)
"paths": {
// CLI
"nitro/cli": ["../../src/cli"],
"nitropack/cli": ["../../src/cli"],
// Config
"nitro/config": ["../../src/config"],
"nitropack/config": ["../../src/config"],
// Core
"nitropack": ["../../src/core"],
"nitro/core": ["../../src/core"],
"nitropack/core": ["../../src/core"],
// Core alias
"nitro": ["../../src/core"],
"nitropack": ["../../src/core"],
// Runtime
"nitro/runtime": ["../../src/runtime"],
"nitropack/runtime": ["../../src/runtime"],
"nitropack/runtime/meta": ["../../runtime-meta"],
"nitro/runtime/meta": ["./runtime-meta"],
"nitropack/runtime/meta": ["./runtime-meta"],
"nitro/runtime/*": ["../../src/runtime/*"],
"nitropack/runtime/*": ["../../src/runtime/*"],
// Kit
"nitro/kit": ["../../src/kit"],
"nitropack/kit": ["../../src/kit"],
// Meta
"nitropack/meta": ["../../src/core/meta"],
"nitro/meta": ["../../src/meta"],
"nitropack/meta": ["../../src/meta"],
// Presets
"nitro/presets": ["../../src/presets"],
"nitropack/presets": ["../../src/presets"],
"nitro/presets/*": ["../../src/presets/*"],
"nitropack/presets/*": ["../../src/presets/*"],
// Rollup
"nitro/rollup": ["../../src/rollup"],
"nitropack/rollup": ["../../src/rollup"],
// Types
"nitro/types": ["../../src/types"],
"nitropack/types": ["../../src/types"],
// Virtual files
"#nitro-internal-virtual/*": ["../../src/types/virtual/*"],
Expand Down
22 changes: 19 additions & 3 deletions tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,26 +16,42 @@
"noEmit": true,
"paths": {
// CLI
"nitro/cli": ["./src/cli"],
"nitropack/cli": ["./src/cli"],
// Config
"nitro/config": ["./src/config"],
"nitropack/config": ["./src/config"],
// Core
"nitropack": ["./src/core"],
"nitro/core": ["./src/core"],
"nitropack/core": ["./src/core"],
// Core (alias)
"nitro": ["./src/core"],
"nitropack": ["./src/core"],
// Runtime
"nitro/runtime": ["./src/runtime"],
"nitropack/runtime": ["./src/runtime"],
"nitropack/runtime/meta": ["./runtime-meta"],
// Runtime subpaths
"nitro/runtime/*": ["./src/runtime/*"],
"nitropack/runtime/*": ["./src/runtime/*"],
// Runtime meta
"nitro/runtime/meta": ["./runtime-meta"],
"nitropack/runtime/meta": ["./runtime-meta"],
// Kit
"nitro/kit": ["./src/kit"],
"nitropack/kit": ["./src/kit"],
// Meta
"nitro/meta": ["./src/meta"],
"nitropack/meta": ["./src/meta"],
// Presets
"nitro/presets": ["./src/presets"],
"nitropack/presets": ["./src/presets"],
"nitro/presets/*": ["./src/presets/*"],
"nitropack/presets/*": ["./src/presets/*"],
// Rollup
"nitro/rollup": ["./src/rollup"],
"nitropack/rollup": ["./src/rollup"],
// Types
"nitro/types": ["./src/types"],
"nitropack/types": ["./src/types"],
// Virtual files
"#nitro-internal-virtual/*": ["./src/types/virtual/*"],
Expand All @@ -44,6 +60,6 @@
"#internal/nitro/*": ["./src/runtime/_compat/*"]
}
},
"include": ["src", "test"],
"include": ["src", "test", "scripts/gen-mirror.ts"],
"exclude": ["dist", "examples", "playground", "test/fixture"]
}
1 change: 0 additions & 1 deletion types.d.ts

This file was deleted.

Loading