Split up + generate client & server bundles, support framework +router in GenerateNodeModulesBundle , read framework from package.json + rename "publicURL" to "origin" + add import.meta.filepath

Former-commit-id: 1e76ebb5375247231181ec19a6396c6acf4684fb
This commit is contained in:
Jarred Sumner
2021-08-09 02:21:31 -07:00
parent 687b22908f
commit f74771144e
35 changed files with 2845 additions and 2000 deletions

View File

@@ -185,6 +185,8 @@ pub const To = struct {
pub const Properties = struct {
pub const UTF8 = struct {
pub var filepath: string = "filepath";
pub const module: string = "module";
pub const globalThis: string = "globalThis";
pub const exports: string = "exports";
@@ -245,6 +247,8 @@ pub const Properties = struct {
};
pub const Refs = struct {
pub var filepath: js.JSStringRef = undefined;
pub var module: js.JSStringRef = undefined;
pub var globalThis: js.JSStringRef = undefined;
pub var exports: js.JSStringRef = undefined;
@@ -283,7 +287,7 @@ pub const Properties = struct {
@field(UTF8, name).len,
);
if (isDebug) {
if (comptime isDebug) {
std.debug.assert(
js.JSStringIsEqualToString(@field(Refs, name), @field(UTF8, name).ptr, @field(UTF8, name).len),
);

View File

@@ -290,16 +290,26 @@ JSC::JSObject *GlobalObject::moduleLoaderCreateImportMetaProperties(JSGlobalObje
JSValue key,
JSModuleRecord *record,
JSValue val) {
return nullptr;
// auto res = Zig__GlobalObject__createImportMetaProperties(
// globalObject,
// loader,
// JSValue::encode(key),
// record,
// JSValue::encode(val)
// );
// return JSValue::decode(res).getObject();
ZigString specifier = Zig::toZigString(key, globalObject);
JSC::VM &vm = globalObject->vm();
auto scope = DECLARE_THROW_SCOPE(vm);
JSC::JSObject *metaProperties =
JSC::constructEmptyObject(vm, globalObject->nullPrototypeObjectStructure());
RETURN_IF_EXCEPTION(scope, nullptr);
metaProperties->putDirect(vm, Identifier::fromString(vm, "filePath"), key);
RETURN_IF_EXCEPTION(scope, nullptr);
// metaProperties->putDirect(vm, Identifier::fromString(vm, "resolve"),
// globalObject->globalThis()
// ->get(vm, Identifier::fromString("Wundle"))
// .getObject()
// ->get(vm, Identifier::fromString("resolve"))); );
// RETURN_IF_EXCEPTION(scope, nullptr);
return metaProperties;
}
JSC::JSValue GlobalObject::moduleLoaderEvaluate(JSGlobalObject *globalObject,

View File

@@ -401,6 +401,17 @@ JSC__JSValue ZigString__toValue(ZigString arg0, JSC__JSGlobalObject *arg1) {
return JSC::JSValue::encode(JSC::JSValue(JSC::jsOwnedString(arg1->vm(), Zig::toString(arg0))));
}
JSC__JSValue ZigString__toValueGC(ZigString arg0, JSC__JSGlobalObject *arg1) {
return JSC::JSValue::encode(
JSC::JSValue(JSC::jsMakeNontrivialString(arg1->vm(), Zig::toString(arg0))));
}
void JSC__JSValue__toZigString(JSC__JSValue JSValue0, ZigString *arg1, JSC__JSGlobalObject *arg2) {
JSC::JSValue value = JSC::JSValue::decode(JSValue0);
auto str = value.toWTFString(arg2);
arg1->ptr = str.characters8();
arg1->len = str.length();
}
JSC__JSValue ZigString__toErrorInstance(const ZigString *str, JSC__JSGlobalObject *globalObject) {
JSC::VM &vm = globalObject->vm();

View File

@@ -110,6 +110,10 @@ pub const ZigString = extern struct {
return shim.cppFn("toValue", .{ this, global });
}
pub fn toValueGC(this: ZigString, global: *JSGlobalObject) JSValue {
return shim.cppFn("toValueGC", .{ this, global });
}
pub fn toJSStringRef(this: *const ZigString) C_API.JSStringRef {
return C_API.JSStringCreateStatic(this.ptr, this.len);
}
@@ -120,6 +124,7 @@ pub const ZigString = extern struct {
pub const Extern = [_][]const u8{
"toValue",
"toValueGC",
"toErrorInstance",
};
};
@@ -1319,6 +1324,16 @@ pub const JSValue = enum(i64) {
return cppFn("toZigException", .{ this, global, exception });
}
pub fn toZigString(this: JSValue, out: *ZigString, global: *JSGlobalObject) void {
return cppFn("toZigString", .{ this, out, global });
}
pub inline fn getZigString(this: JSValue, global: *JSGlobalObject) ZigString {
var str = ZigString.init("");
this.toZigString(&str, global);
return str;
}
// On exception, this returns the empty string.
pub fn toString(this: JSValue, globalThis: *JSGlobalObject) *JSString {
return cppFn("toString", .{ this, globalThis });
@@ -1415,7 +1430,7 @@ pub const JSValue = enum(i64) {
return @intToPtr(*c_void, @intCast(usize, @enumToInt(this)));
}
pub const Extern = [_][]const u8{ "createStringArray", "createEmptyObject", "putRecord", "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "get", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt32", "jsNumberFromInt64", "jsNumberFromUint64", "isUndefined", "isNull", "isUndefinedOrNull", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKey", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable" };
pub const Extern = [_][]const u8{ "toZigString", "createStringArray", "createEmptyObject", "putRecord", "asPromise", "isClass", "getNameProperty", "getClassName", "getErrorsProperty", "toInt32", "toBoolean", "isInt32", "isIterable", "forEach", "isAggregateError", "toZigException", "isException", "toWTFString", "hasProperty", "getPropertyNames", "getDirect", "putDirect", "get", "getIfExists", "asString", "asObject", "asNumber", "isError", "jsNull", "jsUndefined", "jsTDZValue", "jsBoolean", "jsDoubleNumber", "jsNumberFromDouble", "jsNumberFromChar", "jsNumberFromU16", "jsNumberFromInt32", "jsNumberFromInt64", "jsNumberFromUint64", "isUndefined", "isNull", "isUndefinedOrNull", "isBoolean", "isAnyInt", "isUInt32AsAnyInt", "isInt32AsAnyInt", "isNumber", "isString", "isBigInt", "isHeapBigInt", "isBigInt32", "isSymbol", "isPrimitive", "isGetterSetter", "isCustomGetterSetter", "isObject", "isCell", "asCell", "toString", "toStringOrNull", "toPropertyKey", "toPropertyKeyValue", "toObject", "toString", "getPrototype", "getPropertyByPropertyName", "eqlValue", "eqlCell", "isCallable" };
};
pub const PropertyName = extern struct {

View File

@@ -1,4 +1,4 @@
//-- AUTOGENERATED FILE -- 1628394202
//-- AUTOGENERATED FILE -- 1628467440
// clang-format off
#pragma once

View File

@@ -1,4 +1,4 @@
//-- AUTOGENERATED FILE -- 1628394202
//-- AUTOGENERATED FILE -- 1628467440
// clang-format: off
#pragma once
@@ -239,6 +239,7 @@ CPP_DECL void JSC__JSObject__putDirect(JSC__JSObject* arg0, JSC__JSGlobalObject*
CPP_DECL void JSC__JSObject__putRecord(JSC__JSObject* arg0, JSC__JSGlobalObject* arg1, ZigString* arg2, ZigString* arg3, size_t arg4);
CPP_DECL JSC__JSValue ZigString__toErrorInstance(const ZigString* arg0, JSC__JSGlobalObject* arg1);
CPP_DECL JSC__JSValue ZigString__toValue(ZigString arg0, JSC__JSGlobalObject* arg1);
CPP_DECL JSC__JSValue ZigString__toValueGC(ZigString arg0, JSC__JSGlobalObject* arg1);
#pragma mark - JSC::JSCell
@@ -467,6 +468,7 @@ CPP_DECL JSC__JSString* JSC__JSValue__toString(JSC__JSValue JSValue0, JSC__JSGlo
CPP_DECL JSC__JSString* JSC__JSValue__toStringOrNull(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1);
CPP_DECL bWTF__String JSC__JSValue__toWTFString(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1);
CPP_DECL void JSC__JSValue__toZigException(JSC__JSValue JSValue0, JSC__JSGlobalObject* arg1, ZigException* arg2);
CPP_DECL void JSC__JSValue__toZigString(JSC__JSValue JSValue0, ZigString* arg1, JSC__JSGlobalObject* arg2);
#pragma mark - JSC::PropertyName

View File

@@ -105,6 +105,7 @@ pub extern fn JSC__JSObject__putDirect(arg0: [*c]JSC__JSObject, arg1: [*c]JSC__J
pub extern fn JSC__JSObject__putRecord(arg0: [*c]JSC__JSObject, arg1: [*c]JSC__JSGlobalObject, arg2: [*c]ZigString, arg3: [*c]ZigString, arg4: usize) void;
pub extern fn ZigString__toErrorInstance(arg0: [*c]const ZigString, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue;
pub extern fn ZigString__toValue(arg0: ZigString, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue;
pub extern fn ZigString__toValueGC(arg0: ZigString, arg1: [*c]JSC__JSGlobalObject) JSC__JSValue;
pub extern fn JSC__JSCell__getObject(arg0: [*c]JSC__JSCell) [*c]JSC__JSObject;
pub extern fn JSC__JSCell__getString(arg0: [*c]JSC__JSCell, arg1: [*c]JSC__JSGlobalObject) bWTF__String;
pub extern fn JSC__JSCell__getType(arg0: [*c]JSC__JSCell) u8;
@@ -290,6 +291,7 @@ pub extern fn JSC__JSValue__toString(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGl
pub extern fn JSC__JSValue__toStringOrNull(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) [*c]JSC__JSString;
pub extern fn JSC__JSValue__toWTFString(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject) bWTF__String;
pub extern fn JSC__JSValue__toZigException(JSValue0: JSC__JSValue, arg1: [*c]JSC__JSGlobalObject, arg2: [*c]ZigException) void;
pub extern fn JSC__JSValue__toZigString(JSValue0: JSC__JSValue, arg1: [*c]ZigString, arg2: [*c]JSC__JSGlobalObject) void;
pub extern fn JSC__PropertyName__eqlToIdentifier(arg0: [*c]JSC__PropertyName, arg1: [*c]const JSC__Identifier) bool;
pub extern fn JSC__PropertyName__eqlToPropertyName(arg0: [*c]JSC__PropertyName, arg1: [*c]const JSC__PropertyName) bool;
pub extern fn JSC__PropertyName__publicName(arg0: [*c]JSC__PropertyName) [*c]const WTF__StringImpl;

View File

@@ -24,15 +24,19 @@ pub const DefaultSpeedyDefines = struct {
};
};
pub fn configureTransformOptionsForSpeedy(allocator: *std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions {
pub fn configureTransformOptionsForSpeedyVM(allocator: *std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions {
var args = _args;
args.platform = Api.Platform.speedy;
args.serve = false;
args.write = false;
args.resolve = Api.ResolveMode.lazy;
args.generate_node_module_bundle = false;
return try configureTransformOptionsForSpeedy(allocator, args);
}
pub fn configureTransformOptionsForSpeedy(allocator: *std.mem.Allocator, _args: Api.TransformOptions) !Api.TransformOptions {
var args = _args;
args.platform = Api.Platform.speedy;
// We inline process.env.* at bundle time but process.env is a proxy object which will otherwise return undefined.
var env_map = try getNodeEnvMap(allocator);

View File

@@ -33,47 +33,12 @@ pub const GlobalClasses = [_]type{
};
pub const Wundle = struct {
top_level_dir: string,
threadlocal var css_imports_list_strings: [512]ZigString = undefined;
threadlocal var css_imports_list: [512]Api.StringPointer = undefined;
threadlocal var css_imports_list_tail: u16 = 0;
threadlocal var css_imports_buf: std.ArrayList(u8) = undefined;
threadlocal var css_imports_buf_loaded: bool = false;
pub fn flushCSSImports() void {
if (css_imports_buf_loaded) {
css_imports_buf.clearRetainingCapacity();
css_imports_list_tail = 0;
}
}
pub fn getCSSImports() []ZigString {
var i: u16 = 0;
const tail = css_imports_list_tail;
while (i < tail) : (i += 1) {
ZigString.fromStringPointer(css_imports_list[i], css_imports_buf.items, &css_imports_list_strings[i]);
}
return css_imports_list_strings[0..tail];
}
pub fn getImportedStyles(
this: void,
ctx: js.JSContextRef,
function: js.JSObjectRef,
thisObject: js.JSObjectRef,
arguments: []const js.JSValueRef,
exception: js.ExceptionRef,
) js.JSValueRef {
defer flushCSSImports();
const styles = getCSSImports();
if (styles.len == 0) {
return js.JSObjectMakeArray(ctx, 0, null, null);
}
return JSValue.createStringArray(VirtualMachine.vm.global, styles.ptr, styles.len).asRef();
}
pub fn onImportCSS(
resolve_result: *const Resolver.Result,
import_record: *ImportRecord,
@@ -99,13 +64,76 @@ pub const Wundle = struct {
css_imports_list_tail += 1;
}
pub fn flushCSSImports() void {
if (css_imports_buf_loaded) {
css_imports_buf.clearRetainingCapacity();
css_imports_list_tail = 0;
}
}
pub fn getCSSImports() []ZigString {
var i: u16 = 0;
const tail = css_imports_list_tail;
while (i < tail) : (i += 1) {
ZigString.fromStringPointer(css_imports_list[i], css_imports_buf.items, &css_imports_list_strings[i]);
}
return css_imports_list_strings[0..tail];
}
pub fn getCWD(
this: void,
ctx: js.JSContextRef,
thisObject: js.JSValueRef,
prop: js.JSStringRef,
exception: js.ExceptionRef,
) js.JSValueRef {
return ZigString.init(VirtualMachine.vm.bundler.fs.top_level_dir).toValue(VirtualMachine.vm.global).asRef();
}
pub fn getOrigin(
this: void,
ctx: js.JSContextRef,
thisObject: js.JSValueRef,
prop: js.JSStringRef,
exception: js.ExceptionRef,
) js.JSValueRef {
return ZigString.init(VirtualMachine.vm.bundler.options.origin).toValue(VirtualMachine.vm.global).asRef();
}
pub fn getMain(
this: void,
ctx: js.JSContextRef,
thisObject: js.JSValueRef,
prop: js.JSStringRef,
exception: js.ExceptionRef,
) js.JSValueRef {
return ZigString.init(VirtualMachine.vm.main).toValue(VirtualMachine.vm.global).asRef();
}
pub fn getImportedStyles(
this: void,
ctx: js.JSContextRef,
function: js.JSObjectRef,
thisObject: js.JSObjectRef,
arguments: []const js.JSValueRef,
exception: js.ExceptionRef,
) js.JSValueRef {
defer flushCSSImports();
const styles = getCSSImports();
if (styles.len == 0) {
return js.JSObjectMakeArray(ctx, 0, null, null);
}
return JSValue.createStringArray(VirtualMachine.vm.global, styles.ptr, styles.len).asRef();
}
pub fn getPublicPath(to: string, comptime Writer: type, writer: Writer) void {
const relative_path = VirtualMachine.vm.bundler.fs.relativeTo(to);
if (VirtualMachine.vm.bundler.options.public_url.len > 0) {
if (VirtualMachine.vm.bundler.options.origin.len > 0) {
writer.print(
"{s}/{s}",
.{
std.mem.trimRight(u8, VirtualMachine.vm.bundler.options.public_url, "/"),
std.mem.trimRight(u8, VirtualMachine.vm.bundler.options.origin, "/"),
std.mem.trimLeft(u8, relative_path, "/"),
},
) catch unreachable;
@@ -141,6 +169,18 @@ pub const Wundle = struct {
},
.{
.Route = Router.Instance.GetClass(void){},
.main = .{
.get = getMain,
.ts = d.ts{ .name = "main", .@"return" = "string" },
},
.cwd = .{
.get = getCWD,
.ts = d.ts{ .name = "cwd", .@"return" = "string" },
},
.origin = .{
.get = getOrigin,
.ts = d.ts{ .name = "origin", .@"return" = "string" },
},
},
);
};
@@ -165,6 +205,8 @@ pub const VirtualMachine = struct {
require_cache: RequireCacheType,
log: *logger.Log,
event_listeners: EventListenerMixin.Map,
main: string = "",
pub var vm_loaded = false;
pub var vm: *VirtualMachine = undefined;
@@ -187,7 +229,7 @@ pub const VirtualMachine = struct {
const bundler = try Bundler.init(
allocator,
log,
try configureTransformOptionsForSpeedy(allocator, _args),
try configureTransformOptionsForSpeedyVM(allocator, _args),
existing_bundle,
);
VirtualMachine.vm.* = VirtualMachine{
@@ -202,6 +244,7 @@ pub const VirtualMachine = struct {
};
VirtualMachine.vm.bundler.configureLinker();
try VirtualMachine.vm.bundler.configureFramework();
if (_args.serve orelse false) {
VirtualMachine.vm.bundler.linker.onImportCSS = Wundle.onImportCSS;
@@ -559,6 +602,7 @@ pub const VirtualMachine = struct {
pub fn loadEntryPoint(this: *VirtualMachine, entry_point: string) !*JSInternalPromise {
var path = this.bundler.normalizeEntryPointPath(entry_point);
this.main = entry_point;
var promise = JSModuleLoader.loadAndEvaluateModule(this.global, ZigString.init(path));
this.global.vm().drainMicrotasks();