Skip to content

Commit 66edde6

Browse files
committed
added libraryTarget commonjs-module
for commonjs wrapped harmony modules via `__esModule` fixes webpack#2945
1 parent a732888 commit 66edde6

8 files changed

Lines changed: 51 additions & 0 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
var ConcatSource = require("webpack-sources").ConcatSource;
6+
7+
function CommonJsHarmonyMainTemplatePlugin() {}
8+
9+
module.exports = CommonJsHarmonyMainTemplatePlugin;
10+
11+
CommonJsHarmonyMainTemplatePlugin.prototype.apply = function(compilation) {
12+
var mainTemplate = compilation.mainTemplate;
13+
compilation.templatesPlugin("render-with-entry", function(source, chunk, hash) {
14+
var prefix = "module.exports =\n";
15+
var postfix = "\nObject.defineProperty(module.exports, \"__esModule\", { value: true });";
16+
return new ConcatSource(prefix, source, postfix);
17+
});
18+
mainTemplate.plugin("hash", function(hash) {
19+
hash.update("commonjs harmony");
20+
});
21+
};

lib/LibraryTemplatePlugin.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
Author Tobias Koppers @sokra
44
*/
55
var SetVarMainTemplatePlugin = require("./SetVarMainTemplatePlugin");
6+
var CommonJsHarmonyMainTemplatePlugin = require("./CommonJsHarmonyMainTemplatePlugin");
67

78
function accessorToObjectAccess(accessor) {
89
return accessor.map(function(a) {
@@ -55,6 +56,9 @@ LibraryTemplatePlugin.prototype.apply = function(compiler) {
5556
case "commonjs2":
5657
compilation.apply(new SetVarMainTemplatePlugin("module.exports"));
5758
break;
59+
case "commonjs-module":
60+
compilation.apply(new CommonJsHarmonyMainTemplatePlugin());
61+
break;
5862
case "amd":
5963
var AmdMainTemplatePlugin = require("./AmdMainTemplatePlugin");
6064
compilation.apply(new AmdMainTemplatePlugin(this.name));
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export var a = "a";
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
export * from "./a";
2+
export default "default-value";
3+
export var b = "b";
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
exports.noTests = true;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
module.exports = {
2+
output: {
3+
libraryTarget: "commonjs-module"
4+
}
5+
};
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import d from "library";
2+
import { a, b } from "library";
3+
4+
it("should be able to import hamorny exports from library", function() {
5+
d.should.be.eql("default-value");
6+
a.should.be.eql("a");
7+
b.should.be.eql("b");
8+
});
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
var path = require("path");
2+
module.exports = {
3+
resolve: {
4+
alias: {
5+
library: path.resolve(__dirname, "../../../js/config/library/0-create-library/bundle0.js")
6+
}
7+
}
8+
};

0 commit comments

Comments
 (0)