This commit is contained in:
Jarred Sumner
2023-05-30 02:51:27 -07:00
parent 481f916f3f
commit 12a0e2bfdd
2 changed files with 33 additions and 30 deletions

View File

@@ -800,8 +800,13 @@ pub const ModuleLoader = struct {
try dumpSource(specifier, &printer);
}
var commonjs_exports = try bun.default_allocator.alloc(ZigString, parse_result.ast.commonjs_export_names.len);
for (parse_result.ast.commonjs_export_names, commonjs_exports) |name, *out| {
out.* = ZigString.fromUTF8(name);
}
if (jsc_vm.isWatcherEnabled()) {
const resolved_source = jsc_vm.refCountedResolvedSource(printer.ctx.written, specifier, path.text, null);
var resolved_source = jsc_vm.refCountedResolvedSource(printer.ctx.written, specifier, path.text, null);
if (parse_result.input_fd) |fd_| {
if (jsc_vm.bun_watcher != null and std.fs.path.isAbsolute(path.text) and !strings.contains(path.text, "node_modules")) {
@@ -817,12 +822,18 @@ pub const ModuleLoader = struct {
}
}
return resolved_source;
}
resolved_source.commonjs_exports = if (commonjs_exports.len > 0)
commonjs_exports.ptr
else
null;
resolved_source.commonjs_exports_len = if (commonjs_exports.len > 0)
@truncate(u32, commonjs_exports.len)
else if (parse_result.ast.exports_kind == .cjs)
std.math.maxInt(u32)
else
0;
var commonjs_exports = try bun.default_allocator.alloc(ZigString, parse_result.ast.commonjs_export_names.len);
for (parse_result.ast.commonjs_export_names, commonjs_exports) |name, *out| {
out.* = ZigString.fromUTF8(name);
return resolved_source;
}
return ResolvedSource{
@@ -1156,17 +1167,27 @@ pub const ModuleLoader = struct {
try dumpSource(specifier, &printer);
}
if (jsc_vm.isWatcherEnabled()) {
const resolved_source = jsc_vm.refCountedResolvedSource(printer.ctx.written, display_specifier, path.text, null);
return resolved_source;
}
var commonjs_exports = try bun.default_allocator.alloc(ZigString, parse_result.ast.commonjs_export_names.len);
for (parse_result.ast.commonjs_export_names, commonjs_exports) |name, *out| {
out.* = ZigString.fromUTF8(name);
}
if (jsc_vm.isWatcherEnabled()) {
var resolved_source = jsc_vm.refCountedResolvedSource(printer.ctx.written, display_specifier, path.text, null);
resolved_source.commonjs_exports = if (commonjs_exports.len > 0)
commonjs_exports.ptr
else
null;
resolved_source.commonjs_exports_len = if (commonjs_exports.len > 0)
@truncate(u32, commonjs_exports.len)
else if (parse_result.ast.exports_kind == .cjs)
std.math.maxInt(u32)
else
0;
return resolved_source;
}
return .{
.allocator = null,
.source_code = ZigString.init(try default_allocator.dupe(u8, printer.ctx.getWritten())),

View File

@@ -184,16 +184,9 @@ pub const ServerEntryPoint = struct {
break :brk try std.fmt.allocPrint(
allocator,
\\//Auto-generated file
\\var cjsSymbol = Symbol.for("CommonJS");
\\var hmrSymbol = Symbol.for("BunServerHMR");
\\import * as start from '{s}{s}';
\\export * from '{s}{s}';
\\var entryNamespace = start;
\\var cjs = start?.default;
\\if (cjs && typeof cjs === 'function' && cjsSymbol in cjs) {{
\\ // if you module.exports = (class {{}}), don't call it
\\ entryNamespace = ("prototype" in cjs) ? cjs : cjs();
\\}}
\\if (typeof entryNamespace?.then === 'function') {{
\\ entryNamespace = entryNamespace.then((entryNamespace) => {{
\\ if(typeof entryNamespace?.default?.fetch === 'function') {{
@@ -220,23 +213,14 @@ pub const ServerEntryPoint = struct {
.{
dir_to_use,
original_path.filename,
dir_to_use,
original_path.filename,
},
);
}
break :brk try std.fmt.allocPrint(
allocator,
\\//Auto-generated file
\\var cjsSymbol = Symbol.for("CommonJS");
\\import * as start from '{s}{s}';
\\export * from '{s}{s}';
\\var entryNamespace = start;
\\var cjs = start?.default;
\\if (cjs && typeof cjs === 'function' && cjsSymbol in cjs) {{
\\ // if you module.exports = (class {{}}), don't call it
\\ entryNamespace = ("prototype" in cjs) ? cjs : cjs();
\\}}
\\if (typeof entryNamespace?.then === 'function') {{
\\ entryNamespace = entryNamespace.then((entryNamespace) => {{
\\ if(typeof entryNamespace?.default?.fetch === 'function') {{
@@ -251,8 +235,6 @@ pub const ServerEntryPoint = struct {
.{
dir_to_use,
original_path.filename,
dir_to_use,
original_path.filename,
},
);
};