-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: library target UMD supports names per target #4883
Changes from all commits
0ab7b1a
2ec4b5c
d87fb0e
369efb3
913b18b
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,17 @@ function accessorAccess(base, accessor) { | |
|
||
class UmdMainTemplatePlugin { | ||
constructor(name, options) { | ||
this.name = name; | ||
if(typeof name === "object" && !Array.isArray(name)) { | ||
this.name = name.root || name.amd || name.commonjs; | ||
this.names = name; | ||
} else { | ||
this.name = name; | ||
this.names = { | ||
commonjs: name, | ||
root: name, | ||
amd: name | ||
}; | ||
} | ||
this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal; | ||
this.namedDefine = options.namedDefine; | ||
this.auxiliaryComment = options.auxiliaryComment; | ||
|
@@ -124,16 +134,16 @@ class UmdMainTemplatePlugin { | |
) + | ||
" else if(typeof define === 'function' && define.amd)\n" + | ||
(requiredExternals.length > 0 ? | ||
(this.name && this.namedDefine === true ? | ||
" define(" + libraryName(this.name) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" : | ||
(this.names.amd && this.namedDefine === true ? | ||
" define(" + libraryName(this.names.amd) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" : | ||
" define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" | ||
) : | ||
(this.name && this.namedDefine === true ? | ||
" define(" + libraryName(this.name) + ", [], " + amdFactory + ");\n" : | ||
(this.names.amd && this.namedDefine === true ? | ||
" define(" + libraryName(this.names.amd) + ", [], " + amdFactory + ");\n" : | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this line is not tested, if you want to you can add a test, but it's not required. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sokra Thanks, I've added tests for There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sokra Not sure how to get that line covered. Looks like it wasn't before either, and this PR now improves coverage overall. https://codecov.io/gh/webpack/webpack/pull/4883/changes |
||
" define([], " + amdFactory + ");\n" | ||
) | ||
) + | ||
(this.name ? | ||
(this.names.root || this.names.commonjs ? | ||
(this.auxiliaryComment && | ||
typeof this.auxiliaryComment === "string" ? | ||
" //" + this.auxiliaryComment + "\n" : | ||
|
@@ -142,7 +152,7 @@ class UmdMainTemplatePlugin { | |
"" | ||
) + | ||
" else if(typeof exports === 'object')\n" + | ||
" exports[" + libraryName(this.name) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" + | ||
" exports[" + libraryName(this.names.commonjs || this.names.root) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" + | ||
(this.auxiliaryComment && | ||
typeof this.auxiliaryComment === "string" ? | ||
" //" + this.auxiliaryComment + "\n" : | ||
|
@@ -151,7 +161,7 @@ class UmdMainTemplatePlugin { | |
"" | ||
) + | ||
" else\n" + | ||
" " + replaceKeys(accessorAccess("root", this.name)) + " = factory(" + externalsRootArray(externals) + ");\n" : | ||
" " + replaceKeys(accessorAccess("root", this.names.root || this.names.commonjs)) + " = factory(" + externalsRootArray(externals) + ");\n" : | ||
" else {\n" + | ||
(externals.length > 0 ? | ||
" var a = typeof exports === 'object' ? factory(" + externalsRequireArray("commonjs") + ") : factory(" + externalsRootArray(externals) + ");\n" : | ||
|
@@ -163,12 +173,16 @@ class UmdMainTemplatePlugin { | |
"})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})"); | ||
}.bind(this)); | ||
mainTemplate.plugin("global-hash-paths", function(paths) { | ||
if(this.name) paths = paths.concat(this.name); | ||
if(this.names.root) paths = paths.concat(this.names.root); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add all names here? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sokra Is it okay if names are repeated in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. That's ok. They should just contribute all to the hash. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sokra Again here, the paths have been added for all names. |
||
if(this.names.amd) paths = paths.concat(this.names.amd); | ||
if(this.names.commonjs) paths = paths.concat(this.names.commonjs); | ||
return paths; | ||
}.bind(this)); | ||
mainTemplate.plugin("hash", function(hash) { | ||
hash.update("umd"); | ||
hash.update(`${this.name}`); | ||
hash.update(`${this.names.root}`); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. here also all names There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sokra Again, is it okay if names are repeated here, or do we need to dedupe first? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep, it's ok There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @sokra The hash has been updated with all names. |
||
hash.update(`${this.names.amd}`); | ||
hash.update(`${this.names.commonjs}`); | ||
}.bind(this)); | ||
} | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
it("should run", function() { | ||
|
||
}); |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
module.exports = { | ||
output: { | ||
libraryTarget: "umd", | ||
library: { | ||
root: "testLibrary", | ||
amd: "test-library", | ||
commonjs: "test-library" | ||
} | ||
} | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
does the schema allow an object as
output.libraryName
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sokra Good point. Updated schema and added test.