Skip to content

Commit

Permalink
JS: remove import statement with empty named imports
Browse files Browse the repository at this point in the history
  • Loading branch information
tdewolff committed Oct 26, 2023
1 parent 61a4bb8 commit 19c34c5
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 26 deletions.
54 changes: 28 additions & 26 deletions js/js.go
Original file line number Diff line number Diff line change
Expand Up @@ -322,37 +322,39 @@ func (m *jsMinifier) minifyStmt(i js.IStmt) {
m.requireSemicolon()
case *js.EmptyStmt:
case *js.ImportStmt:
m.write(importBytes)
if stmt.Default != nil {
m.write(spaceBytes)
m.write(stmt.Default)
if len(stmt.List) != 0 {
m.write(commaBytes)
} else if stmt.Default != nil || len(stmt.List) != 0 {
if stmt.Default != nil || stmt.List == nil || 0 < len(stmt.List) {
m.write(importBytes)
if stmt.Default != nil {
m.write(spaceBytes)
}
}
if len(stmt.List) == 1 && len(stmt.List[0].Name) == 1 && stmt.List[0].Name[0] == '*' {
m.writeSpaceBeforeIdent()
m.minifyAlias(stmt.List[0])
if stmt.Default != nil || len(stmt.List) != 0 {
m.write(spaceBytes)
}
} else if 0 < len(stmt.List) {
m.write(openBraceBytes)
for i, item := range stmt.List {
if i != 0 {
m.write(stmt.Default)
if len(stmt.List) != 0 {
m.write(commaBytes)
} else if stmt.Default != nil || len(stmt.List) != 0 {
m.write(spaceBytes)
}
m.minifyAlias(item)
}
m.write(closeBraceBytes)
}
if stmt.Default != nil || len(stmt.List) != 0 {
m.write(fromBytes)
if len(stmt.List) == 1 && len(stmt.List[0].Name) == 1 && stmt.List[0].Name[0] == '*' {
m.writeSpaceBeforeIdent()
m.minifyAlias(stmt.List[0])
if stmt.Default != nil || len(stmt.List) != 0 {
m.write(spaceBytes)
}
} else if stmt.List != nil {
m.write(openBraceBytes)
for i, item := range stmt.List {
if i != 0 {
m.write(commaBytes)
}
m.minifyAlias(item)
}
m.write(closeBraceBytes)
}
if stmt.Default != nil || stmt.List != nil {
m.write(fromBytes)
}
m.write(minifyString(stmt.Module, false))
m.requireSemicolon()
}
m.write(minifyString(stmt.Module, false))
m.requireSemicolon()
case *js.ExportStmt:
m.write(exportBytes)
if stmt.Decl != nil {
Expand Down
1 change: 1 addition & 0 deletions js/js_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ func TestJS(t *testing.T) {
{`import "path"`, `import"path"`},
{`import x from "path"`, `import x from"path"`},
{`import * as b from "path"`, `import*as b from"path"`},
{`import {} from "path"`, ``},
{`import {a as b} from "path"`, `import{a as b}from"path"`},
{`import {a as b, c} from "path"`, `import{a as b,c}from"path"`},
{`import x, * as b from "path"`, `import x,*as b from"path"`},
Expand Down

0 comments on commit 19c34c5

Please sign in to comment.