Skip to content

Commit 1f7e2d3

Browse files
committed
fix: shadow global define in __commonJS
1 parent 3327274 commit 1f7e2d3

File tree

6 files changed

+63
-1
lines changed

6 files changed

+63
-1
lines changed

internal/bundler_tests/bundler_loader_test.go

+26
Original file line numberDiff line numberDiff line change
@@ -1697,6 +1697,32 @@ func TestLoaderBundleWithTypeJSONOnlyDefaultExport(t *testing.T) {
16971697
})
16981698
}
16991699

1700+
func TestLoaderUmdDefineShouldBeShadowed(t *testing.T) {
1701+
loader_suite.expectBundled(t, bundled{
1702+
files: map[string]string{
1703+
"/entry.js": `
1704+
const LZString = "test";
1705+
if (typeof define === 'function' && define.amd) {
1706+
define(function () { return LZString; });
1707+
} else if( typeof module !== 'undefined' && module != null ) {
1708+
module.exports = LZString
1709+
} else if( typeof angular !== 'undefined' && angular != null ) {
1710+
angular.module('LZString', [])
1711+
.factory('LZString', function () {
1712+
return LZString;
1713+
});
1714+
}
1715+
`,
1716+
},
1717+
entryPaths: []string{"/entry.js"},
1718+
options: config.Options{
1719+
Mode: config.ModeConvertFormat,
1720+
OutputFormat: config.FormatESModule,
1721+
AbsOutputFile: "/out.js",
1722+
},
1723+
})
1724+
}
1725+
17001726
func TestLoaderJSONPrototype(t *testing.T) {
17011727
loader_suite.expectBundled(t, bundled{
17021728
files: map[string]string{

internal/bundler_tests/snapshots/snapshots_loader.txt

+21
Original file line numberDiff line numberDiff line change
@@ -968,6 +968,27 @@ var y_default = "y";
968968
var x_txt = require_x();
969969
console.log(x_txt, y_default);
970970

971+
================================================================================
972+
TestLoaderUmdDefineShouldBeShadowed
973+
---------- /out.js ----------
974+
var require_entry = __commonJS({
975+
"entry.js"(exports, module, define) {
976+
const LZString = "test";
977+
if (typeof define === "function" && define.amd) {
978+
define(function() {
979+
return LZString;
980+
});
981+
} else if (typeof module !== "undefined" && module != null) {
982+
module.exports = LZString;
983+
} else if (typeof angular !== "undefined" && angular != null) {
984+
angular.module("LZString", []).factory("LZString", function() {
985+
return LZString;
986+
});
987+
}
988+
}
989+
});
990+
export default require_entry();
991+
971992
================================================================================
972993
TestRequireCustomExtensionBase64
973994
---------- /out.js ----------

internal/graph/graph.go

+3
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,9 @@ func (g *LinkerGraph) GenerateSymbolImportAndUse(
396396
if ref == repr.AST.ModuleRef {
397397
repr.AST.UsesModuleRef = true
398398
}
399+
if ref == repr.AST.DefineRef {
400+
repr.AST.UsesDefineRef = true
401+
}
399402

400403
// Track that this specific symbol was imported
401404
if sourceIndexToImportFrom != sourceIndex {

internal/js_ast/js_ast.go

+2
Original file line numberDiff line numberDiff line change
@@ -1589,6 +1589,7 @@ type AST struct {
15891589

15901590
ExportsRef ast.Ref
15911591
ModuleRef ast.Ref
1592+
DefineRef ast.Ref
15921593
WrapperRef ast.Ref
15931594

15941595
ApproximateLineCount int32
@@ -1601,6 +1602,7 @@ type AST struct {
16011602
// here because only the parser checks those.
16021603
UsesExportsRef bool
16031604
UsesModuleRef bool
1605+
UsesDefineRef bool
16041606
ExportsKind ExportsKind
16051607
}
16061608

internal/js_parser/js_parser.go

+7
Original file line numberDiff line numberDiff line change
@@ -224,6 +224,7 @@ type parser struct {
224224
exportsRef ast.Ref
225225
requireRef ast.Ref
226226
moduleRef ast.Ref
227+
defineRef ast.Ref
227228
importMetaRef ast.Ref
228229
promiseRef ast.Ref
229230
regExpRef ast.Ref
@@ -14615,6 +14616,7 @@ func (p *parser) visitExprInOut(expr js_ast.Expr, in exprIn) (js_ast.Expr, exprO
1461514616
if p.options.mode == config.ModeBundle && !p.isFileConsideredToHaveESMExports {
1461614617
p.recordUsage(p.moduleRef)
1461714618
p.recordUsage(p.exportsRef)
14619+
p.recordUsage(p.defineRef)
1461814620
}
1461914621

1462014622
// Mark this scope and all parent scopes as containing a direct eval.
@@ -17520,10 +17522,12 @@ func (p *parser) prepareForVisitPass() {
1752017522
// CommonJS-style exports
1752117523
p.exportsRef = p.declareCommonJSSymbol(ast.SymbolHoisted, "exports")
1752217524
p.moduleRef = p.declareCommonJSSymbol(ast.SymbolHoisted, "module")
17525+
p.defineRef = p.declareCommonJSSymbol(ast.SymbolHoisted, "define")
1752317526
} else {
1752417527
// ESM-style exports
1752517528
p.exportsRef = p.newSymbol(ast.SymbolHoisted, "exports")
1752617529
p.moduleRef = p.newSymbol(ast.SymbolHoisted, "module")
17530+
p.defineRef = p.newSymbol(ast.SymbolHoisted, "define")
1752717531
}
1752817532

1752917533
// Handle "@jsx" and "@jsxFrag" pragmas now that lexing is done
@@ -17939,6 +17943,7 @@ func (p *parser) toAST(before, parts, after []js_ast.Part, hashbang string, dire
1793917943
exportsKind := js_ast.ExportsNone
1794017944
usesExportsRef := p.symbols[p.exportsRef.InnerIndex].UseCountEstimate > 0
1794117945
usesModuleRef := p.symbols[p.moduleRef.InnerIndex].UseCountEstimate > 0
17946+
usesDefineRef := p.symbols[p.defineRef.InnerIndex].UseCountEstimate > 0
1794217947

1794317948
if p.esmExportKeyword.Len > 0 || p.esmImportMeta.Len > 0 || p.topLevelAwaitKeyword.Len > 0 {
1794417949
exportsKind = js_ast.ExportsESM
@@ -17975,6 +17980,7 @@ func (p *parser) toAST(before, parts, after []js_ast.Part, hashbang string, dire
1797517980
Symbols: p.symbols,
1797617981
ExportsRef: p.exportsRef,
1797717982
ModuleRef: p.moduleRef,
17983+
DefineRef: p.defineRef,
1797817984
WrapperRef: wrapperRef,
1797917985
Hashbang: hashbang,
1798017986
Directives: directives,
@@ -17995,6 +18001,7 @@ func (p *parser) toAST(before, parts, after []js_ast.Part, hashbang string, dire
1799518001
// CommonJS features
1799618002
UsesExportsRef: usesExportsRef,
1799718003
UsesModuleRef: usesModuleRef,
18004+
UsesDefineRef: usesDefineRef,
1799818005
ExportsKind: exportsKind,
1799918006

1800018007
// ES6 features

internal/linker/linker.go

+4-1
Original file line numberDiff line numberDiff line change
@@ -4791,8 +4791,11 @@ func (c *linkerContext) generateCodeForFileInChunkJS(
47914791
args := []js_ast.Arg{}
47924792
if repr.AST.UsesExportsRef || repr.AST.UsesModuleRef {
47934793
args = append(args, js_ast.Arg{Binding: js_ast.Binding{Data: &js_ast.BIdentifier{Ref: repr.AST.ExportsRef}}})
4794-
if repr.AST.UsesModuleRef {
4794+
if repr.AST.UsesModuleRef || repr.AST.UsesDefineRef {
47954795
args = append(args, js_ast.Arg{Binding: js_ast.Binding{Data: &js_ast.BIdentifier{Ref: repr.AST.ModuleRef}}})
4796+
if repr.AST.UsesDefineRef {
4797+
args = append(args, js_ast.Arg{Binding: js_ast.Binding{Data: &js_ast.BIdentifier{Ref: repr.AST.DefineRef}}})
4798+
}
47964799
}
47974800
}
47984801

0 commit comments

Comments
 (0)