Skip to content

Commit

Permalink
start work on code generation
Browse files Browse the repository at this point in the history
  • Loading branch information
evanw committed May 23, 2020
1 parent cb0d60f commit f36aca1
Show file tree
Hide file tree
Showing 12 changed files with 682 additions and 438 deletions.
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,14 @@ Options:
--target=... Language target (default esnext)
--platform=... Platform target (browser or node, default browser)
--external:M Exclude module M from the bundle
--format=... Output format (iife, cjs, es)
--format=... Output format (iife, cjs, esm)
--color=... Force use of color terminal escapes (true or false)
--minify Sets all --minify-* flags
--minify-whitespace Remove whitespace
--minify-identifiers Shorten identifiers
--minify-syntax Use equivalent but shorter syntax
--tree-shaking Remove unused code from the bundle
--define:K=V Substitute K with V while parsing
--jsx-factory=... What to use instead of React.createElement
Expand Down
10 changes: 7 additions & 3 deletions cmd/esbuild/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,14 @@ Options:
--target=... Language target (default esnext)
--platform=... Platform target (browser or node, default browser)
--external:M Exclude module M from the bundle
--format=... Output format (iife, cjs, es)
--format=... Output format (iife, cjs, esm)
--color=... Force use of color terminal escapes (true or false)
--minify Sets all --minify-* flags
--minify-whitespace Remove whitespace
--minify-identifiers Shorten identifiers
--minify-syntax Use equivalent but shorter syntax
--tree-shaking Remove unused code from the bundle
--define:K=V Substitute K with V while parsing
--jsx-factory=... What to use instead of React.createElement
Expand Down Expand Up @@ -219,6 +220,9 @@ func parseArgs(fs fs.FS, rawArgs []string) (argsObject, error) {
case arg == "--minify-identifiers":
args.bundleOptions.MinifyIdentifiers = true

case arg == "--tree-shaking":
args.bundleOptions.TreeShaking = true

case arg == "--sourcemap":
args.bundleOptions.SourceMap = bundler.SourceMapLinkedWithComment

Expand Down Expand Up @@ -336,10 +340,10 @@ func parseArgs(fs fs.FS, rawArgs []string) (argsObject, error) {
args.bundleOptions.OutputFormat = bundler.FormatIIFE
case "cjs":
args.bundleOptions.OutputFormat = bundler.FormatCommonJS
case "es":
case "esm":
args.bundleOptions.OutputFormat = bundler.FormatESModule
default:
return argsObject{}, fmt.Errorf("Valid formats: iife, cjs, es")
return argsObject{}, fmt.Errorf("Valid formats: iife, cjs, esm")
}

case strings.HasPrefix(arg, "--color="):
Expand Down
5 changes: 5 additions & 0 deletions internal/ast/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -995,6 +995,10 @@ func (sm SymbolMap) IncrementUseCountEstimate(ref Ref) {
sm.Outer[ref.OuterIndex][ref.InnerIndex].UseCountEstimate++
}

func (sm SymbolMap) SetName(ref Ref, name string) {
sm.Outer[ref.OuterIndex][ref.InnerIndex].Name = name
}

func (sm SymbolMap) SetNamespaceAlias(ref Ref, alias NamespaceAlias) {
sm.Outer[ref.OuterIndex][ref.InnerIndex].NamespaceAlias = &alias
}
Expand Down Expand Up @@ -1037,6 +1041,7 @@ type AST struct {
ModuleScope *Scope
ExportsRef Ref
ModuleRef Ref
WrapperRef Ref

// These are used when bundling.
NamedImports map[Ref]NamedImport
Expand Down
24 changes: 15 additions & 9 deletions internal/bundler/bundler.go
Original file line number Diff line number Diff line change
Expand Up @@ -364,6 +364,9 @@ type BundleOptions struct {
// false: imports are left alone and the file is passed through as-is
IsBundling bool

// If true, unused code is removed. If false, all code is kept.
TreeShaking bool

AbsOutputFile string
AbsOutputDir string
RemoveWhitespace bool
Expand Down Expand Up @@ -1304,13 +1307,12 @@ func (b *Bundle) renameOrMinifyAllSymbolsInRuntime(files []file, symbols ast.Sym
reservedNames := computeReservedNames(moduleScopes, symbols)

// We're only renaming symbols in the runtime
runtimeModuleScope := []*ast.Scope{files[runtimeSourceIndex].ast.ModuleScope}
scope := files[runtimeSourceIndex].ast.ModuleScope

if options.MinifyIdentifiers {
nextName := 54 * 54 // Use names ending with '$' to avoid taking good short names
minifyAllSymbols(reservedNames, runtimeModuleScope, symbols, nextName)
minifyAllSymbols(reservedNames, []*ast.Scope{scope}, scope.Children, symbols)
} else {
renameAllSymbols(reservedNames, runtimeModuleScope, symbols)
renameAllSymbols(reservedNames, []*ast.Scope{scope}, scope.Children, symbols)
}
}

Expand Down Expand Up @@ -1345,10 +1347,15 @@ func (b *Bundle) renameOrMinifyAllSymbols(files []file, symbols ast.SymbolMap, g
}
}

nestedScopes := []*ast.Scope{}
for _, scope := range moduleScopes {
nestedScopes = append(nestedScopes, scope.Children...)
}

if options.MinifyIdentifiers {
minifyAllSymbols(reservedNames, moduleScopes, symbols, 0 /* nextName */)
minifyAllSymbols(reservedNames, moduleScopes, nestedScopes, symbols)
} else {
renameAllSymbols(reservedNames, moduleScopes, symbols)
renameAllSymbols(reservedNames, moduleScopes, nestedScopes, symbols)
}
}

Expand Down Expand Up @@ -1565,9 +1572,8 @@ func (b *Bundle) compileIndependent(log logging.Log, options *BundleOptions) []B
}

func (b *Bundle) compileBundle(log logging.Log, options *BundleOptions) []BundleResult {
c := newLinkerContext(log, b.sources, b.files, b.entryPoints)
c.link()
return b.oldCompileBundle(log, options)
c := newLinkerContext(options, log, b.fs, b.sources, b.files, b.entryPoints)
return c.link()
}

func (b *Bundle) oldCompileBundle(log logging.Log, options *BundleOptions) []BundleResult {
Expand Down
Loading

0 comments on commit f36aca1

Please sign in to comment.