Skip to content

Commit de8fc51

Browse files
committed
allow to export any property/export of the bundle
fixes webpack#3929
1 parent c123016 commit de8fc51

7 files changed

Lines changed: 80 additions & 2 deletions

File tree

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
"use strict";
6+
7+
const ConcatSource = require("webpack-sources").ConcatSource;
8+
9+
function accessorToObjectAccess(accessor) {
10+
return accessor.map(a => `[${JSON.stringify(a)}]`).join("");
11+
}
12+
13+
class ExportPropertyMainTemplatePlugin {
14+
constructor(property) {
15+
this.property = property;
16+
}
17+
18+
apply(compilation) {
19+
const mainTemplate = compilation.mainTemplate;
20+
compilation.templatesPlugin("render-with-entry", (source, chunk, hash) => {
21+
const postfix = `${accessorToObjectAccess([].concat(this.property))}`;
22+
return new ConcatSource(source, postfix);
23+
});
24+
mainTemplate.plugin("hash", hash => {
25+
hash.update("export property");
26+
hash.update(`${this.property}`);
27+
});
28+
}
29+
}
30+
31+
module.exports = ExportPropertyMainTemplatePlugin;

‎lib/LibraryTemplatePlugin.js‎

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,15 +26,20 @@ function accessorAccess(base, accessor, joinWith) {
2626

2727
class LibraryTemplatePlugin {
2828

29-
constructor(name, target, umdNamedDefine, auxiliaryComment) {
29+
constructor(name, target, umdNamedDefine, auxiliaryComment, exportProperty) {
3030
this.name = name;
3131
this.target = target;
3232
this.umdNamedDefine = umdNamedDefine;
3333
this.auxiliaryComment = auxiliaryComment;
34+
this.exportProperty = exportProperty;
3435
}
3536

3637
apply(compiler) {
3738
compiler.plugin("this-compilation", (compilation) => {
39+
if(this.exportProperty) {
40+
var ExportPropertyMainTemplatePlugin = require("./ExportPropertyMainTemplatePlugin");
41+
compilation.apply(new ExportPropertyMainTemplatePlugin(this.exportProperty));
42+
}
3843
switch(this.target) {
3944
case "var":
4045
compilation.apply(new SetVarMainTemplatePlugin(`var ${accessorAccess(false, this.name)}`));

‎lib/WebpackOptionsApply.js‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ class WebpackOptionsApply extends OptionsApply {
187187

188188
if(options.output.library || options.output.libraryTarget !== "var") {
189189
let LibraryTemplatePlugin = require("./LibraryTemplatePlugin");
190-
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget, options.output.umdNamedDefine, options.output.auxiliaryComment || ""));
190+
compiler.apply(new LibraryTemplatePlugin(options.output.library, options.output.libraryTarget, options.output.umdNamedDefine, options.output.auxiliaryComment || "", options.output.libraryExport));
191191
}
192192
if(options.externals) {
193193
ExternalsPlugin = require("./ExternalsPlugin");

‎schemas/webpackOptionsSchema.json‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,16 @@
389389
"jsonp"
390390
]
391391
},
392+
"libraryExport": {
393+
"anyOf": [
394+
{
395+
"type": "string"
396+
},
397+
{
398+
"$ref": "#/definitions/common.arrayOfStringValues"
399+
}
400+
]
401+
},
392402
"path": {
393403
"description": "The output directory as **absolute path** (required).",
394404
"type": "string",

‎test/configCases/library/0-create-library/webpack.config.js‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,18 @@ module.exports = [
2121
}
2222
}
2323
},
24+
{
25+
output: {
26+
filename: "umd-default.js",
27+
libraryTarget: "umd",
28+
libraryExport: "default"
29+
},
30+
resolve: {
31+
alias: {
32+
external: "./non-external"
33+
}
34+
}
35+
},
2436
{
2537
output: {
2638
filename: "this.js",
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import d from "library";
2+
var data = require("library");
3+
4+
it("should get default export from library (" + NAME + ")", function() {
5+
data.should.be.eql("default-value");
6+
d.should.be.eql("default-value");
7+
});

‎test/configCases/library/1-use-library/webpack.config.js‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,18 @@ module.exports = [
5151
TEST_EXTERNAL: true
5252
})
5353
]
54+
},
55+
{
56+
entry: "./default-test.js",
57+
resolve: {
58+
alias: {
59+
library: path.resolve(__dirname, "../../../js/config/library/0-create-library/umd-default.js")
60+
}
61+
},
62+
plugins: [
63+
new webpack.DefinePlugin({
64+
NAME: JSON.stringify("default")
65+
})
66+
]
5467
}
5568
];

0 commit comments

Comments
 (0)