From 4e3dd3aadc36f3187d08764f122ee093acf1983b Mon Sep 17 00:00:00 2001 From: Jarred Sumner Date: Fri, 27 Aug 2021 18:16:57 -0700 Subject: Skip unused Former-commit-id: 82de54e123f629bf3ca17df6d10151b32efb2409 --- src/bundler.zig | 4 ++-- src/js_parser/js_parser.zig | 16 ++++++++-------- 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