Former-commit-id: 1086d4f894611c1171f57d9eaf82f9170e9de2d1
This commit is contained in:
Jarred Sumner
2021-09-02 15:54:07 -07:00
parent 1b894ad9f6
commit d009e02c1d
41 changed files with 5132 additions and 2363 deletions

3
.gitignore vendored
View File

@@ -57,3 +57,6 @@ bun-mimalloc
examples/lotta-modules/bun-yday
examples/lotta-modules/bun-old
examples/lotta-modules/bun-nofscache
src/node-fallbacks/out/*
src/node-fallbacks/node_modules

View File

@@ -1,9 +1,20 @@
bun: bun-prod-native bun-prod-wasi bun-prod-wasm
bun: vendor bun-prod-native bun-prod-wasi bun-prod-wasm
vendor: api node-fallbacks runtime_js fallback_decoder mimalloc picohttp jsc
api:
peechy --schema src/api/schema.peechy --esm src/api/schema.js --ts src/api/schema.d.ts --zig src/api/schema.zig
node-fallbacks:
cd src/node-fallbacks; npm install; npm run --silent build
fallback_decoder:
esbuild --target=esnext --bundle src/fallback.ts --format=iife --platform=browser --minify > src/fallback.out.js
runtime_js:
esbuild --target=esnext --bundle src/runtime/index.ts --format=iife --platform=browser --global-name=BUN_RUNTIME --minify > src/runtime.out.js; cat src/runtime.footer.js >> src/runtime.out.js
jsc: jsc-build jsc-bindings
jsc-build: jsc-build-mac jsc-copy-headers
jsc-bindings:

View File

@@ -104,34 +104,31 @@ Unlike many other bundlers, `Bun` only bundles `node_modules`. This is great for
# Not implemented yet
# Building from source
Estimated: 30-60 minutes :(
You'll want to start downloading two things at once:
Compile Zig:
```bash
git clone https://github.com/jarred-sumner/zig && git checkout jarred/zig-sloppy-with-small-structs
```
```bash
git submodule update --init --recursive --progress --depth=1
```
Next, compile Zig.
On a Mac, that looks like this:
```bash
cmake . -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) -DZIG_STATIC_LLVM=ON -DCMAKE_BUILD_TYPE=Release && make -j 16
git clone https://github.com/jarred-sumner/zig
cd zig
git checkout jarred/zig-sloppy-with-small-structs
cmake . -DCMAKE_PREFIX_PATH=$(brew --prefix llvm) -DZIG_STATIC_LLVM=ON -DCMAKE_BUILD_TYPE=Release && make -j 16
```
Note that `brew install zig` won't work. Bun uses a build of Zig with a couple patches.
You'll want to make sure `zig` is in `$PATH`. The `zig` binary wil be in the same folder as the newly-cloned `zig` repo. If you use fish, you can run `fish_add_path (pwd)`.
In `bun`:
```bash
git submodule update --init --recursive --progress --depth=1
```
You
Now go back to the folder with `Bun`'s repository.
Run:

View File

@@ -76,8 +76,6 @@
"author": "",
"license": "ISC",
"dependencies": {
"buffer": "^6.0.3",
"path-browserify": "^1.0.1",
"react-is": "^17.0.2"
}
}

View File

@@ -417,12 +417,7 @@ export async function render({
(DocumentNamespace && DocumentNamespace.default) || NextDocument.default;
// Document.Html.prototype.getScripts = getScripts;
// }
console.log(
"next",
typeof NextDocument.default,
"doc",
typeof NextDocument.default.renderDocument
);
const callMiddleware = async (method: string, args: any[], props = false) => {
let results: any = props ? {} : [];

File diff suppressed because it is too large Load Diff

View File

@@ -35,6 +35,8 @@ const Css = @import("css_scanner.zig");
const DotEnv = @import("./env_loader.zig");
const Lock = @import("./lock.zig").Lock;
const NewBunQueue = @import("./bun_queue.zig").NewBunQueue;
const NodeFallbackModules = @import("./node_fallbacks.zig");
const CacheEntry = @import("./cache.zig").FsCacheEntry;
// How it works end-to-end
// 1. Resolve a file path from input using the resolver
@@ -96,7 +98,6 @@ pub fn NewBundler(cache_files: bool) type {
return struct {
pub const Linker = if (cache_files) linker.Linker else linker.ServeLinker;
pub const Resolver = if (cache_files) _resolver.Resolver else _resolver.ResolverUncached;
const ThisBundler = @This();
options: options.BundleOptions,
@@ -1025,7 +1026,18 @@ pub fn NewBundler(cache_files: bool) type {
pub fn get(this: *GenerateNodeModuleBundle, resolve_result: *const _resolver.Result) ?BundledModuleData {
const path = resolve_result.pathConst() orelse return null;
if (strings.eqlComptime(path.namespace, "node")) {
const _import_path = path.text["/bun-vfs/node_modules/".len..][resolve_result.package_json.?.name.len + 1 ..];
return BundledModuleData{
.import_path = _import_path,
.package_path = path.text["/bun-vfs/node_modules/".len..],
.package = resolve_result.package_json.?,
.module_id = resolve_result.package_json.?.hashModule(_import_path),
};
}
var base_path = path.text;
const package_json: *const PackageJSON = this.bundler.resolver.rootNodeModulePackageJSON(
resolve_result,
&base_path,
@@ -1072,15 +1084,40 @@ pub fn NewBundler(cache_files: bool) type {
var written: usize = undefined;
var code_offset: u32 = 0;
const entry = try bundler.resolver.caches.fs.readFileShared(
bundler.fs,
file_path.text,
resolve.dirname_fd,
if (resolve.file_fd != 0) resolve.file_fd else null,
shared_buffer,
);
const entry: CacheEntry = brk: {
if (!strings.eqlComptime(file_path.namespace, "node"))
break :brk try bundler.resolver.caches.fs.readFileShared(
bundler.fs,
file_path.text,
resolve.dirname_fd,
if (resolve.file_fd != 0) resolve.file_fd else null,
shared_buffer,
);
const module_data = BundledModuleData.get(this, &resolve) orelse return error.ResolveError;
var module_name = file_path.text["/bun-vfs/node_modules/".len..];
module_name = module_name[0..strings.indexOfChar(module_name, '/').?];
if (NodeFallbackModules.Map.get(module_name)) |mod| {
break :brk CacheEntry{ .contents = mod.code.* };
}
break :brk CacheEntry{
.contents = "",
};
};
const module_data = BundledModuleData.get(this, &resolve) orelse {
const source = logger.Source.initPathString(file_path.text, entry.contents);
this.log.addResolveError(
&source,
logger.Range.None,
this.allocator,
"Bug while resolving: \"{s}\"",
.{file_path.text},
resolve.import_kind,
) catch {};
return error.ResolveError;
};
const module_id = module_data.module_id;
const package = module_data.package;
const package_relative_path = module_data.import_path;

View File

@@ -14,6 +14,20 @@ const Mutex = @import("./lock.zig").Lock;
const import_record = @import("./import_record.zig");
const ImportRecord = import_record.ImportRecord;
pub const FsCacheEntry = struct {
contents: string,
fd: StoredFileDescriptorType = 0,
// Null means its not usable
mod_key: ?fs.FileSystem.Implementation.ModKey = null,
pub fn deinit(entry: *FsCacheEntry, allocator: *std.mem.Allocator) void {
if (entry.contents.len > 0) {
allocator.free(entry.contents);
entry.contents = "";
}
}
};
pub fn NewCache(comptime cache_files: bool) type {
return struct {
pub const Set = struct {
@@ -37,24 +51,12 @@ pub fn NewCache(comptime cache_files: bool) type {
}
};
pub const Fs = struct {
const Entry = FsCacheEntry;
mutex: Mutex,
entries: std.StringHashMap(Entry),
shared_buffer: MutableString,
pub const Entry = struct {
contents: string,
fd: StoredFileDescriptorType = 0,
// Null means its not usable
mod_key: ?fs.FileSystem.Implementation.ModKey = null,
pub fn deinit(entry: *Entry, allocator: *std.mem.Allocator) void {
if (entry.contents.len > 0) {
allocator.free(entry.contents);
entry.contents = "";
}
}
};
pub fn deinit(c: *Fs) void {
var iter = c.entries.iterator();
while (iter.next()) |entry| {

View File

@@ -178,6 +178,7 @@ pub const Arguments = struct {
clap.parseParam("-l, --loader <STR>... Parse files with .ext:loader, e.g. --loader .js:jsx. Valid loaders: jsx, js, json, tsx (not implemented yet), ts (not implemented yet), css (not implemented yet)") catch unreachable,
clap.parseParam("-o, --outdir <STR> Save output to directory (default: \"out\" if none provided and multiple entry points passed)") catch unreachable,
clap.parseParam("-r, --resolve <STR> Determine import/require behavior. \"disable\" ignores. \"dev\" bundles node_modules and builds everything else as independent entry points") catch unreachable,
// clap.parseParam("-r, --resolve <STR> Determine import/require behavior. \"disable\" ignores. \"dev\" bundles node_modules and builds everything else as independent entry points") catch unreachable,
clap.parseParam("<POS>... ") catch unreachable,
};

View File

@@ -1 +1 @@
f6c1d621b6e90172
703503c7cc54abc8

View File

@@ -1165,6 +1165,16 @@ pub const Path = struct {
};
}
pub fn initWithNamespaceVirtual(comptime text: string, comptime namespace: string, comptime package: string) Path {
return Path{
.pretty = comptime "node:" ++ package,
.is_symlink = true,
.text = text,
.namespace = namespace,
.name = PathName.init(text),
};
}
pub fn isBefore(a: *Path, b: Path) bool {
return a.namespace > b.namespace ||
(a.namespace == b.namespace and (a.text < b.text ||

View File

@@ -280,7 +280,7 @@ pub fn NewLinker(comptime BundlerType: type) type {
const package_relative_path = linker.fs.relative(
package_base_dir,
path.pretty,
if (!strings.eqlComptime(path.namespace, "node")) path.pretty else path.text,
);
const found_module = node_modules_bundle.findModuleInPackage(package, package_relative_path) orelse {

View File

@@ -0,0 +1 @@
export * from 'assert';

View File

@@ -0,0 +1 @@
export * from "buffer";

View File

@@ -0,0 +1 @@
module.exports = console;

View File

@@ -0,0 +1 @@
export * from "constants-browserify";

View File

@@ -0,0 +1 @@
export * from "crypto-browserify";

View File

@@ -0,0 +1 @@
export * from "domain-browser";

View File

@@ -0,0 +1 @@
export * from "events";

View File

@@ -0,0 +1 @@
export * from "stream-http";

View File

@@ -0,0 +1 @@
export * from "https-browserify";

1
src/node-fallbacks/os.js Normal file
View File

@@ -0,0 +1 @@
export * from "os-browserify/browser";

2046
src/node-fallbacks/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,37 @@
{
"name": "fallbacks",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1",
"build": "esbuild --bundle *.js --outdir=out --format=cjs --minify --platform=browser"
},
"author": "",
"license": "ISC",
"dependencies": {
"assert": "^2.0.0",
"browserify-zlib": "^0.2.0",
"buffer": "^6.0.3",
"console-browserify": "^1.2.0",
"constants-browserify": "^1.0.0",
"crypto-browserify": "^3.12.0",
"domain-browser": "^4.22.0",
"esbuild": "^0.12.25",
"events": "^3.3.0",
"https-browserify": "^1.0.0",
"os-browserify": "^0.3.0",
"path-browserify": "^1.0.1",
"process": "^0.11.10",
"punycode": "^2.1.1",
"querystring-es3": "^0.2.1",
"stream-browserify": "^3.0.0",
"stream-http": "^3.2.0",
"string_decoder": "^1.3.0",
"timers-browserify": "^2.0.12",
"tty-browserify": "^0.0.1",
"url": "^0.11.0",
"util": "^0.12.4",
"vm-browserify": "^1.1.2"
}
}

View File

@@ -0,0 +1 @@
export * from "path-browserify";

View File

@@ -0,0 +1 @@
export * from "process/browser";

View File

@@ -0,0 +1 @@
export * from "punycode";

View File

@@ -0,0 +1 @@
export * from "querystring-es3";

View File

@@ -0,0 +1 @@
export * from "stream-browserify";

View File

@@ -0,0 +1 @@
export * from "string_decoder";

View File

@@ -0,0 +1 @@
export * from "util";

View File

@@ -0,0 +1 @@
export * from "timers-browserify";

View File

@@ -0,0 +1,28 @@
{
"compilerOptions": {
"baseUrl": ".",
"paths": {
"assert": ["node_modules/assert"],
"buffer": ["node_modules/buffer"],
"constants": ["node_modules/constants-browserify"],
"crypto": ["node_modules/crypto-browserify"],
"domain": ["node_modules/domain-browser"],
"events": ["node_modules/events"],
"http": ["node_modules/stream-http"],
"https": ["node_modules/https-browserify"],
"os": ["node_modules/os-browserify/browser"],
"path": ["node_modules/path-browserify"],
"punycode": ["node_modules/punycode"],
"process": ["node_modules/process/browser"],
"querystring": ["node_modules/querystring-es3"],
"stream": ["node_modules/stream-browserify"],
"string_decoder": ["node_modules/string_decoder"],
"sys": ["node_modules/util"],
"timers": ["node_modules/timers-browserify"],
"tty": ["node_modules/tty-browserify"],
"url": ["node_modules/url"],
"util": ["node_modules/util"],
"zlib": ["node_modules/browserify-zlib"]
}
}
}

View File

@@ -0,0 +1 @@
export * from "tty-browserify";

View File

@@ -0,0 +1 @@
export * from "url";

View File

@@ -0,0 +1 @@
export * from "util";

View File

@@ -0,0 +1 @@
export * from "browserify-zlib";

420
src/node_fallbacks.zig Normal file
View File

@@ -0,0 +1,420 @@
const std = @import("std");
const string = @import("./string_types.zig").string;
const PackageJSON = @import("./resolver/package_json.zig").PackageJSON;
const logger = @import("./logger.zig");
const Fs = @import("./fs.zig");
const _assert_code: string = @embedFile("./node-fallbacks/out/assert.js");
const _buffer_code: string = @embedFile("./node-fallbacks/out/buffer.js");
const _console_code: string = @embedFile("./node-fallbacks/out/console.js");
const _constants_code: string = @embedFile("./node-fallbacks/out/constants.js");
const _crypto_code: string = @embedFile("./node-fallbacks/out/crypto.js");
const _domain_code: string = @embedFile("./node-fallbacks/out/domain.js");
const _events_code: string = @embedFile("./node-fallbacks/out/events.js");
const _http_code: string = @embedFile("./node-fallbacks/out/http.js");
const _https_code: string = @embedFile("./node-fallbacks/out/https.js");
const _os_code: string = @embedFile("./node-fallbacks/out/os.js");
const _path_code: string = @embedFile("./node-fallbacks/out/path.js");
const _process_code: string = @embedFile("./node-fallbacks/out/process.js");
const _punycode_code: string = @embedFile("./node-fallbacks/out/punycode.js");
const _querystring_code: string = @embedFile("./node-fallbacks/out/querystring.js");
const _stream_code: string = @embedFile("./node-fallbacks/out/stream.js");
const _string_decoder_code: string = @embedFile("./node-fallbacks/out/string_decoder.js");
const _sys_code: string = @embedFile("./node-fallbacks/out/sys.js");
const _timers_code: string = @embedFile("./node-fallbacks/out/timers.js");
const _tty_code: string = @embedFile("./node-fallbacks/out/tty.js");
const _url_code: string = @embedFile("./node-fallbacks/out/url.js");
const _util_code: string = @embedFile("./node-fallbacks/out/util.js");
const _zlib_code: string = @embedFile("./node-fallbacks/out/zlib.js");
const assert_code: *const string = &_assert_code;
const buffer_code: *const string = &_buffer_code;
const console_code: *const string = &_console_code;
const constants_code: *const string = &_constants_code;
const crypto_code: *const string = &_crypto_code;
const domain_code: *const string = &_domain_code;
const events_code: *const string = &_events_code;
const http_code: *const string = &_http_code;
const https_code: *const string = &_https_code;
const os_code: *const string = &_os_code;
const path_code: *const string = &_path_code;
const process_code: *const string = &_process_code;
const punycode_code: *const string = &_punycode_code;
const querystring_code: *const string = &_querystring_code;
const stream_code: *const string = &_stream_code;
const string_decoder_code: *const string = &_string_decoder_code;
const sys_code: *const string = &_sys_code;
const timers_code: *const string = &_timers_code;
const tty_code: *const string = &_tty_code;
const url_code: *const string = &_url_code;
const util_code: *const string = &_util_code;
const zlib_code: *const string = &_zlib_code;
const assert_import_path = "/bun-vfs/node_modules/assert/index.js";
const buffer_import_path = "/bun-vfs/node_modules/buffer/index.js";
const console_import_path = "/bun-vfs/node_modules/console/index.js";
const constants_import_path = "/bun-vfs/node_modules/constants/index.js";
const crypto_import_path = "/bun-vfs/node_modules/crypto/index.js";
const domain_import_path = "/bun-vfs/node_modules/domain/index.js";
const events_import_path = "/bun-vfs/node_modules/events/index.js";
const http_import_path = "/bun-vfs/node_modules/http/index.js";
const https_import_path = "/bun-vfs/node_modules/https/index.js";
const os_import_path = "/bun-vfs/node_modules/os/index.js";
const path_import_path = "/bun-vfs/node_modules/path/index.js";
const process_import_path = "/bun-vfs/node_modules/process/index.js";
const punycode_import_path = "/bun-vfs/node_modules/punycode/index.js";
const querystring_import_path = "/bun-vfs/node_modules/querystring/index.js";
const stream_import_path = "/bun-vfs/node_modules/stream/index.js";
const string_decoder_import_path = "/bun-vfs/node_modules/string_decoder/index.js";
const sys_import_path = "/bun-vfs/node_modules/sys/index.js";
const timers_import_path = "/bun-vfs/node_modules/timers/index.js";
const tty_import_path = "/bun-vfs/node_modules/tty/index.js";
const url_import_path = "/bun-vfs/node_modules/url/index.js";
const util_import_path = "/bun-vfs/node_modules/util/index.js";
const zlib_import_path = "/bun-vfs/node_modules/zlib/index.js";
const assert_package_json = PackageJSON{
.name = "assert",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "assert@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/assert/package.json", ""),
};
const buffer_package_json = PackageJSON{
.name = "buffer",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "buffer@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/buffer/package.json", ""),
};
const console_package_json = PackageJSON{
.name = "console",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "console@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/console/package.json", ""),
};
const constants_package_json = PackageJSON{
.name = "constants",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "constants@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/constants/package.json", ""),
};
const crypto_package_json = PackageJSON{
.name = "crypto",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "crypto@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/crypto/package.json", ""),
};
const domain_package_json = PackageJSON{
.name = "domain",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "domain@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/domain/package.json", ""),
};
const events_package_json = PackageJSON{
.name = "events",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "events@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/events/package.json", ""),
};
const http_package_json = PackageJSON{
.name = "http",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "http@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/http/package.json", ""),
};
const https_package_json = PackageJSON{
.name = "https",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "https@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/https/package.json", ""),
};
const os_package_json = PackageJSON{
.name = "os",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "os@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/os/package.json", ""),
};
const path_package_json = PackageJSON{
.name = "path",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "path@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/path/package.json", ""),
};
const process_package_json = PackageJSON{
.name = "process",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "process@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/process/package.json", ""),
};
const punycode_package_json = PackageJSON{
.name = "punycode",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "punycode@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/punycode/package.json", ""),
};
const querystring_package_json = PackageJSON{
.name = "querystring",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "querystring@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/querystring/package.json", ""),
};
const stream_package_json = PackageJSON{
.name = "stream",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "stream@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/stream/package.json", ""),
};
const string_decoder_package_json = PackageJSON{
.name = "string_decoder",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = brk: {
@setEvalBranchQuota(9999);
break :brk @truncate(u32, std.hash.Wyhash.hash(0, "string_decoder@0.0.0-polyfill"));
},
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/string_decoder/package.json", ""),
};
const sys_package_json = PackageJSON{
.name = "sys",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "sys@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/sys/package.json", ""),
};
const timers_package_json = PackageJSON{
.name = "timers",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "timers@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/timers/package.json", ""),
};
const tty_package_json = PackageJSON{
.name = "tty",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "tty@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/tty/package.json", ""),
};
const url_package_json = PackageJSON{
.name = "url",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "url@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/url/package.json", ""),
};
const util_package_json = PackageJSON{
.name = "util",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "util@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/util/package.json", ""),
};
const zlib_package_json = PackageJSON{
.name = "zlib",
.version = "0.0.0-polyfill",
.module_type = .cjs,
.hash = @truncate(u32, std.hash.Wyhash.hash(0, "zlib@0.0.0-polyfill")),
.main_fields = undefined,
.browser_map = undefined,
.source = logger.Source.initPathString("/bun-vfs/node_modules/zlib/package.json", ""),
};
pub const FallbackModule = struct {
path: Fs.Path,
code: *const string,
package_json: *const PackageJSON,
pub const @"assert" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(assert_import_path, "node", "assert"),
.code = assert_code,
.package_json = &assert_package_json,
};
pub const @"buffer" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(buffer_import_path, "node", "buffer"),
.code = buffer_code,
.package_json = &buffer_package_json,
};
pub const @"console" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(console_import_path, "node", "console"),
.code = console_code,
.package_json = &console_package_json,
};
pub const @"constants" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(constants_import_path, "node", "constants"),
.code = constants_code,
.package_json = &constants_package_json,
};
pub const @"crypto" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(crypto_import_path, "node", "crypto"),
.code = crypto_code,
.package_json = &crypto_package_json,
};
pub const @"domain" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(domain_import_path, "node", "domain"),
.code = domain_code,
.package_json = &domain_package_json,
};
pub const @"events" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(events_import_path, "node", "events"),
.code = events_code,
.package_json = &events_package_json,
};
pub const @"http" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(http_import_path, "node", "http"),
.code = http_code,
.package_json = &http_package_json,
};
pub const @"https" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(https_import_path, "node", "https"),
.code = https_code,
.package_json = &https_package_json,
};
pub const @"os" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(os_import_path, "node", "os"),
.code = os_code,
.package_json = &os_package_json,
};
pub const @"path" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(path_import_path, "node", "path"),
.code = path_code,
.package_json = &path_package_json,
};
pub const @"process" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(process_import_path, "node", "process"),
.code = process_code,
.package_json = &process_package_json,
};
pub const @"punycode" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(punycode_import_path, "node", "punycode"),
.code = punycode_code,
.package_json = &punycode_package_json,
};
pub const @"querystring" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(querystring_import_path, "node", "querystring"),
.code = querystring_code,
.package_json = &querystring_package_json,
};
pub const @"stream" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(stream_import_path, "node", "stream"),
.code = stream_code,
.package_json = &stream_package_json,
};
pub const @"string_decoder" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(string_decoder_import_path, "node", "string_decoder"),
.code = string_decoder_code,
.package_json = &string_decoder_package_json,
};
pub const @"sys" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(sys_import_path, "node", "sys"),
.code = sys_code,
.package_json = &sys_package_json,
};
pub const @"timers" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(timers_import_path, "node", "timers"),
.code = timers_code,
.package_json = &timers_package_json,
};
pub const @"tty" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(tty_import_path, "node", "tty"),
.code = tty_code,
.package_json = &tty_package_json,
};
pub const @"url" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(url_import_path, "node", "url"),
.code = url_code,
.package_json = &url_package_json,
};
pub const @"util" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(util_import_path, "node", "util"),
.code = util_code,
.package_json = &util_package_json,
};
pub const @"zlib" = FallbackModule{
.path = Fs.Path.initWithNamespaceVirtual(zlib_import_path, "node", "zlib"),
.code = zlib_code,
.package_json = &zlib_package_json,
};
};
pub const Map = std.ComptimeStringMap(FallbackModule, .{
&.{ "assert", FallbackModule.assert },
&.{ "buffer", FallbackModule.buffer },
&.{ "console", FallbackModule.console },
&.{ "constants", FallbackModule.constants },
&.{ "crypto", FallbackModule.crypto },
&.{ "domain", FallbackModule.domain },
&.{ "events", FallbackModule.events },
&.{ "http", FallbackModule.http },
&.{ "https", FallbackModule.https },
&.{ "os", FallbackModule.os },
&.{ "path", FallbackModule.path },
&.{ "process", FallbackModule.process },
&.{ "punycode", FallbackModule.punycode },
&.{ "querystring", FallbackModule.querystring },
&.{ "stream", FallbackModule.stream },
&.{ "string_decoder", FallbackModule.string_decoder },
&.{ "sys", FallbackModule.sys },
&.{ "timers", FallbackModule.timers },
&.{ "tty", FallbackModule.tty },
&.{ "url", FallbackModule.url },
&.{ "util", FallbackModule.util },
&.{ "zlib", FallbackModule.zlib },
});

View File

@@ -85,11 +85,11 @@ pub const ExternalModules = struct {
},
.bun => {
// TODO: fix this stupid copy
result.node_modules.hash_map.ensureCapacity(BunNodeBuiltinPatternsCompat.len) catch unreachable;
for (BunNodeBuiltinPatternsCompat) |pattern| {
result.node_modules.insert(pattern) catch unreachable;
}
// // TODO: fix this stupid copy
// result.node_modules.hash_map.ensureCapacity(BunNodeBuiltinPatternsCompat.len) catch unreachable;
// for (BunNodeBuiltinPatternsCompat) |pattern| {
// result.node_modules.insert(pattern) catch unreachable;
// }
},
else => {},
}
@@ -809,6 +809,7 @@ pub const BundleOptions = struct {
defines_loaded: bool = false,
env: Env = Env{},
transform_options: Api.TransformOptions,
polyfill_node_globals: bool = true,
pub inline fn cssImportBehavior(this: *const BundleOptions) Api.CssInJsBehavior {
switch (this.platform) {
@@ -1143,6 +1144,8 @@ pub const BundleOptions = struct {
opts.output_dir_handle = try openOutputDir(opts.output_dir);
}
opts.polyfill_node_globals = opts.platform != .node;
return opts;
}
};

View File

@@ -13,6 +13,8 @@ pub const DirInfo = @import("./dir_info.zig");
const HTTPWatcher = @import("../http.zig").Watcher;
const Wyhash = std.hash.Wyhash;
const NodeFallbackModules = @import("../node_fallbacks.zig");
const Mutex = @import("../lock.zig").Lock;
const StringBoolMap = std.StringHashMap(bool);
@@ -126,7 +128,7 @@ pub const Result = struct {
// checking package.json may not be relevant
pub fn isLikelyNodeModule(this: *const Result) bool {
const path_ = this.pathConst() orelse return false;
return strings.indexOf(path_.text, "/node_modules/") != null;
return this.is_from_node_modules or strings.indexOf(path_.text, "/node_modules/") != null;
}
// Most NPM modules are CommonJS
@@ -639,7 +641,9 @@ pub fn NewResolver(cache_files: bool) type {
return error.ModuleNotFound;
};
try r.finalizeResult(&result);
if (!strings.eqlComptime(result.path_pair.primary.namespace, "node"))
try r.finalizeResult(&result);
r.flushDebugLogs(.success) catch {};
result.import_kind = kind;
return result;
@@ -747,7 +751,6 @@ pub fn NewResolver(cache_files: bool) type {
.path_pair = res.path_pair,
.diff_case = res.diff_case,
.dirname_fd = dir_info.getFileDescriptor(),
.is_from_node_modules = res.is_node_module,
.package_json = res.package_json,
};
}
@@ -776,7 +779,6 @@ pub fn NewResolver(cache_files: bool) type {
.dirname_fd = entry.dirname_fd,
.path_pair = entry.path_pair,
.diff_case = entry.diff_case,
.is_from_node_modules = entry.is_node_module,
.package_json = entry.package_json,
};
}
@@ -832,7 +834,6 @@ pub fn NewResolver(cache_files: bool) type {
result = Result{
.path_pair = _result.path_pair,
.diff_case = _result.diff_case,
.is_from_node_modules = _result.is_node_module,
.module_type = pkg.module_type,
.dirname_fd = _result.dirname_fd,
.package_json = pkg,
@@ -851,7 +852,6 @@ pub fn NewResolver(cache_files: bool) type {
result = Result{
.path_pair = res.path_pair,
.diff_case = res.diff_case,
.is_from_node_modules = res.is_node_module,
.dirname_fd = res.dirname_fd,
.package_json = res.package_json,
};
@@ -862,6 +862,33 @@ pub fn NewResolver(cache_files: bool) type {
}
if (check_package) {
if (r.opts.polyfill_node_globals) {
var import_path_without_node_prefix = import_path;
const had_node_prefix = import_path_without_node_prefix.len > "node:".len and
strings.eqlComptime(import_path_without_node_prefix[0.."node:".len], "node:");
import_path_without_node_prefix = if (had_node_prefix)
import_path_without_node_prefix["node:".len..]
else
import_path_without_node_prefix;
if (NodeFallbackModules.Map.get(import_path_without_node_prefix)) |*fallback_module| {
result.path_pair.primary = fallback_module.path;
result.module_type = .cjs;
result.package_json = @intToPtr(*PackageJSON, @ptrToInt(fallback_module.package_json));
result.is_from_node_modules = true;
return result;
} else if (had_node_prefix) {
result.path_pair.primary.namespace = "node";
result.path_pair.primary.text = import_path_without_node_prefix;
result.path_pair.primary.name = Fs.PathName.init(import_path_without_node_prefix);
result.module_type = .cjs;
result.path_pair.primary.is_disabled = true;
result.is_from_node_modules = true;
return result;
}
}
// Check for external packages first
if (r.opts.external.node_modules.count() > 0) {
var query = import_path;
@@ -901,7 +928,6 @@ pub fn NewResolver(cache_files: bool) type {
.path_pair = pair,
.dirname_fd = node_module.dirname_fd,
.diff_case = node_module.diff_case,
.is_from_node_modules = true,
.package_json = package_json,
};
}
@@ -922,7 +948,6 @@ pub fn NewResolver(cache_files: bool) type {
result = Result{
.path_pair = res.path_pair,
.diff_case = res.diff_case,
.is_from_node_modules = res.is_node_module,
.dirname_fd = res.dirname_fd,
.package_json = res.package_json,
};

View File

@@ -1 +1 @@
494c65d79c81935d
ed96593b8e099dd5