Skip to content

Commit f4ba0d0

Browse files
committed
support for importing into object shothand
fixed webpack#2522
1 parent 2edd1ad commit f4ba0d0

6 files changed

Lines changed: 45 additions & 7 deletions

File tree

lib/Parser.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -652,8 +652,12 @@ Parser.prototype.walkSpreadElement = function walkSpreadElement(expression) {
652652
Parser.prototype.walkObjectExpression = function walkObjectExpression(expression) {
653653
expression.properties.forEach(function(prop) {
654654
if(prop.computed)
655-
this.walkExpression(prop.key)
655+
this.walkExpression(prop.key);
656+
if(prop.shorthand)
657+
this.scope.inShorthand = true;
656658
this.walkExpression(prop.value);
659+
if(prop.shorthand)
660+
this.scope.inShorthand = false;
657661
}, this);
658662
};
659663

@@ -858,6 +862,7 @@ Parser.prototype.inScope = function inScope(params, fn) {
858862
var oldScope = this.scope;
859863
this.scope = {
860864
inTry: false,
865+
inShorthand: false,
861866
definitions: oldScope.definitions.slice(),
862867
renames: Object.create(oldScope.renames)
863868
};

lib/dependencies/HarmonyImportDependencyParserPlugin.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ module.exports = AbstractPlugin.create({
2929
var name = expr.name;
3030
var settings = this.state.harmonySpecifier["$" + name];
3131
var dep = new HarmonyImportSpecifierDependency(settings[0], settings[1], settings[2], name, expr.range);
32+
dep.shorthand = this.scope.inShorthand;
3233
dep.loc = expr.loc;
3334
this.state.current.addDependency(dep);
3435
return true;

lib/dependencies/HarmonyImportSpecifierDependency.js

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,15 @@ HarmonyImportSpecifierDependency.Template.prototype.apply = function(dep, source
5151
} else {
5252
content = dep.importedVar;
5353
}
54-
if(!dep.call) {
55-
source.replace(dep.range[0], dep.range[1] - 1, content);
56-
} else if(defaultImport) {
57-
source.replace(dep.range[0], dep.range[1] - 1, dep.importedVar + "_default()");
58-
} else {
59-
source.replace(dep.range[0], dep.range[1] - 1, "__webpack_require__.i(" + content + ")");
54+
if(dep.call) {
55+
if(defaultImport) {
56+
content = dep.importedVar + "_default()";
57+
} else {
58+
content = "__webpack_require__.i(" + content + ")";
59+
}
60+
}
61+
if(dep.shorthand) {
62+
content = dep.name + ": " + content;
6063
}
64+
source.replace(dep.range[0], dep.range[1] - 1, content);
6165
};
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { a, a as aa } from "./module";
2+
import b from "./module";
3+
import * as c from "./module";
4+
5+
it("should import into object shorthand", function() {
6+
var o = {
7+
a,
8+
aa,
9+
b,
10+
c
11+
};
12+
o.should.be.eql({
13+
a: 123,
14+
aa: 123,
15+
b: 456,
16+
c: {
17+
a: 123,
18+
default: 456
19+
}
20+
});
21+
})
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
export var a = 123;
2+
export default 456;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
var supportsES6 = require("../../../helpers/supportsES6");
2+
3+
module.exports = function(config) {
4+
return !config.minimize && supportsES6();
5+
};

0 commit comments

Comments
 (0)