diff --git a/.github/workflows/release-npm.yml b/.github/workflows/release-npm.yml index 65b48342..51b7a517 100644 --- a/.github/workflows/release-npm.yml +++ b/.github/workflows/release-npm.yml @@ -130,7 +130,8 @@ jobs: git status cp napi/{index,browser}.js npm cp napi/index.d.ts npm - pnpm node scripts/x.mjs publish --otp --tag ${{inputs.tag}} ${{inputs.dry_run && '--dry-run' || '--no-dry-run'}} ${{inputs.push_tags && '--push-tags' || '--no-push-tags'}} + pnpm node scripts/x.mjs prepublish + pnpm node scripts/x.mjs publish --otp --tag ${{inputs.tag}} ${{inputs.dry_run && '--dry-run' || '--no-dry-run'}} ${{inputs.push_tags && '--push-tags' || '--no-push-tags'}} env: NPM_TOKEN: ${{ secrets.NPM_TOKEN }} REPOSITORY: ${{ github.repository }} diff --git a/npm/README.md b/npm/README.md index be3253e2..99c32d8d 100644 --- a/npm/README.md +++ b/npm/README.md @@ -1,9 +1,9 @@ -# Oxc Resolver Napi Binding +# Rspack Resolver Napi Binding See * `index.d.ts` for `resolveSync` and `ResolverFactory` API. -* [README.md](https://github.com/oxc-project/oxc-resolver?tab=readme-ov-file#oxc-resolver) for options. +* [README.md](https://github.com/web-infra-dev/rspack-resolver?tab=readme-ov-file#rspack-resolver) for options. ## API diff --git a/npm/package.json b/npm/package.json index 5b93a42a..085ac79e 100644 --- a/npm/package.json +++ b/npm/package.json @@ -1,6 +1,6 @@ { "name": "@rspack/resolver", - "version": "1.12.0", + "version": "0.1.2", "description": "Rspack Resolver Node API", "main": "index.js", "browser": "browser.js", @@ -39,6 +39,5 @@ "aarch64-apple-darwin" ] }, - "optionalDependencies": { - } + "optionalDependencies": {} } diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 3e47a282..6be6aee2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -42,24 +42,6 @@ importers: specifier: ^8.5.2 version: 8.5.2 - bindings/darwin-arm64: {} - - bindings/darwin-x64: {} - - bindings/linux-arm64-gnu: {} - - bindings/linux-arm64-musl: {} - - bindings/linux-x64-gnu: {} - - bindings/linux-x64-musl: {} - - bindings/win32-arm64-msvc: {} - - bindings/win32-ia32-msvc: {} - - bindings/win32-x64-msvc: {} - fixtures/pnpm: devDependencies: axios: diff --git a/scripts/prepublish.mjs b/scripts/prepublish.mjs new file mode 100644 index 00000000..0e7d9c48 --- /dev/null +++ b/scripts/prepublish.mjs @@ -0,0 +1,98 @@ +import * as path from "node:path"; +import fs from "node:fs/promises"; +import {getPackageJson} from "./version.mjs"; + +const CpuToNodeArch = { + x86_64: "x64", + aarch64: "arm64", + i686: "ia32", + armv7: "arm" +}; + +const NodeArchToCpu = { + x64: "x86_64", + arm64: "aarch64", + ia32: "i686", + arm: "armv7" +}; + +const SysToNodePlatform = { + linux: "linux", + freebsd: "freebsd", + darwin: "darwin", + windows: "win32" +}; + +const AbiToNodeLibc = { + gnu: "glibc", + musl: "musl" +}; + +const UniArchsByPlatform = { + darwin: ["x64", "arm64"] +}; + +/** + * A triple is a specific format for specifying a target architecture. + * Triples may be referred to as a target triple which is the architecture for the artifact produced, and the host triple which is the architecture that the compiler is running on. + * The general format of the triple is `---` where: + * - `arch` = The base CPU architecture, for example `x86_64`, `i686`, `arm`, `thumb`, `mips`, etc. + * - `sub` = The CPU sub-architecture, for example `arm` has `v7`, `v7s`, `v5te`, etc. + * - `vendor` = The vendor, for example `unknown`, `apple`, `pc`, `nvidia`, etc. + * - `sys` = The system name, for example `linux`, `windows`, `darwin`, etc. none is typically used for bare-metal without an OS. + * - `abi` = The ABI, for example `gnu`, `android`, `eabi`, etc. + */ +function parseTriple(rawTriple) { + const triple = rawTriple.endsWith("eabi") + ? `${rawTriple.slice(0, -4)}-eabi` + : rawTriple; + const triples = triple.split("-"); + let cpu; + let sys; + let abi = null; + if (triples.length === 4) { + [cpu, , sys, abi = null] = triples; + } else if (triples.length === 3) { + [cpu, , sys] = triples; + } else { + [cpu, sys] = triples; + } + const platformName = SysToNodePlatform[sys] ?? sys; + const arch = CpuToNodeArch[cpu] ?? cpu; + return { + platform: platformName, + arch, + abi, + platformArchABI: abi + ? `${platformName}-${arch}-${abi}` + : `${platformName}-${arch}`, + raw: rawTriple + }; +} + + +export async function prepublish_handler(options) { + let root = process.cwd(); + let json = await getPackageJson(root) + + let {napi, version} = json; + + let optionalDependencies = {}; + for (let rawTarget of napi.targets) { + let target = parseTriple(rawTarget); + optionalDependencies[`${napi.packageName}-${target.platformArchABI}`] = version; + } + + const packageFile = path.resolve(process.cwd(), "npm/package.json") + let newPackageJson = { + ...json, + optionalDependencies + } + + await fs.writeFile( + packageFile, + `${JSON.stringify(newPackageJson, null, 2)}\n`, + "utf-8" + ); + +} diff --git a/scripts/version.mjs b/scripts/version.mjs index 5a75665f..dad00a79 100644 --- a/scripts/version.mjs +++ b/scripts/version.mjs @@ -7,6 +7,11 @@ async function getCommitId() { } export async function getLastVersion(root) { + let pkg = await getPackageJson(root); + return pkg.version +} + +export async function getPackageJson(root) { const pkgPath = path.resolve(root, "./npm/package.json"); try { @@ -16,7 +21,7 @@ export async function getLastVersion(root) { type: "json" } }); - return result.default.version; + return result.default; } catch (e) { // Node < 20 const result = await import(pkgPath, { @@ -24,7 +29,7 @@ export async function getLastVersion(root) { type: "json" } }); - return result.default.version; + return result.default; } } diff --git a/scripts/x.mjs b/scripts/x.mjs index 24aa060c..5d78b7f4 100644 --- a/scripts/x.mjs +++ b/scripts/x.mjs @@ -6,6 +6,7 @@ import { Command } from "commander"; import { publish_handler } from "./publish.mjs"; import { version_handler } from "./version.mjs"; +import {prepublish_handler} from "./prepublish.mjs"; process.env.CARGO_TERM_COLOR = "always"; // Assume every terminal that using zx supports color process.env.FORCE_COLOR = 3; // Fix zx losing color output in subprocesses @@ -39,6 +40,11 @@ program .description("bump version") .action(version_handler); +program + .command("prepublish") + .description("prepublishOnly") + .action(prepublish_handler); + let argv = process.argv.slice(2); // remove the `node` and script call if (argv[0] && /x.mjs/.test(argv[0])) { // Called from `zx x.mjs`