Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/workflows/release-npm.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }}
Expand Down
4 changes: 2 additions & 2 deletions npm/README.md
Original file line number Diff line number Diff line change
@@ -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

Expand Down
5 changes: 2 additions & 3 deletions npm/package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down Expand Up @@ -39,6 +39,5 @@
"aarch64-apple-darwin"
]
},
"optionalDependencies": {
}
"optionalDependencies": {}
}
18 changes: 0 additions & 18 deletions pnpm-lock.yaml

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

98 changes: 98 additions & 0 deletions scripts/prepublish.mjs
Original file line number Diff line number Diff line change
@@ -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 `<arch><sub>-<vendor>-<sys>-<abi>` 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"
);

}
9 changes: 7 additions & 2 deletions scripts/version.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -16,15 +21,15 @@ export async function getLastVersion(root) {
type: "json"
}
});
return result.default.version;
return result.default;
} catch (e) {
// Node < 20
const result = await import(pkgPath, {
assert: {
type: "json"
}
});
return result.default.version;
return result.default;
}
}

Expand Down
6 changes: 6 additions & 0 deletions scripts/x.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down
Loading