diff options
author | 2021-09-12 01:53:32 -0700 | |
---|---|---|
committer | 2021-09-12 01:53:32 -0700 | |
commit | dca47a565e8960371dd623e978bd952c1b50048d (patch) | |
tree | f607d1b7c9c3291a04567b38084b5122628b1664 | |
parent | 19922287aadc78ac8c2aa97885386d4d3366a366 (diff) | |
download | bun-dca47a565e8960371dd623e978bd952c1b50048d.tar.gz bun-dca47a565e8960371dd623e978bd952c1b50048d.tar.zst bun-dca47a565e8960371dd623e978bd952c1b50048d.zip |
ok
-rw-r--r-- | src/linker.zig | 30 | ||||
-rw-r--r-- | src/options.zig | 12 | ||||
-rw-r--r-- | src/resolver/resolver.zig | 12 | ||||
-rw-r--r-- | src/runtime.js | 8 | ||||
-rw-r--r-- | src/runtime.version | 2 |
5 files changed, 44 insertions, 20 deletions
diff --git a/src/linker.zig b/src/linker.zig index 5787573b1..efcde5a24 100644 --- a/src/linker.zig +++ b/src/linker.zig @@ -219,6 +219,7 @@ pub fn NewLinker(comptime BundlerType: type) type { // runtime is included in the bundle, so we don't need to dynamically import it if (linker.options.node_modules_bundle != null) { import_record.path.text = linker.nodeModuleBundleImportPath(); + result.ast.runtime_import_record_id = record_index; } else { import_record.path = try linker.generateImportPath( source_dir, @@ -432,16 +433,20 @@ pub fn NewLinker(comptime BundlerType: type) type { if (result.ast.needs_runtime and result.ast.runtime_import_record_id == null) { var import_records = try linker.allocator.alloc(ImportRecord, result.ast.import_records.len + 1); std.mem.copy(ImportRecord, import_records, result.ast.import_records); + import_records[import_records.len - 1] = ImportRecord{ .kind = .stmt, - .path = try linker.generateImportPath( - source_dir, - linker.runtime_source_path, - Runtime.version(), - false, - "bun", - import_path_format, - ), + .path = if (linker.options.node_modules_bundle != null) + Fs.Path.init(linker.nodeModuleBundleImportPath()) + else + try linker.generateImportPath( + source_dir, + linker.runtime_source_path, + Runtime.version(), + false, + "bun", + import_path_format, + ), .range = logger.Range{ .loc = logger.Loc{ .start = 0 }, .len = 0 }, }; result.ast.runtime_import_record_id = @truncate(u32, import_records.len - 1); @@ -544,6 +549,10 @@ pub fn NewLinker(comptime BundlerType: type) type { ) !Fs.Path { switch (import_path_format) { .absolute_path => { + if (strings.eqlComptime(namespace, "node")) { + return Fs.Path.initWithNamespace(source_path, "node"); + } + var relative_name = linker.fs.relative(source_dir, source_path); return Fs.Path.initWithPretty(source_path, relative_name); @@ -599,9 +608,12 @@ pub fn NewLinker(comptime BundlerType: type) type { .absolute_url => { if (strings.eqlComptime(namespace, "node")) { + if (comptime isDebug) std.debug.assert(strings.eqlComptime(source_path[0..5], "node:")); + return Fs.Path.init(try std.fmt.allocPrint( linker.allocator, - "{s}/node:{s}", + // assumption: already starts with "node:" + "{s}/{s}", .{ linker.options.origin.origin, source_path, diff --git a/src/options.zig b/src/options.zig index 08121b05a..240ceeaaf 100644 --- a/src/options.zig +++ b/src/options.zig @@ -882,7 +882,17 @@ pub const BundleOptions = struct { }; pub const Defaults = struct { - pub var ExtensionOrder = [_]string{ ".tsx", ".ts", ".jsx", ".js", ".json", ".css" }; + pub const ExtensionOrder = [_]string{ + ".tsx", + ".ts", + ".jsx", + ".js", + ".json", + }; + + pub const CSSExtensionOrder = [_]string{ + ".css", + }; }; pub fn fromApi( diff --git a/src/resolver/resolver.zig b/src/resolver/resolver.zig index 5cf860a88..3502b29cf 100644 --- a/src/resolver/resolver.zig +++ b/src/resolver/resolver.zig @@ -316,6 +316,7 @@ pub fn NewResolver(cache_files: bool) type { log: *logger.Log, allocator: *std.mem.Allocator, node_module_bundle: ?*NodeModuleBundle, + extension_order: []const string = undefined, debug_logs: ?DebugLogs = null, elapsed: i128 = 0, // tracing @@ -379,6 +380,7 @@ pub fn NewResolver(cache_files: bool) type { resolver_Mutex = Mutex.init(); resolver_Mutex_loaded = true; } + return ThisResolver{ .allocator = allocator, .dir_cache = DirInfo.HashMap.init(allocator), @@ -388,6 +390,7 @@ pub fn NewResolver(cache_files: bool) type { .fs = _fs, .node_module_bundle = opts.node_modules_bundle, .log = log, + .extension_order = opts.extension_order, }; } @@ -559,6 +562,8 @@ pub fn NewResolver(cache_files: bool) type { } pub fn resolve(r: *ThisResolver, source_dir: string, import_path: string, kind: ast.ImportKind) !Result { + r.extension_order = if (kind.isFromCSS()) std.mem.span(&options.BundleOptions.Defaults.CSSExtensionOrder) else r.opts.extension_order; + if (FeatureFlags.tracing) { tracing_start = std.time.nanoTimestamp(); } @@ -728,6 +733,7 @@ pub fn NewResolver(cache_files: bool) type { } pub fn resolveWithoutSymlinks(r: *ThisResolver, source_dir: string, import_path: string, kind: ast.ImportKind) !?Result { + // This implements the module resolution algorithm from node.js, which is // described here: https://nodejs.org/api/modules.html#modules_all_together var result: Result = Result{ .path_pair = PathPair{ .primary = Path.empty } }; @@ -1641,7 +1647,7 @@ pub fn NewResolver(cache_files: bool) type { } var remapped = pkg.browser_map.get(cleaned); if (remapped == null) { - for (r.opts.extension_order) |ext| { + for (r.extension_order) |ext| { std.mem.copy(u8, &TemporaryBuffer.ExtensionPathBuf, cleaned); std.mem.copy(u8, TemporaryBuffer.ExtensionPathBuf[cleaned.len .. cleaned.len + ext.len], ext); const new_path = TemporaryBuffer.ExtensionPathBuf[0 .. cleaned.len + ext.len]; @@ -1831,7 +1837,7 @@ pub fn NewResolver(cache_files: bool) type { } pub fn loadAsFileOrDirectory(r: *ThisResolver, path: string, kind: ast.ImportKind) ?MatchResult { - const extension_order = r.opts.extension_order; + const extension_order = r.extension_order; // Is this a file? if (r.loadAsFile(path, extension_order)) |file| { @@ -2045,7 +2051,7 @@ pub fn NewResolver(cache_files: bool) type { // Try the path with extensions std.mem.copy(u8, &TemporaryBuffer.ExtensionPathBuf, path); - for (r.opts.extension_order) |ext| { + for (r.extension_order) |ext| { var buffer = TemporaryBuffer.ExtensionPathBuf[0 .. path.len + ext.len]; std.mem.copy(u8, buffer[path.len..buffer.len], ext); const file_name = buffer[path.len - base.len .. buffer.len]; diff --git a/src/runtime.js b/src/runtime.js index a90af7427..2bcbdacc5 100644 --- a/src/runtime.js +++ b/src/runtime.js @@ -121,12 +121,8 @@ export var __BUN_INTERNAL_DO_NOT_USE_OR_YOU_WILL_BE_FIRED = { // ); export var __require = (namespace) => { - if ( - typeof namespace === "object" && - "default" in namespace && - namespace.default[cjsRequireSymbol] - ) - return namespace.default(); + if (typeof namespace === "function" && namespace[cjsRequireSymbol]) + return namespace(); return namespace; // // is it an ESM module record? diff --git a/src/runtime.version b/src/runtime.version index 9e186b4f7..50cc16396 100644 --- a/src/runtime.version +++ b/src/runtime.version @@ -1 +1 @@ -b375f4919de4a554
\ No newline at end of file +56f0fd66cd90913c
\ No newline at end of file |