diff options
author | 2021-08-27 18:16:57 -0700 | |
---|---|---|
committer | 2021-08-27 18:16:57 -0700 | |
commit | 4e3dd3aadc36f3187d08764f122ee093acf1983b (patch) | |
tree | 10ac6e59baa2ea17c6a0bfbed34a5208e3004094 | |
parent | 476bcfdae32b237300faeea6f78e50cf245957e3 (diff) | |
download | bun-4e3dd3aadc36f3187d08764f122ee093acf1983b.tar.gz bun-4e3dd3aadc36f3187d08764f122ee093acf1983b.tar.zst bun-4e3dd3aadc36f3187d08764f122ee093acf1983b.zip |
Skip unused
Former-commit-id: 82de54e123f629bf3ca17df6d10151b32efb2409
Diffstat (limited to '')
-rw-r--r-- | src/bundler.zig | 4 | ||||
-rw-r--r-- | src/js_parser/js_parser.zig | 16 | ||||
-rw-r--r-- | src/linker.zig | 2 |
3 files changed, 12 insertions, 10 deletions
diff --git a/src/bundler.zig b/src/bundler.zig index 4beef36c7..7d64f16ea 100644 --- a/src/bundler.zig +++ b/src/bundler.zig @@ -978,7 +978,7 @@ pub fn NewBundler(cache_files: bool) type { for (ast.import_records) |*import_record, record_id| { // Don't resolve the runtime - if (import_record.is_internal) { + if (import_record.is_internal or import_record.is_unused) { continue; } @@ -1268,7 +1268,7 @@ pub fn NewBundler(cache_files: bool) type { { for (scan_pass_result.import_records.items) |*import_record, i| { - if (import_record.is_internal) { + if (import_record.is_internal or import_record.is_unused) { continue; } diff --git a/src/js_parser/js_parser.zig b/src/js_parser/js_parser.zig index e1fbd2ca4..48346fc0a 100644 --- a/src/js_parser/js_parser.zig +++ b/src/js_parser/js_parser.zig @@ -130,14 +130,14 @@ pub const ImportScanner = struct { // // const keep_unused_imports = !p.options.trim_unused_imports; var did_remove_star_loc = false; - const keep_unused_imports = true; + const keep_unused_imports = false; // TypeScript always trims unused imports. This is important for // correctness since some imports might be fake (only in the type // system and used for type-only imports). if (!keep_unused_imports) { var found_imports = false; - var is_unused_in_typescript = false; + var is_unused_in_typescript = true; if (st.default_name) |default_name| { found_imports = true; @@ -185,7 +185,7 @@ pub const ImportScanner = struct { // Remove items if they are unused if (st.items.len > 0) { - found_imports = false; + found_imports = true; var items_end: usize = 0; var i: usize = 0; while (i < st.items.len) : (i += 1) { @@ -235,10 +235,10 @@ pub const ImportScanner = struct { // e.g. `import 'fancy-stylesheet-thing/style.css';` // This is a breaking change though. We can make it an option with some guardrail // so maybe if it errors, it shows a suggestion "retry without trimming unused imports" - if (found_imports and !p.options.preserve_unused_imports_ts) { + if (p.options.ts and found_imports and is_unused_in_typescript and !p.options.preserve_unused_imports_ts) { // Ignore import records with a pre-filled source index. These are // for injected files and we definitely do not want to trim these. - if (!Ref.isSourceIndexNull(record.source_index)) { + if (!record.is_internal) { record.is_unused = true; continue; } @@ -285,13 +285,13 @@ pub const ImportScanner = struct { // it's really stupid to import all 1,000 components from that design system // when you just want <Button /> const namespace_ref = st.namespace_ref; - const convert_star_to_clause = p.symbols.items[namespace_ref.inner_index].use_count_estimate == 0 and st.default_name == null; + const convert_star_to_clause = !p.options.can_import_from_bundle and p.symbols.items[namespace_ref.inner_index].use_count_estimate == 0; if (convert_star_to_clause and !keep_unused_imports) { st.star_name_loc = null; } - // "importItemsForNamespace" has property accesses off the namespace + // "import_items_for_namespace" has property accesses off the namespace if (p.import_items_for_namespace.get(namespace_ref)) |import_items| { var count = import_items.count(); if (count > 0) { @@ -300,7 +300,7 @@ pub const ImportScanner = struct { var iter = import_items.iterator(); var i: usize = 0; while (iter.next()) |item| { - sorted[i] = item.key; + sorted[i] = item.key_ptr.*; i += 1; } strings.sortAsc(sorted); diff --git a/src/linker.zig b/src/linker.zig index bebbe6a99..9c0c1d6a1 100644 --- a/src/linker.zig +++ b/src/linker.zig @@ -195,6 +195,8 @@ pub fn NewLinker(comptime BundlerType: type) type { switch (result.loader) { .jsx, .js, .ts, .tsx => { for (result.ast.import_records) |*import_record, _record_index| { + if (import_record.is_unused) continue; + const record_index = @truncate(u32, _record_index); if (comptime !ignore_runtime) { if (strings.eqlComptime(import_record.path.text, Runtime.Imports.Name)) { |