Skip to content

Commit 8ad6d27

Browse files
authored
Merge pull request webpack#4987 from webpack/feature/library-default-export
allow to export properties/exports for a library
2 parents 8cd5cf7 + de8fc51 commit 8ad6d27

18 files changed

Lines changed: 203 additions & 46 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",
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./a";
22
export default "default-value";
33
export var b = "b";
4+
export { default as external } from "external";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export default "non-external";
Lines changed: 64 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,65 @@
1-
module.exports = {
2-
output: {
3-
libraryTarget: "commonjs-module"
1+
module.exports = [
2+
{
3+
output: {
4+
filename: "commonjs.js",
5+
libraryTarget: "commonjs-module"
6+
},
7+
resolve: {
8+
alias: {
9+
external: "./non-external"
10+
}
11+
}
12+
},
13+
{
14+
output: {
15+
filename: "umd.js",
16+
libraryTarget: "umd"
17+
},
18+
resolve: {
19+
alias: {
20+
external: "./non-external"
21+
}
22+
}
23+
},
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+
},
36+
{
37+
output: {
38+
filename: "this.js",
39+
libraryTarget: "this"
40+
},
41+
resolve: {
42+
alias: {
43+
external: "./non-external"
44+
}
45+
}
46+
},
47+
{
48+
output: {
49+
filename: "global.js",
50+
library: "globalName"
51+
},
52+
resolve: {
53+
alias: {
54+
external: "./non-external"
55+
}
56+
}
57+
},
58+
{
59+
output: {
60+
filename: "commonjs2-external.js",
61+
libraryTarget: "commonjs2"
62+
},
63+
externals: ["external"]
464
}
5-
};
65+
];
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+
});
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
var data = require("library");
2+
3+
it("should be able get items from library (" + NAME + ")", function() {
4+
data.should.have.property("default").be.eql("default-value");
5+
data.should.have.property("a").be.eql("a");
6+
data.should.have.property("b").be.eql("b");
7+
});
Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,14 @@
11
import d from "library";
2-
import { a, b } from "library";
2+
import { a, b, external } from "library";
33

4-
it("should be able to import hamorny exports from library", function() {
4+
it("should be able to import hamorny exports from library (" + NAME + ")", function() {
55
d.should.be.eql("default-value");
66
a.should.be.eql("a");
77
b.should.be.eql("b");
8-
});
8+
if(typeof TEST_EXTERNAL !== "undefined" && TEST_EXTERNAL) {
9+
external.should.be.eql(["external"]);
10+
external.should.be.equal(require("external"));
11+
} else {
12+
external.should.be.eql("non-external");
13+
}
14+
});

0 commit comments

Comments
 (0)