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
16 changes: 14 additions & 2 deletions 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 All @@ -12,8 +13,9 @@ export const subpaths = [
"kit",
"presets",
"rollup",
"types",
"runtime",
"meta",
"types",
];

export default defineBuildConfig({
Expand Down Expand Up @@ -50,6 +52,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 +71,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.

13 changes: 5 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
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.

107 changes: 107 additions & 0 deletions scripts/gen-mirror.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
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";
import { e } from "crossws/dist/shared/crossws.381454fe";

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

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

async function main() {
// Dirs
const mainDir = fileURLToPath(new URL("..", import.meta.url));
const mirrorDir = fileURLToPath(new URL("../.mirror", import.meta.url));
await rm(mirrorDir, { recursive: true }).catch(() => {});
await mkdir(mirrorDir, { recursive: true });

// Read main package
const mainPkg = await readPackageJSON(mainDir);

// Check for nightly
const isNightly = mainPkg.name!.includes("nightly");

// Mirror nitro<>nitropack
const mirrrorPkgName = mainPkg.name!.includes("pack")
? mainPkg.name!.replace("pack", "")
: mainPkg.name!.replace("nitro", "nitropack");

// Canonical name for main pkg (without -nightly suffix)
const canonicalName = mainPkg.name!.replace("-nightly", "");

// Copy package.json fields
const mirrorPkg: PackageJson = {
name: mirrrorPkgName,
version: `${mainPkg.version}-${mainPkg.name}-mirror`,
dependencies: {},
};

// Add dependency
if (isNightly) {
mirrorPkg.dependencies![canonicalName] =
`npm:${mainPkg.name}@${mainPkg.version}`;
} else {
mirrorPkg.dependencies![canonicalName] = `${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 "${canonicalName}/${subpath}";`
);
await writeFile(
join(mirrorDir, "dist", subpath, "index.d.ts"),
`export * from "${canonicalName}/${subpath}";`
);
await writeFile(
join(mirrorDir, "dist", subpath, "index.d.mts"),
`export * from "${canonicalName}/${subpath}";`
);
await writeFile(
join(mirrorDir, `${subpath}.d.ts`),
`export * from "./dist/${subpath}";`
);
}

// Runtime Meta
await writeFile(
join(mirrorDir, "runtime-meta.mjs"),
`export * from "${canonicalName}/runtime/meta";`
);
await writeFile(
join(mirrorDir, "runtime-meta.d.ts"),
`export * from "${canonicalName}/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: 1 addition & 1 deletion scripts/gen-presets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ for (const preset of allPresets) {
const names = [preset._meta.name, ...(preset._meta.aliases || [])];
for (const name of names) {
if (_names.has(name)) {
if (!preset._meta.compatibility?.date) {
if (!preset._meta.compatibilityDate) {
consola.warn(`Preset ${name} is duplicated`);
}
continue;
Expand Down
10 changes: 9 additions & 1 deletion scripts/release-nightly.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ pnpm changelogen --bump
# Bump versions to nightly
pnpm jiti ./scripts/bump-nightly

# Build mirror
pnpm gen-mirror

# Resolve lockfile
# pnpm install

Expand All @@ -26,5 +29,10 @@ if [[ ! -z ${NODE_AUTH_TOKEN} ]] ; then
fi

# Release packages
echo "Publishing package..."

echo "Publishing main package..."
npm publish --access public --tolerate-republish

echo "Publishing mirror package..."
cd .mirror
npm publish --access public --tolerate-republish
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": runtimeDir,
"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/*"]
}
},
"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.