Skip to content

Commit

Permalink
enable code split for cjs target
Browse files Browse the repository at this point in the history
cherry pick from evanw#2211
  • Loading branch information
ArrayZoneYour committed Jul 31, 2023
1 parent b92d937 commit cbca82c
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 2 deletions.
55 changes: 55 additions & 0 deletions internal/linker/linker.go
Expand Up @@ -1035,6 +1035,33 @@ func (c *linkerContext) computeCrossChunkDependencies() {
}}}
}

case config.FormatCommonJS:
r := renamer.ExportRenamer{}
var items []js_ast.Property
for _, export := range c.sortedCrossChunkExportItems(chunkMetas[chunkIndex].exports) {
var alias string
if c.options.MinifyIdentifiers {
alias = r.NextMinifiedName()
} else {
alias = r.NextRenamedName(c.graph.Symbols.Get(export.Ref).OriginalName)
}
items = append(items, js_ast.Property{Key: js_ast.Expr{Data: &js_ast.EString{Value: helpers.StringToUTF16(alias)}}, ValueOrNil: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: export.Ref}}})
chunkRepr.exportsToOtherChunks[export.Ref] = alias
}
if len(items) > 0 {
st := js_ast.AssignStmt(
js_ast.Expr{Data: &js_ast.EDot{
Target: js_ast.Expr{Data: &js_ast.EIdentifier{Ref: c.unboundModuleRef}},
Name: "exports",
}},
js_ast.Expr{Data: &js_ast.EObject{
Properties: items,
}},
)

chunkRepr.crossChunkSuffixStmts = []js_ast.Stmt{st}
}

default:
panic("Internal error")
}
Expand Down Expand Up @@ -1077,6 +1104,34 @@ func (c *linkerContext) computeCrossChunkDependencies() {
}})
}

case config.FormatCommonJS:
var items []js_ast.PropertyBinding
for _, item := range crossChunkImport.sortedImportItems {
items = append(items, js_ast.PropertyBinding{Key: js_ast.Expr{Data: &js_ast.EString{Value: helpers.StringToUTF16(item.exportAlias)}},
Value: js_ast.Binding{Data: &js_ast.BIdentifier{Ref: item.ref}}})
}
importRecordIndex := uint32(len(chunk.crossChunkImports))
chunk.crossChunkImports = append(chunk.crossChunkImports, chunkImport{
importKind: ast.ImportStmt,
chunkIndex: crossChunkImport.chunkIndex,
})
if len(items) > 0 {
crossChunkPrefixStmts = append(crossChunkPrefixStmts, js_ast.Stmt{
Data: &js_ast.SLocal{Decls: []js_ast.Decl{{
Binding: js_ast.Binding{Data: &js_ast.BObject{
Properties: items,
}},
ValueOrNil: js_ast.Expr{Data: &js_ast.ERequireString{
ImportRecordIndex: importRecordIndex,
}},
}}},
})
} else {
crossChunkPrefixStmts = append(crossChunkPrefixStmts, js_ast.Stmt{Data: &js_ast.SExpr{Value: js_ast.Expr{Data: &js_ast.ERequireString{
ImportRecordIndex: importRecordIndex,
}}}})
}

default:
panic("Internal error")
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/api/api_impl.go
Expand Up @@ -1401,8 +1401,8 @@ func validateBuildOptions(
}

// Code splitting is experimental and currently only enabled for ES6 modules
if options.CodeSplitting && options.OutputFormat != config.FormatESModule {
log.AddError(nil, logger.Range{}, "Splitting currently only works with the \"esm\" format")
if options.CodeSplitting && options.OutputFormat != config.FormatESModule && options.OutputFormat != config.FormatCommonJS {
log.AddError(nil, logger.Range{}, "Splitting currently only works with the \"esm\" and \"cjs\" format")
}

// If we aren't writing the output to the file system, then we can allow the
Expand Down

0 comments on commit cbca82c

Please sign in to comment.