Skip to content

Commit fe11f3e

Browse files
committed
npm
1 parent 343cf58 commit fe11f3e

13 files changed

+297
-18
lines changed

.cargo/config.toml

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
[build]
2+
target-dir = "target/.cargo"
3+
4+
[target.aarch64-unknown-linux-gnu]
5+
linker = "aarch64-linux-gnu-gcc"

justfile

+227-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,228 @@
1+
set windows-shell := ["pwsh", "-NoLogo", "-NoProfileLoadTime", "-Command"]
2+
3+
MACH_VERSION := env_var_or_default("MACH_VERSION", "")
4+
profile := env_var_or_default("profile", "debug")
5+
6+
os := \
7+
if \
8+
env_var_or_default("os", "") == "Windows_NT" { "windows" } \
9+
else if \
10+
env_var_or_default("os", "") != "" { env_var("os") } \
11+
else \
12+
{ os() }
13+
14+
arch := \
15+
if \
16+
env_var_or_default("arch", "") != "" { env_var("arch") } \
17+
else if \
18+
arch() == "x86_64" { "amd64" } \
19+
else if \
20+
arch() == "aarch64" { "arm64" } \
21+
else \
22+
{ arch() }
23+
24+
dylib := \
25+
if \
26+
os == "windows" { "dll" } \
27+
else if \
28+
os == "macos" { "dylib" } \
29+
else if \
30+
os == "linux" { "so" } \
31+
else \
32+
{ os() }
33+
34+
target := \
35+
if \
36+
os + arch == "linuxamd64" { "x86_64-unknown-linux-gnu" } \
37+
else if \
38+
os + arch == "linuxarm64" { "aarch64-unknown-linux-gnu" } \
39+
else if \
40+
os + arch == "macosamd64" { "x86_64-apple-darwin" } \
41+
else if\
42+
os + arch == "macosarm64" { "aarch64-apple-darwin" } \
43+
else if \
44+
os + arch == "windowsamd64" { "x86_64-pc-windows-msvc" } \
45+
else if \
46+
os + arch == "windowsarm64" { "aarch64-pc-windows-msvc" } \
47+
else \
48+
{ env_var_or_default("target", "debug") }
49+
50+
profile_cargo := \
51+
if \
52+
profile != "debug" { "--profile " + profile } \
53+
else \
54+
{ "" }
55+
56+
target_cargo := \
57+
if \
58+
target == "debug" { "" } \
59+
else if \
60+
target == "" { "" } \
61+
else \
62+
{ "--target " + target }
63+
64+
out_dir := join(justfile_directory(), "target", os + "-" + arch, profile)
65+
out_dir_link := join(justfile_directory(), "target", profile)
66+
67+
_default:
68+
@echo "Available Env:"
69+
@echo " profile"
70+
@echo " debug [default]"
71+
@echo " release"
72+
@echo " os"
73+
@echo " auto [default]"
74+
@echo " linux"
75+
@echo " macos"
76+
@echo " windows"
77+
@echo " arch"
78+
@echo " auto [default]"
79+
@echo " arm64"
80+
@echo " amd64"
81+
@just --list --unsorted
82+
83+
[unix]
184
build:
2-
cargo build
85+
# Install npm
86+
test -d node_modules || npm install
87+
88+
# Build crates
89+
cargo build {{profile_cargo}} {{target_cargo}}
90+
@cp "./target/.cargo/{{target}}/{{profile}}/libmach_bundler_npm_os_arch.{{dylib}}" "./packages/mach_nodejs/index.node"
91+
92+
# Clean dir
93+
@rm -rf "{{out_dir}}"
94+
@rm -rf "{{out_dir_link}}"
95+
@mkdir -p "{{out_dir}}"
96+
97+
# Copy binary
98+
@mkdir -p "{{out_dir}}/bin"
99+
@cp "./target/.cargo/{{target}}/{{profile}}/mach" "{{out_dir}}/bin"
100+
101+
[windows]
102+
build:
103+
# Install npm
104+
if (!(Test-Path 'node_modules')) { pnpm install }
105+
106+
# Build mach and napi
107+
cargo build {{profile_cargo}} {{target_cargo}}
108+
@Copy-Item ".\target\.cargo\{{target}}\{{profile}}\mach_bundler_npm_os_arch.{{dylib}}" -Destination ".\npm\mach-os-arch\platform\native\index.node" | Out-Null
109+
110+
# Clean dir
111+
@if (Test-Path {{out_dir}}) { Remove-Item -Recurse -Force {{out_dir}} | Out-Null }
112+
@if (Test-Path {{out_dir_link}}) { Remove-Item -Recurse -Force {{out_dir_link}} | Out-Null }
113+
@New-Item -ItemType "directory" -Force -Path "{{out_dir}}" | Out-Null
114+
115+
# Copy binary
116+
@New-Item -ItemType "directory" -Force -Path "{{out_dir}}\bin" | Out-Null
117+
@Copy-Item ".\target\.cargo\{{target}}\{{profile}}\mach.exe" -Destination "{{out_dir}}\bin" | Out-Null
118+
119+
# Copy Nodejs adapter
120+
@New-Item -ItemType "directory" -Force -Path "{{out_dir}}\nodejs" | Out-Null
121+
@Copy-Item ".\npm\mach-os-arch\cmd" -Destination "{{out_dir}}\nodejs" -Recurse | Out-Null
122+
@Copy-Item ".\npm\mach-os-arch\platform" -Destination "{{out_dir}}\nodejs" -Recurse | Out-Null
123+
@Copy-Item ".\npm\mach-os-arch\package.json" -Destination "{{out_dir}}\nodejs" -Recurse | Out-Null
124+
@New-Item -ItemType SymbolicLink -Path "{{out_dir_link}}" -Target "{{out_dir}}" | Out-Null
125+
126+
[unix]
127+
run *ARGS:
128+
just build
129+
{{out_dir}}/bin/mach {{ARGS}}
130+
131+
[windows]
132+
run *ARGS:
133+
just build
134+
{{out_dir}}/bin/mach.exe {{ARGS}}
135+
136+
[unix]
137+
example cmd fixture *ARGS:
138+
@just build
139+
cd ./examples/{{fixture}} && {{out_dir}}/bin/mach {{cmd}} {{ARGS}}
140+
141+
[windows]
142+
example cmd fixture *ARGS:
143+
@just build
144+
cd ./examples/{{fixture}} && {{out_dir}}/bin/mach.exe {{cmd}} {{ARGS}}
145+
146+
serve:
147+
npx http-server -p 3000 ./examples
148+
149+
integration-tests *ARGS:
150+
node --import ./testing/node_modules/tsx/dist/loader.mjs ./testing/setup.ts {{ARGS}}
151+
152+
unit-tests:
153+
cargo test
154+
155+
fmt:
156+
cargo +nightly fmt
157+
./.github/scripts/node_modules/.bin/prettier ./npm --write
158+
./.github/scripts/node_modules/.bin/prettier ./examples --write
159+
./.github/scripts/node_modules/.bin/prettier "./testing/tests" --write
160+
./.github/scripts/node_modules/.bin/prettier "./testing/setup.ts" --write
161+
./.github/scripts/node_modules/.bin/prettier "./testing/utils" --write
162+
163+
[unix]
164+
build-publish:
165+
pnpm i
166+
just build-publish-common
167+
just build
168+
cp "./README.md" "npm/mach"
169+
170+
[windows]
171+
build-publish:
172+
pnpm i
173+
just build-publish-common
174+
just build
175+
Copy-Item ".\README.md" -Destination "npm\mach" | Out-Null
176+
177+
[private]
178+
build-publish-common:
179+
node {{justfile_directory()}}/.github/scripts/ci/string-replace.mjs \
180+
"./crates/mach_bundler_cli/Cargo.toml" \
181+
"0.0.0-local" \
182+
{{MACH_VERSION}}
183+
184+
node {{justfile_directory()}}/.github/scripts/ci/string-replace.mjs \
185+
"./crates/mach_bundler_core/Cargo.toml" \
186+
"0.0.0-local" \
187+
{{MACH_VERSION}}
188+
189+
node {{justfile_directory()}}/.github/scripts/ci/string-replace.mjs \
190+
"./npm/mach/package.json" \
191+
"0.0.0-local" \
192+
"{{MACH_VERSION}}"
193+
194+
node {{justfile_directory()}}/.github/scripts/ci/json.mjs \
195+
"./npm/mach-os-arch/package.json" \
196+
"name" \
197+
"@alshdavid/mach-{{os}}-{{arch}}"
198+
199+
node {{justfile_directory()}}/.github/scripts/ci/json.mjs \
200+
"./npm/mach-os-arch/package.json" \
201+
"version" \
202+
"{{MACH_VERSION}}"
203+
204+
node {{justfile_directory()}}/.github/scripts/ci/json.mjs \
205+
"./npm/mach-os-arch/package.json" \
206+
"os.0" \
207+
$(node "{{justfile_directory()}}/.github/scripts/ci/map.mjs" "os" {{os}})
208+
209+
node {{justfile_directory()}}/.github/scripts/ci/json.mjs \
210+
"./npm/mach-os-arch/package.json" \
211+
"cpu.0" \
212+
$(node "{{justfile_directory()}}/.github/scripts/ci/map.mjs" "arch" {{arch}})
213+
214+
benchmark project="mach" count="50" script="build" *ARGS="":
215+
@just {{ if project == "mach" { "build" } else { "_skip" } }}
216+
just benchmark-generate {{project}} {{count}}
217+
cd benchmarks/{{project}}_{{count}} && \
218+
rm -rf dist && \
219+
CMD="console.log(require(\"./package.json\").scripts[\"build\"])" && \
220+
CMD="$(echo $CMD | node)" && \
221+
echo $CMD && \
222+
mach_profiler=../{{project}}_{{count}}.csv \
223+
time bash -c "$CMD {{ARGS}}"
224+
225+
benchmark-generate project="mach" count="50":
226+
PROJECT={{project}} \
227+
BENCH_COPIES={{count}} \
228+
node .github/scripts/ci/generate_benchmark.mjs
File renamed without changes.

packages/mach_nodejs/cjs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "commonjs" }

packages/mach_nodejs/jsconfig.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,5 +9,5 @@
99
"@alshdavid/mach": ["../mach_nodejs/types/index.d.ts"]
1010
}
1111
},
12-
"include": ["bin", "js"]
12+
"include": ["bin", "cjs", "mjs"]
1313
}

packages/mach_nodejs/mjs/index.js

Whitespace-only changes.

packages/mach_nodejs/mjs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }

packages/mach_nodejs/package.json

+5-2
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@
22
"name": "@alshdavid/mach-os-arch",
33
"version": "0.0.0-local",
44
"license": "MIT",
5-
"type": "module",
65
"bin": "./cmd/napi/main.js",
7-
"main": "./lib/index.js",
6+
"exports": {
7+
"require": "./cjs/index.js",
8+
"import": "./mjs/index.js",
9+
"default": "./cjs/index.js"
10+
},
811
"repository": {
912
"type": "git",
1013
"url": "git+https://github.com/alshdavid/mach.git"

packages/mach_npm/cjs/index.js

+41
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
const OS = {
2+
win32: 'windows',
3+
darwin: 'macos',
4+
linux: 'linux',
5+
}[process.platform]
6+
7+
const ARCH = {
8+
arm64: 'arm64',
9+
x64: 'amd64',
10+
}[process.arch]
11+
12+
let to_export = undefined
13+
14+
try {
15+
to_export = require(`@alshdavid/mach-${OS}-${ARCH}`)
16+
} catch (error) {
17+
const fs = require('node:fs')
18+
const path = require('node:path')
19+
20+
const package_json = JSON.parse(
21+
fs.readFileSync(path.join(__dirname, '..', 'package.json'), 'utf-8'),
22+
)
23+
if (package_json.version !== '0.0.0-local') {
24+
throw error
25+
}
26+
27+
to_export = require('@alshdavid/mach-os-arch')
28+
}
29+
30+
class MachInitError extends Error {
31+
constructor() {
32+
super('Mach is not initialized')
33+
throw this
34+
}
35+
}
36+
37+
module.exports.Mach = (to_export.Mach || globalThis.Mach?.Mach || MachInitError)
38+
module.exports.Resolver = (to_export.Resolver || globalThis.Mach?.Resolver || MachInitError)
39+
module.exports.Transformer = (to_export.Transformer || globalThis.Mach?.Transformer || MachInitError)
40+
module.exports.Dependency = (to_export.Dependency || globalThis.Mach?.Dependency || MachInitError)
41+
module.exports.MutableAsset = (to_export.MutableAsset || globalThis.Mach?.MutableAsset || MachInitError)

packages/mach_npm/cjs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "commonjs" }

packages/mach_npm/lib/index.js packages/mach_npm/mjs/index.js

+8-12
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ const ARCH = {
99
x64: 'amd64',
1010
}[process.arch]
1111

12-
let exports = undefined
12+
let to_export = undefined
1313

1414
try {
15-
exports = await import(`@alshdavid/mach-${OS}-${ARCH}/lib/index.js`)
15+
to_export = await import(`@alshdavid/mach-${OS}-${ARCH}/lib/index.js`)
1616
} catch (error) {
1717
const fs = await import('node:fs/promises')
1818
const path = await import('node:path')
@@ -27,7 +27,7 @@ try {
2727
throw error
2828
}
2929

30-
exports = await import('@alshdavid/mach-os-arch/lib/index.js')
30+
to_export = await import('@alshdavid/mach-os-arch')
3131
}
3232

3333
class MachInitError extends Error {
@@ -37,12 +37,8 @@ class MachInitError extends Error {
3737
}
3838
}
3939

40-
export const Mach = exports.Mach || globalThis.Mach?.Mach || MachInitError
41-
export const Resolver =
42-
exports.Resolver || globalThis.Mach?.Resolver || MachInitError
43-
export const Transformer =
44-
exports.Transformer || globalThis.Mach?.Transformer || MachInitError
45-
export const Dependency =
46-
exports.Dependency || globalThis.Mach?.Dependency || MachInitError
47-
export const MutableAsset =
48-
exports.MutableAsset || globalThis.Mach?.MutableAsset || MachInitError
40+
export const Mach = (to_export.Mach || globalThis.Mach?.Mach || MachInitError)
41+
export const Resolver = (to_export.Resolver || globalThis.Mach?.Resolver || MachInitError)
42+
export const Transformer = (to_export.Transformer || globalThis.Mach?.Transformer || MachInitError)
43+
export const Dependency = (to_export.Dependency || globalThis.Mach?.Dependency || MachInitError)
44+
export const MutableAsset = (to_export.MutableAsset || globalThis.Mach?.MutableAsset || MachInitError)

packages/mach_npm/mjs/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{ "type": "module" }

packages/mach_npm/package.json

+6-2
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,15 @@
33
"version": "0.0.0-local",
44
"license": "MIT",
55
"type": "module",
6-
"main": "./lib/index.js",
7-
"types": "./types/index.d.ts",
86
"bin": {
97
"mach": "./bin/mach.js"
108
},
9+
"exports": {
10+
"require": "./cjs/index.js",
11+
"import": "./mjs/index.js",
12+
"default": "./cjs/index.js",
13+
"types": "./types/index.d.ts"
14+
},
1115
"repository": {
1216
"type": "git",
1317
"url": "git+https://github.com/alshdavid/mach.git"

0 commit comments

Comments
 (0)