From 62fb5ea9e396c5296f845d32f1aae5629fbf5836 Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Wed, 15 Dec 2021 15:16:53 -0800 Subject: wip fix live bindings --- Makefile | 2 + src/fallback.version | 2 +- src/import_record.zig | 57 ++++--- src/js_ast.zig | 3 + src/js_parser/js_parser.zig | 309 +++++++++++++++------------------ src/js_printer.zig | 404 ++++++++++++++++++++------------------------ 6 files changed, 359 insertions(+), 418 deletions(-) diff --git a/Makefile b/Makefile index 0b88a673e..d7cdda4a6 100644 --- a/Makefile +++ b/Makefile @@ -438,6 +438,8 @@ s2n-mac: CC=$(CC) CXX=$(CXX) cmake --build ./build -j$(CPUS); \ CC=$(CC) CXX=$(CXX) CTEST_PARALLEL_LEVEL=$(CPUS) ninja -C build cp $(DEPS_DIR)/s2n-tls/build/lib/libs2n.a $(DEPS_DIR)/libs2n.a + +libcrypto-old: unlink $(DEPS_DIR)/libcrypto.a || echo ""; ln $(LIBCRYPTO_STATIC_LIB) $(DEPS_DIR)/libcrypto.a || echo ""; diff --git a/src/fallback.version b/src/fallback.version index 74aeaab27..23bd52db7 100644 --- a/src/fallback.version +++ b/src/fallback.version @@ -1 +1 @@ -2bbe5942da63d2ba \ No newline at end of file +796022f759787f0a \ No newline at end of file diff --git a/src/import_record.zig b/src/import_record.zig index aa82e4731..4d4199004 100644 --- a/src/import_record.zig +++ b/src/import_record.zig @@ -44,61 +44,64 @@ pub const ImportRecord = struct { range: logger.Range, path: fs.Path, - // 0 is invalid + /// 0 is invalid module_id: u32 = 0, source_index: Ref.Int = std.math.maxInt(Ref.Int), print_mode: PrintMode = .normal, - // True for the following cases: - // - // try { require('x') } catch { handle } - // try { await import('x') } catch { handle } - // try { require.resolve('x') } catch { handle } - // import('x').catch(handle) - // import('x').then(_, handle) - // - // In these cases we shouldn't generate an error if the path could not be - // resolved. + /// True for the following cases: + /// + /// try { require('x') } catch { handle } + /// try { await import('x') } catch { handle } + /// try { require.resolve('x') } catch { handle } + /// import('x').catch(handle) + /// import('x').then(_, handle) + /// + /// In these cases we shouldn't generate an error if the path could not be + /// resolved. handles_import_errors: bool = false, is_internal: bool = false, - // This tells the printer that we should print as export var $moduleID = ... - // Instead of using the path. + /// This tells the printer that we should print as export var $moduleID = ... + /// Instead of using the path. is_bundled: bool = false, - // Sometimes the parser creates an import record and decides it isn't needed. - // For example, TypeScript code may have import statements that later turn - // out to be type-only imports after analyzing the whole file. + /// Sometimes the parser creates an import record and decides it isn't needed. + /// For example, TypeScript code may have import statements that later turn + /// out to be type-only imports after analyzing the whole file. is_unused: bool = false, - // If this is true, the import contains syntax like "* as ns". This is used - // to determine whether modules that have no exports need to be wrapped in a - // CommonJS wrapper or not. + /// If this is true, the import contains syntax like "* as ns". This is used + /// to determine whether modules that have no exports need to be wrapped in a + /// CommonJS wrapper or not. contains_import_star: bool = false, - // If this is true, the import contains an import for the alias "default", - // either via the "import x from" or "import {default as x} from" syntax. + /// If this is true, the import contains an import for the alias "default", + /// either via the "import x from" or "import {default as x} from" syntax. contains_default_alias: bool = false, - // If true, this "export * from 'path'" statement is evaluated at run-time by - // calling the "__reExport()" helper function + /// If true, this "export * from 'path'" statement is evaluated at run-time by + /// calling the "__reExport()" helper function calls_run_time_re_export_fn: bool = false, - // Tell the printer to wrap this call to "require()" in "__toModule(...)" + /// Tell the printer to wrap this call to "require()" in "__toModule(...)" wrap_with_to_module: bool = false, - // True for require calls like this: "try { require() } catch {}". In this - // case we shouldn't generate an error if the path could not be resolved. + /// True for require calls like this: "try { require() } catch {}". In this + /// case we shouldn't generate an error if the path could not be resolved. is_inside_try_body: bool = false, - // If true, this was originally written as a bare "import 'file'" statement + /// If true, this was originally written as a bare "import 'file'" statement was_originally_bare_import: bool = false, was_originally_require: bool = false, + /// If a macro used , it will be tracked here. + was_injected_by_macro: bool = false, + kind: ImportKind, pub const PrintMode = enum { diff --git a/src/js_ast.zig b/src/js_ast.zig index e926ae3b7..73ddac0f4 100644 --- a/src/js_ast.zig +++ b/src/js_ast.zig @@ -417,6 +417,7 @@ pub const G = struct { pub const NamespaceAlias = struct { namespace_ref: Ref, alias: string, + import_record_index: u32 = std.math.maxInt(u32), }; pub const ExportStarAlias = struct { @@ -974,6 +975,8 @@ pub const E = struct { // false, this could potentially have been a member access expression such // as "ns.foo" off of an imported namespace object. was_originally_identifier: bool = false, + + was_from_macro: bool = false, }; // This is similar to EIdentifier but it represents class-private fields and diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig index 38ce0f270..4d10c0b5e 100644 --- a/src/js_parser/js_parser.zig +++ b/src/js_parser/js_parser.zig @@ -20,6 +20,8 @@ const ScopeOrderList = std.ArrayListUnmanaged(?ScopeOrder); const JSXFactoryName = "JSX"; const JSXAutomaticName = "jsx_module"; +// kept as a static reference +const exports_string_name: string = "exports"; const MacroRefs = std.AutoArrayHashMap(Ref, u32); // If we are currently in a hoisted child of the module scope, relocate these @@ -437,124 +439,77 @@ pub const ImportScanner = struct { } } - if (p.options.trim_unused_imports) { - if (st.star_name_loc != null or did_remove_star_loc) { - // -- Original Comment -- - // If we're bundling a star import and the namespace is only ever - // used for property accesses, then convert each unique property to - // a clause item in the import statement and remove the star import. - // That will cause the bundler to bundle them more efficiently when - // both this module and the imported module are in the same group. - // - // Before: - // - // import * as ns from 'foo' - // console.log(ns.a, ns.b) - // - // After: - // - // import {a, b} from 'foo' - // console.log(a, b) - // - // This is not done if the namespace itself is used, because in that - // case the code for the namespace will have to be generated. This is - // determined by the symbol count because the parser only counts the - // star import as used if it was used for something other than a - // property access: - // - // import * as ns from 'foo' - // console.log(ns, ns.a, ns.b) - // - // -- Original Comment -- - - // jarred: we don't use the same grouping mechanism as esbuild - // but, we do this anyway. - // The reasons why are: - // * It makes static analysis for other tools simpler. - // * I imagine browsers may someday do some optimizations - // when it's "easier" to know only certain modules are used - // For example, if you're importing a component from a design system - // it's really stupid to import all 1,000 components from that design system - // when you just want