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

feat(bun/run): Impl. npm_package_config #3661

Closed
wants to merge 192 commits into from
Closed
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
192 commits
Select commit Hold shift + click to select a range
e70700c
Impl. test
trnxdev Jul 17, 2023
4b26291
Fix missing import
trnxdev Jul 17, 2023
d37d8bd
oops
trnxdev Jul 17, 2023
c750eb5
Impl. npm_package_config
trnxdev Jul 17, 2023
03e26e3
Merge branch 'main' into tiramify/npm_pkg_cfg
trnxdev Jul 17, 2023
a256377
Fixes
trnxdev Jul 18, 2023
882732c
Merge branch 'main' into tiramify/npm_pkg_cfg
trnxdev Jul 18, 2023
adf1d59
Add support for numbers + booleans
trnxdev Jul 18, 2023
487a471
More fixes
trnxdev Jul 18, 2023
66b0ac3
Oops
trnxdev Jul 18, 2023
ea0fdb7
Oops again
trnxdev Jul 18, 2023
18ba515
Last oops
trnxdev Jul 18, 2023
40553a2
Add object support
trnxdev Jul 18, 2023
1012d32
Merge branch 'main' into tiramify/npm_pkg_cfg
trnxdev Jul 23, 2023
3c5c87b
Merge branch 'oven-sh:main' into tiramify/npm_pkg_cfg
trnxdev Jul 26, 2023
a57fc15
Little fixes
trnxdev Jul 26, 2023
ba36410
Add files (#3826)
colinhacks Jul 26, 2023
fd68a02
[install] handle `workspace:[alias@]range` (#3819)
alexlamsl Jul 26, 2023
5f86b94
Concurrent Transpiler (#3816)
Jarred-Sumner Jul 26, 2023
8c082f4
Don't hoist bun plugin in concurrent transpiler
Jarred-Sumner Jul 26, 2023
e466189
fix decorator and declare (#3828)
dylan-conway Jul 26, 2023
746060a
Disable concurrent transpiler in macros
Jarred-Sumner Jul 26, 2023
5dbdc19
Update macro-test.test.ts
Jarred-Sumner Jul 26, 2023
cf7b5a9
Fix broken anchors
colinhacks Jul 27, 2023
3cd0d31
[fetch] Ignore invalid Content-Encoding header (#3834)
Jarred-Sumner Jul 27, 2023
20eb161
Start time performance improvements to build tools (#3797)
Jarred-Sumner Jul 27, 2023
4b34152
Enable `Promise.withResolvers()`
Jarred-Sumner Jul 27, 2023
504f506
Remove lstat call in fs.watch (#3836)
Jarred-Sumner Jul 27, 2023
4476b2f
Make readdir() async, fix crash in large directory trees (#3838)
Jarred-Sumner Jul 27, 2023
82b77d5
Remove no longer needed flag from benchmark documentation (#3845)
lpinca Jul 27, 2023
654682c
Use putDirectIndex instead of initializeIndex (#3839)
Jarred-Sumner Jul 27, 2023
dbcfb95
Outdated copy
Jarred-Sumner Jul 27, 2023
c8ee378
Fix hot reloading in canary (#3848)
Jarred-Sumner Jul 27, 2023
4ee5c23
Make readFile() async (#3850)
Jarred-Sumner Jul 27, 2023
ea2a411
Create pull_request_template.md
Jarred-Sumner Jul 28, 2023
81baac6
Update pull_request_template.md
Jarred-Sumner Jul 28, 2023
a42724c
Update pull_request_template.md
Jarred-Sumner Jul 28, 2023
b132085
Update pull_request_template.md
Jarred-Sumner Jul 28, 2023
d385cc9
Update pull_request_template.md
Jarred-Sumner Jul 28, 2023
83bcd94
Update pull_request_template.md
Jarred-Sumner Jul 28, 2023
adacee8
Update pull_request_template.md
Jarred-Sumner Jul 28, 2023
5cf0f76
Resolve watch directories outside main thread + async iterator and sy…
cirospaciari Jul 28, 2023
e651d79
Uncomment test
Jarred-Sumner Jul 28, 2023
a6b8b1a
Add `Bun.isMainThread`
Jarred-Sumner Jul 28, 2023
83d47b6
Fix bug with // @bun annotation in main thread (#3855)
Jarred-Sumner Jul 28, 2023
23414b9
Update pull_request_template.md
Jarred-Sumner Jul 28, 2023
93165fb
Update pull_request_template.md
Jarred-Sumner Jul 28, 2023
9e42c10
Add todo
Jarred-Sumner Jul 28, 2023
00d4d1e
Fixes #3795 (#3856)
Jarred-Sumner Jul 28, 2023
23f0072
fix(tls) exposes native canonicalizeIP and fix rootCertificates (#3866)
cirospaciari Jul 28, 2023
5c625a6
Support file: URLs in `fetch` (#3858)
Jarred-Sumner Jul 28, 2023
c40fa72
fix the chunk boundary (`node:stream:createReadStream`) (#3853)
Hanaasagi Jul 28, 2023
0dbd6ee
feat(bun/test): Impl. expect().pass() & expect().fail() (#3843)
trnxdev Jul 28, 2023
a2b1de0
add fork to child_process (#3851)
sirenkovladd Jul 28, 2023
78ce9b0
mark tests as todo
Jarred-Sumner Jul 28, 2023
0147a7e
`MessageChannel` and `MessagePort` (#3860)
dylan-conway Jul 28, 2023
6deb13c
fix types and add message channel/port gc test
dylan-conway Jul 28, 2023
2bd1ee1
Mark broken test as todo
Jarred-Sumner Jul 28, 2023
56c5176
Fix assertion failure and possible infinite loop when printing as yar…
Jarred-Sumner Jul 29, 2023
7ce493a
Fixes #3868
Jarred-Sumner Jul 29, 2023
6459a82
optional parameter
dylan-conway Jul 29, 2023
6ab08ef
markBinding
dylan-conway Jul 29, 2023
1785c67
Fix bug with `/path/to/absolute/bun.lockb`
Jarred-Sumner Jul 29, 2023
79c6c45
Ignore when printing to stdout errors (#3869)
Jarred-Sumner Jul 29, 2023
9ff3ca0
Stat largefile test (#3870)
Jarred-Sumner Jul 29, 2023
282659b
Defer task destruction
Jarred-Sumner Jul 29, 2023
8d43a75
Update nodejs-apis.md
Jarred-Sumner Jul 29, 2023
06fbe66
Update nodejs-apis.md
Jarred-Sumner Jul 29, 2023
d20b7fd
typo spawn.md (#3875)
jh0rman Jul 29, 2023
f6080c7
fix: small error (#3878)
0xflotus Jul 29, 2023
ea81d3d
docs: fixed small errors (#3879)
0xflotus Jul 29, 2023
9376261
Make `bun:jsc` memoryUsage more accurate (#3876)
Jarred-Sumner Jul 29, 2023
b2aaa71
Improv. (#3885)
trnxdev Jul 30, 2023
4129e53
Fix coredump when reading an empty file(`node:stream:createReadStream…
Hanaasagi Jul 30, 2023
438da16
Fix memory leak (#3887)
Jarred-Sumner Jul 30, 2023
20f1d99
remove unnecessary move
Jarred-Sumner Jul 30, 2023
56b7150
Bump
Jarred-Sumner Jul 30, 2023
cb0d7e8
Fixes #3884
Jarred-Sumner Jul 30, 2023
79c927a
Fix issue with file loader (#3889)
Jarred-Sumner Jul 30, 2023
0c97d5c
Update install.md (#3890)
trnxdev Jul 30, 2023
fba8ae2
Support `bun .` to run the entry point (#3891)
Jarred-Sumner Jul 30, 2023
ecb86a0
skip flay test
Jarred-Sumner Jul 30, 2023
833ba09
Micro-optimize LineOffsetTable.generate
Jarred-Sumner Jul 30, 2023
4aa1827
FIx Uint8Array to a ReadableStream code snippet (#3895)
audothomas Jul 31, 2023
69f3c48
Fix memory leak in response.clone(), further reduce memory usage of R…
Jarred-Sumner Jul 31, 2023
e48db1b
Fix incorrect function
Jarred-Sumner Jul 31, 2023
9a732ca
Update arraybuffer-to-buffer.md (#3906)
huseeiin Jul 31, 2023
bef6418
Fixes #2935
Jarred-Sumner Jul 31, 2023
4d41c71
Add a test for TransformStream
Jarred-Sumner Jul 31, 2023
2916bd0
Fixes #1669
Jarred-Sumner Jul 31, 2023
a87aa06
More logging
Jarred-Sumner Jul 31, 2023
eba5ede
Add ecosystem guides (#3847)
colinhacks Jul 31, 2023
d701a10
Update titles, add Workspaces guide
colinhacks Jul 31, 2023
7a384de
Add env guides
colinhacks Jul 31, 2023
6b7d911
Switch table to headings in Nodejs compat page
colinhacks Jul 31, 2023
7b44223
Update stricjs
colinhacks Aug 1, 2023
bbbc0f5
throw error if node module does not exist (#3913)
sirenkovladd Aug 1, 2023
684ea36
Fix prisma.md typo (#3921)
emlez Aug 1, 2023
bb7025a
Implement Worker.prototype.threadId
Jarred-Sumner Aug 1, 2023
b7c031b
Update JSWorker.cpp
Jarred-Sumner Aug 1, 2023
2dcb92d
Update types
Jarred-Sumner Aug 1, 2023
134d6ca
Update worker.test.ts
dylan-conway Aug 2, 2023
76f7daf
add `BroadcastChannel` (#3922)
dylan-conway Aug 2, 2023
63c6883
Fix expanding on .env files (#3918)
cirospaciari Aug 2, 2023
266b350
Fix some flaky tests (#3919)
cirospaciari Aug 2, 2023
be38a91
close `BroadcastChannel` in tests
dylan-conway Aug 2, 2023
e946c80
clean declare structs (#3929)
cirospaciari Aug 2, 2023
270f37e
Update streams.md (#3926)
vjpr Aug 2, 2023
b362a2b
Fix path_watcher (#3920)
cirospaciari Aug 2, 2023
3bc9445
Fixes #3931 (#3933)
Jarred-Sumner Aug 2, 2023
0af4f14
Rewrite built-in modules to use CommonJS over ESM (#3814)
paperclover Aug 2, 2023
eb0667a
Fix
Jarred-Sumner Aug 3, 2023
4eeb37a
Implement `node:diagnostics_channel` (#3934)
Electroid Aug 3, 2023
124986a
Implement `node:worker_threads` (#3923)
Jarred-Sumner Aug 3, 2023
888aed5
Remove module and browser conditions (#3935)
paperclover Aug 3, 2023
5d8c105
Fix http write (#3939)
paperclover Aug 3, 2023
991ee2d
Dave/removedeprecation (#3938)
paperclover Aug 3, 2023
158fb70
Fix formatting
Electroid Aug 3, 2023
fb31931
typo (#3941)
paperclover Aug 3, 2023
b0efc1f
SvelteKit compatibility hack
Jarred-Sumner Aug 3, 2023
49c90ed
Fix the string encoding in `path.extname`. (#3949)
Hanaasagi Aug 3, 2023
52c7ffd
Minor optimization: Avoid extra copying when the chunk has only one e…
Hanaasagi Aug 3, 2023
f99eb86
[install] fix stale `bun.lockb` on workspaces (#3945)
alexlamsl Aug 3, 2023
b1192d2
Bunch of fixes (#3516)
Electroid Aug 3, 2023
6cfb3b5
Fix edge case in `path.relative`. (#3952)
Hanaasagi Aug 3, 2023
b3138d6
allow bundling builtins for browser (#3959)
dylan-conway Aug 3, 2023
7fcf80f
More guides and fixing links (#3960)
colinhacks Aug 3, 2023
de56a09
Add type tests, update modules.md
colinhacks Aug 4, 2023
a3d3b94
Add type tests
colinhacks Aug 4, 2023
ca40e9e
Update Node.js compat docs
colinhacks Aug 4, 2023
c9dc8d7
Update build-id (#3940)
Jarred-Sumner Aug 4, 2023
5db8a80
Implement Module.wrap()
Jarred-Sumner Aug 4, 2023
44127f6
Test for Module.wrap()
Jarred-Sumner Aug 4, 2023
afbc1ae
Make the test better
Jarred-Sumner Aug 4, 2023
3cf6436
Fix thread safety issue in async fs functions file paths (#3964)
Jarred-Sumner Aug 4, 2023
851e9eb
Add microbenchmark for private properties
Jarred-Sumner Aug 4, 2023
3fac6c1
feat(hot-clear-screen): clear terminal on hot reload (#3976)
simylein Aug 4, 2023
e9a9e47
Fix types (#3963)
colinhacks Aug 4, 2023
6cc2959
[install] store resolved workspace path in lockfile (#3974)
alexlamsl Aug 4, 2023
9573014
Buffer.copy should ignore out-of-range sourceEnd (#3971)
buffaybu Aug 4, 2023
d8c0ba3
FFI typo (#3973)
paperclover Aug 4, 2023
d32ab42
[types] fix `blob.json()` (#3995)
alexlamsl Aug 5, 2023
2c0462b
Fix incorrect docs
Jarred-Sumner Aug 5, 2023
6c4433f
Update import-meta.md
Jarred-Sumner Aug 5, 2023
991cfec
[install] handle `workspace:*` correctly (#3994)
alexlamsl Aug 5, 2023
5fee948
Fixes #3991
Jarred-Sumner Aug 5, 2023
b679bd2
fix macro string escaping (#3967)
dylan-conway Aug 5, 2023
e45d20e
Support --dev/-D and support more flags on bun install (#3989)
colinhacks Aug 5, 2023
fc6478b
Remove Bun.plugin transpiler hook, encourage usage of `--preload` ins…
paperclover Aug 5, 2023
b89947d
Fix(cli/init): support subpath entrypoint. (#4002)
Hanaasagi Aug 5, 2023
3431dbe
Implement --test-name-pattern (#3998)
paperclover Aug 6, 2023
4dc1ca9
feat: impl `dns.getServers` (#3982)
Hanaasagi Aug 6, 2023
ad893ab
Code coverage for `bun test` (#3975)
Jarred-Sumner Aug 6, 2023
fcbe06b
Fixes #3129 (#4018)
Jarred-Sumner Aug 6, 2023
2ec72a5
Update lol-html
Jarred-Sumner Aug 6, 2023
d9242bd
Update coverage.md
Jarred-Sumner Aug 6, 2023
95c5b9e
fix nan
Jarred-Sumner Aug 6, 2023
6bfbbe4
[install] handle `bun add` of existing `peerDependencies` correctly (…
alexlamsl Aug 7, 2023
12592a6
Running missing scripts exits with non-0 (#4026)
YashoSharma Aug 7, 2023
10fa1bf
Fixes #4020
Jarred-Sumner Aug 7, 2023
e547d76
Bind require.resolve() (#4030)
Jarred-Sumner Aug 7, 2023
44be2c4
Update build-id
Jarred-Sumner Aug 7, 2023
3eebdfa
Fixes #4010 (#4031)
Jarred-Sumner Aug 7, 2023
a3cd4fd
Fixes #4029 and fixes #4022 (#4032)
Jarred-Sumner Aug 7, 2023
f70f458
Fixes #4001 (#4034)
Jarred-Sumner Aug 7, 2023
884de74
Fix `path.normalize` edge case. (#4042)
Hanaasagi Aug 7, 2023
9137cc2
Improve plugin docs
colinhacks Aug 7, 2023
d8ebee3
WASM test analyzer (#4043)
Jarred-Sumner Aug 8, 2023
f2a6c8a
fix iterating headers with `set-cookie` (#4048)
dylan-conway Aug 8, 2023
8062976
implement `bun update` (#4046)
alexlamsl Aug 8, 2023
2703c29
implement fetching data urls (#4000)
dylan-conway Aug 8, 2023
2c38e45
add bun update to help menu
Jarred-Sumner Aug 8, 2023
555b46d
fix `worker.ref()`
dylan-conway Aug 8, 2023
58f46f7
Fix `Bun.hash` functions (#4054)
jhmaster2000 Aug 8, 2023
7563942
Add `env` option for `node:worker_threads` (#4052)
paperclover Aug 8, 2023
aad089f
Remove printf
Jarred-Sumner Aug 8, 2023
091c845
Enable `Headers.prototype.getSetCookie`
Jarred-Sumner Aug 8, 2023
069664d
import bun (#4055)
Jarred-Sumner Aug 8, 2023
456189f
1. Check if the argument is an empty string in `path.format`. (#4064)
Hanaasagi Aug 8, 2023
b902693
Update Worker.cpp
dylan-conway Aug 8, 2023
acfd36a
Fix require("console") #3820 (#4073)
paperclover Aug 9, 2023
c9ee514
Set exports to {} in user-constructed CommonJSModuleRecords (#4076)
paperclover Aug 9, 2023
230b5ef
feat(bun/test): Implement "toSatisfy" & "toIncludeRepeated" (fwup) (#…
trnxdev Aug 9, 2023
22b7ae2
worker tests (#4058)
paperclover Aug 9, 2023
de2b451
feat(bun:test) add support for test.each() and describe.each() (#4047)
jecquas Aug 9, 2023
94454e9
Fix one of the astro segfaults, also fix bun init version (#4079)
paperclover Aug 9, 2023
eb687b4
oops
trnxdev Jul 17, 2023
2d792f0
Impl. npm_package_config
trnxdev Jul 17, 2023
1cd2402
Add support for numbers + booleans
trnxdev Jul 18, 2023
f219744
More fixes
trnxdev Jul 18, 2023
fad8866
Add object support
trnxdev Jul 18, 2023
63320b9
Little fixes
trnxdev Jul 26, 2023
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
18 changes: 18 additions & 0 deletions src/cli/run_command.zig
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ const default_allocator = bun.default_allocator;
const C = bun.C;
const std = @import("std");

const PackageJSON = @import("../resolver/package_json.zig").PackageJSON;
const lex = bun.js_lexer;
const logger = @import("root").bun.logger;

Expand Down Expand Up @@ -230,6 +231,7 @@ pub const RunCommand = struct {
env: *DotEnv.Loader,
passthrough: []const string,
silent: bool,
package_json: ?*PackageJSON,
) !bool {
const shell_bin = findShell(env.map.get("PATH") orelse "", cwd) orelse return error.MissingShell;

Expand All @@ -241,6 +243,19 @@ pub const RunCommand = struct {

try replacePackageManagerRun(&copy_script, script);

if (package_json) |pkg_json| {
var i: usize = 0;
const prefix = "$npm_package_config_";
while (std.mem.indexOfPos(u8, copy_script.items, i, prefix)) |start| {
const end = std.mem.indexOfAnyPos(u8, copy_script.items, start + prefix.len, &std.ascii.whitespace) orelse copy_script.items.len;
const key = copy_script.items[start + prefix.len .. end];
i = end;
const value = pkg_json.npm_cfg_map.get(key) orelse continue;
i = start + value.len;
try copy_script.replaceRange(start, prefix.len + key.len, value);
}
}

var combined_script: []u8 = copy_script.items;

log("Script: \"{s}\"", .{combined_script});
Expand Down Expand Up @@ -1027,6 +1042,7 @@ pub const RunCommand = struct {
this_bundler.env,
passthrough,
ctx.debug.silent,
root_dir_info.enclosing_package_json,
)) {
return false;
}
Expand All @@ -1040,6 +1056,7 @@ pub const RunCommand = struct {
this_bundler.env,
passthrough,
ctx.debug.silent,
root_dir_info.enclosing_package_json,
)) return false;

temp_script_buffer[0.."post".len].* = "post".*;
Expand All @@ -1053,6 +1070,7 @@ pub const RunCommand = struct {
this_bundler.env,
passthrough,
ctx.debug.silent,
root_dir_info.enclosing_package_json
)) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/install/lockfile.zig
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub const Scripts = struct {
pub fn run(this: *Scripts, allocator: Allocator, env: *DotEnv.Loader, silent: bool, comptime hook: []const u8) !void {
for (@field(this, hook).items) |entry| {
if (comptime Environment.allow_assert) std.debug.assert(Fs.FileSystem.instance_loaded);
_ = try RunCommand.runPackageScript(allocator, entry.script, hook, entry.cwd, env, &.{}, silent);
_ = try RunCommand.runPackageScript(allocator, entry.script, hook, entry.cwd, env, &.{}, silent, null);
}
}

Expand Down
27 changes: 27 additions & 0 deletions src/node_fallbacks.zig
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const assert_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("assert@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/assert/package.json", ""),
};
const buffer_package_json = PackageJSON{
Expand All @@ -80,6 +81,7 @@ const buffer_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("buffer@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/buffer/package.json", ""),
};
const console_package_json = PackageJSON{
Expand All @@ -89,6 +91,7 @@ const console_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("console@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/console/package.json", ""),
};
const constants_package_json = PackageJSON{
Expand All @@ -98,6 +101,7 @@ const constants_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("constants@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/constants/package.json", ""),
};
const crypto_package_json = PackageJSON{
Expand All @@ -107,6 +111,7 @@ const crypto_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("crypto@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/crypto/package.json", ""),
};
const domain_package_json = PackageJSON{
Expand All @@ -116,6 +121,7 @@ const domain_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("domain@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/domain/package.json", ""),
};
const events_package_json = PackageJSON{
Expand All @@ -125,6 +131,7 @@ const events_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("events@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/events/package.json", ""),
};
const http_package_json = PackageJSON{
Expand All @@ -134,6 +141,7 @@ const http_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("http@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/http/package.json", ""),
};
const https_package_json = PackageJSON{
Expand All @@ -143,6 +151,7 @@ const https_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("https@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/https/package.json", ""),
};
const net_package_json = PackageJSON{
Expand All @@ -152,6 +161,7 @@ const net_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("net@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/net/package.json", ""),
};
const os_package_json = PackageJSON{
Expand All @@ -161,6 +171,7 @@ const os_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("os@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/os/package.json", ""),
};
const path_package_json = PackageJSON{
Expand All @@ -170,6 +181,7 @@ const path_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("path@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/path/package.json", ""),
};
const process_package_json = PackageJSON{
Expand All @@ -179,6 +191,7 @@ const process_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("process@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/process/package.json", ""),
};
const punycode_package_json = PackageJSON{
Expand All @@ -188,6 +201,7 @@ const punycode_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("punycode@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/punycode/package.json", ""),
};
const querystring_package_json = PackageJSON{
Expand All @@ -197,6 +211,7 @@ const querystring_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("querystring@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/querystring/package.json", ""),
};
const stream_package_json = PackageJSON{
Expand All @@ -206,6 +221,7 @@ const stream_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("stream@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/stream/package.json", ""),
};
const string_decoder_package_json = PackageJSON{
Expand All @@ -219,6 +235,7 @@ const string_decoder_package_json = PackageJSON{

.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/string_decoder/package.json", ""),
};

Expand All @@ -229,6 +246,7 @@ const sys_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("sys@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/sys/package.json", ""),
};
const timers_package_json = PackageJSON{
Expand All @@ -238,6 +256,7 @@ const timers_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("timers@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/timers/package.json", ""),
};
const tty_package_json = PackageJSON{
Expand All @@ -247,6 +266,7 @@ const tty_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("tty@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/tty/package.json", ""),
};
const url_package_json = PackageJSON{
Expand All @@ -256,6 +276,7 @@ const url_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("url@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/url/package.json", ""),
};
const util_package_json = PackageJSON{
Expand All @@ -265,6 +286,7 @@ const util_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("util@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/util/package.json", ""),
};
const zlib_package_json = PackageJSON{
Expand All @@ -274,6 +296,7 @@ const zlib_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("zlib@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/zlib/package.json", ""),
};

Expand All @@ -284,6 +307,7 @@ const node_fetch_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("node-fetch@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/node-fetch/package.json", ""),
};
const isomorphic_fetch_package_json = PackageJSON{
Expand All @@ -293,6 +317,7 @@ const isomorphic_fetch_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("isomorphic-fetch@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/isomorphic-fetch/package.json", ""),
};
const supports_color_package_json = PackageJSON{
Expand All @@ -302,6 +327,7 @@ const supports_color_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("supports-color@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/supports-color/package.json", ""),
};

Expand All @@ -312,6 +338,7 @@ const vercel_fetch_package_json = PackageJSON{
.hash = @truncate(u32, bun.hash("@vercel/fetch@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.npm_cfg_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/@vercel/fetch/package.json", ""),
};

Expand Down
20 changes: 19 additions & 1 deletion src/resolver/package_json.zig
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ const resolve_path = @import("./resolve_path.zig");
// Assume they're not going to have hundreds of main fields or browser map
// so use an array-backed hash table instead of bucketed
const MainFieldMap = bun.StringMap;
const NpmCfgMap = bun.StringMap;
pub const BrowserMap = bun.StringMap;
pub const MacroImportReplacementMap = bun.StringArrayHashMap(string);
pub const MacroMap = bun.StringArrayHashMapUnmanaged(MacroImportReplacementMap);
Expand Down Expand Up @@ -144,7 +145,7 @@ pub const PackageJSON = struct {
// should match and the query "./ext" should ALSO match.
//
browser_map: BrowserMap,

npm_cfg_map: NpmCfgMap,
exports: ?ExportsMap = null,
imports: ?ExportsMap = null,

Expand Down Expand Up @@ -644,6 +645,7 @@ pub const PackageJSON = struct {
.module_type = .unknown,
.browser_map = BrowserMap.init(allocator, false),
.main_fields = MainFieldMap.init(allocator, false),
.npm_cfg_map = NpmCfgMap.init(allocator, false),
};

// Note: we tried rewriting this to be fewer loops over all the properties (asProperty loops over each)
Expand All @@ -667,6 +669,22 @@ pub const PackageJSON = struct {
}
}

if (json.asProperty("config")) |npm_pkg_cfg| {
switch (npm_pkg_cfg.expr.data) {
.e_object => |obj| {
for (obj.properties.slice()) |*prop| {
const key = prop.key.?.asString(allocator) orelse continue;
const value = prop.value.?.asString(allocator) orelse continue;

if (!(key.len > 0 and value.len > 0)) continue;

package_json.npm_cfg_map.put(key, value) catch unreachable;
}
},
else => r.log.addWarning(&json_source, npm_pkg_cfg.loc, "The \"config\" field must be an object") catch unreachable,
}
}

// If we're coming from `bun run`
// We do not need to parse all this stuff.
if (comptime !include_scripts) {
Expand Down
36 changes: 35 additions & 1 deletion test/cli/run/run-cjs.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { expect, test } from "bun:test";
import { mkdirSync, realpathSync } from "fs";
import { bunEnv, bunExe } from "harness";
import { bunEnv, bunExe, tempDirWithFiles, bunRunAsScript } from "harness";
import { tmpdir } from "os";
import { join } from "path";

Expand Down Expand Up @@ -38,3 +38,37 @@ export default Foo
});
expect(stdout.toString("utf8")).toEqual("");
});

test("npm_package_config", () => {
const dir = tempDirWithFiles("npmpkgcfg", {
"package.json": JSON.stringify({
"name": "bun_npm_package_config",
"config": {
"a": "echo Hello, Bun!"
},
"scripts": {
"c": "$npm_package_config_a",
}
})
});

const { stdout } = bunRunAsScript(dir, "c");
expect(stdout.toString()).toBe("Hello, Bun!");

// test multiple config values
const dir2 = tempDirWithFiles("npmpkgcfg2", {
"package.json": JSON.stringify({
"name": "bun_npm_package_config",
"config": {
"a": "Bun!",
"b": "Hello,"
},
"scripts": {
"c": "echo $npm_package_config_b $npm_package_config_a",
}
})
});

const { stdout: stdout2 } = bunRunAsScript(dir2, "c");
expect(stdout2.toString()).toBe("Hello, Bun!");
});