Skip to content

Commit 0ab7b1a

Browse files
feat: library target UMD supports names per target
1 parent 60824f1 commit 0ab7b1a

4 files changed

Lines changed: 74 additions & 10 deletions

File tree

‎lib/UmdMainTemplatePlugin.js‎

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,17 @@ function accessorAccess(base, accessor) {
2323

2424
class UmdMainTemplatePlugin {
2525
constructor(name, options) {
26-
this.name = name;
26+
if(typeof name === "object" && !Array.isArray(name)) {
27+
this.name = name.root || name.amd || name.commonjs;
28+
this.names = name;
29+
} else {
30+
this.name = name;
31+
this.names = {
32+
commonjs: name,
33+
root: name,
34+
amd: name
35+
};
36+
}
2737
this.optionalAmdExternalAsGlobal = options.optionalAmdExternalAsGlobal;
2838
this.namedDefine = options.namedDefine;
2939
this.auxiliaryComment = options.auxiliaryComment;
@@ -124,16 +134,16 @@ class UmdMainTemplatePlugin {
124134
) +
125135
" else if(typeof define === 'function' && define.amd)\n" +
126136
(requiredExternals.length > 0 ?
127-
(this.name && this.namedDefine === true ?
128-
" define(" + libraryName(this.name) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
137+
(this.names.amd && this.namedDefine === true ?
138+
" define(" + libraryName(this.names.amd) + ", " + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n" :
129139
" define(" + externalsDepsArray(requiredExternals) + ", " + amdFactory + ");\n"
130140
) :
131-
(this.name && this.namedDefine === true ?
132-
" define(" + libraryName(this.name) + ", [], " + amdFactory + ");\n" :
141+
(this.names.amd && this.namedDefine === true ?
142+
" define(" + libraryName(this.names.amd) + ", [], " + amdFactory + ");\n" :
133143
" define([], " + amdFactory + ");\n"
134144
)
135145
) +
136-
(this.name ?
146+
(this.names.root || this.names.commonjs ?
137147
(this.auxiliaryComment &&
138148
typeof this.auxiliaryComment === "string" ?
139149
" //" + this.auxiliaryComment + "\n" :
@@ -142,7 +152,7 @@ class UmdMainTemplatePlugin {
142152
""
143153
) +
144154
" else if(typeof exports === 'object')\n" +
145-
" exports[" + libraryName(this.name) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
155+
" exports[" + libraryName(this.names.commonjs || this.names.root) + "] = factory(" + externalsRequireArray("commonjs") + ");\n" +
146156
(this.auxiliaryComment &&
147157
typeof this.auxiliaryComment === "string" ?
148158
" //" + this.auxiliaryComment + "\n" :
@@ -151,7 +161,7 @@ class UmdMainTemplatePlugin {
151161
""
152162
) +
153163
" else\n" +
154-
" " + replaceKeys(accessorAccess("root", this.name)) + " = factory(" + externalsRootArray(externals) + ");\n" :
164+
" " + replaceKeys(accessorAccess("root", this.names.root || this.names.commonjs)) + " = factory(" + externalsRootArray(externals) + ");\n" :
155165
" else {\n" +
156166
(externals.length > 0 ?
157167
" var a = typeof exports === 'object' ? factory(" + externalsRequireArray("commonjs") + ") : factory(" + externalsRootArray(externals) + ");\n" :
@@ -163,12 +173,12 @@ class UmdMainTemplatePlugin {
163173
"})(this, function(" + externalsArguments(externals) + ") {\nreturn ", "webpack/universalModuleDefinition"), source, ";\n})");
164174
}.bind(this));
165175
mainTemplate.plugin("global-hash-paths", function(paths) {
166-
if(this.name) paths = paths.concat(this.name);
176+
if(this.names.root) paths = paths.concat(this.names.root);
167177
return paths;
168178
}.bind(this));
169179
mainTemplate.plugin("hash", function(hash) {
170180
hash.update("umd");
171-
hash.update(`${this.name}`);
181+
hash.update(`${this.names.root}`);
172182
}.bind(this));
173183
}
174184
}

‎test/LibraryTemplatePlugin.test.js‎

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -318,6 +318,58 @@ describe("LibraryTemplatePlugin", function() {
318318
});
319319
});
320320
});
321+
322+
describe("name is an object of names per target", function() {
323+
[{
324+
type: "umd",
325+
assertion: function(compilationContext) {
326+
compilationContext.name.should.be.exactly("barRoot");
327+
compilationContext.names.should.deepEqual({
328+
root: "barRoot",
329+
amd: "bar-amd"
330+
});
331+
compilationContext.optionalAmdExternalAsGlobal.should.be.false();
332+
compilationContext.namedDefine.should.be.exactly("bar");
333+
compilationContext.auxiliaryComment.should.be.exactly("baz");
334+
}
335+
},
336+
{
337+
type: "umd2",
338+
assertion: function(compilationContext) {
339+
compilationContext.name.should.be.exactly("barRoot");
340+
compilationContext.names.should.deepEqual({
341+
root: "barRoot",
342+
amd: "bar-amd"
343+
});
344+
compilationContext.optionalAmdExternalAsGlobal.should.be.true();
345+
compilationContext.namedDefine.should.be.exactly("bar");
346+
compilationContext.auxiliaryComment.should.be.exactly("baz");
347+
}
348+
}
349+
].forEach(function(targetTypeAndAssertion) {
350+
var type = targetTypeAndAssertion.type;
351+
352+
describe("when target is " + type, function() {
353+
beforeEach(function() {
354+
env.eventBindings = applyPluginWithOptions(LibraryTemplatePlugin, {
355+
root: "barRoot",
356+
amd: "bar-amd"
357+
}, type, "bar", "baz");
358+
env.eventBinding = env.eventBindings[0];
359+
env.eventBinding.handler(env.compilation);
360+
});
361+
362+
it("compilation callback is called", function() {
363+
env.compilation.callCount.should.be.exactly(1);
364+
});
365+
366+
it("compilation callback context is set up", function() {
367+
var compilationContext = env.compilation.firstCall.thisValue;
368+
targetTypeAndAssertion.assertion(compilationContext);
369+
});
370+
});
371+
});
372+
});
321373
});
322374
});
323375
});

‎test/fixtures/temp-1000/file.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('./file2')

‎test/fixtures/temp-1000/file2.js‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
original

0 commit comments

Comments
 (0)