Skip to content

Commit e0f975e

Browse files
committed
1 parent 47aae0c commit e0f975e

File tree

11 files changed

+93
-10
lines changed

11 files changed

+93
-10
lines changed

lib/Chunk.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ Chunk.prototype.addOrigin = function(module, loc) {
7676
};
7777

7878
Chunk.prototype.remove = function(reason) {
79-
// console.log("remove " + this.toString());
8079
this.modules.slice().forEach(function(m) {
8180
m.removeChunk(this);
8281
}, this);
@@ -114,6 +113,7 @@ Chunk.prototype.integrate = function(other, reason) {
114113
m.removeChunk(other);
115114
m.addChunk(this);
116115
this.addModule(m);
116+
m.rewriteChunkInReasons(other, [this]);
117117
}, this);
118118
other.modules.length = 0;
119119
function moveChunks(chunks, kind, onChunk) {

lib/Module.js

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,12 +61,36 @@ Module.prototype.removeReason = function(module, dependency) {
6161
Module.prototype.hasReasonForChunk = function(chunk) {
6262
for(var i = 0; i < this.reasons.length; i++) {
6363
var r = this.reasons[i];
64-
if(r.module.chunks.indexOf(chunk) >= 0)
64+
if(r.chunks) {
65+
if(r.chunks.indexOf(chunk) >= 0)
66+
return true;
67+
} else if(r.module.chunks.indexOf(chunk) >= 0)
6568
return true;
6669
}
6770
return false;
6871
};
6972

73+
function addToSet(set, items) {
74+
items.forEach(function(item) {
75+
if(set.indexOf(item) < 0)
76+
set.push(item);
77+
});
78+
}
79+
80+
Module.prototype.rewriteChunkInReasons = function(oldChunk, newChunks) {
81+
this.reasons.forEach(function(r) {
82+
if(!r.chunks) {
83+
if(r.module.chunks.indexOf(oldChunk) < 0)
84+
return;
85+
r.chunks = r.module.chunks;
86+
}
87+
r.chunks = r.chunks.reduce(function(arr, c) {
88+
addToSet(arr, c !== oldChunk ? [c] : newChunks);
89+
return arr;
90+
}, []);
91+
});
92+
};
93+
7094
Module.prototype.toString = function() {
7195
return "Module[" + (this.id || this.debugId) + "]";
7296
};

lib/SourceMapDevToolPlugin.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,10 @@ SourceMapDevToolPlugin.prototype.apply = function(compiler) {
100100
sourceMap.sourceRoot = "";
101101
sourceMap.file = file;
102102
asset.__SourceMapDevTool_Data = {};
103+
var currentSourceMappingURLComment = sourceMappingURLComment;
104+
if(currentSourceMappingURLComment !== false && /\.css($|\?)/i.test(file)) {
105+
currentSourceMappingURLComment = currentSourceMappingURLComment.replace(/^\n\/\/(.*)$/, "\n/*$1*/");
106+
}
103107
if(sourceMapFilename) {
104108
var filename = file, query = "";
105109
var idx = filename.indexOf("?");
@@ -114,13 +118,13 @@ SourceMapDevToolPlugin.prototype.apply = function(compiler) {
114118
.replace(Template.REGEXP_HASH, this.hash)
115119
.replace(Template.REGEXP_ID, chunk.id);
116120
var sourceMapUrl = path.relative(path.dirname(file), sourceMapFile).replace(/\\/g, "/");
117-
if(sourceMappingURLComment !== false) {
118-
asset.__SourceMapDevTool_Data[file] = this.assets[file] = new ConcatSource(asset, sourceMappingURLComment.replace(/\[url\]/g, sourceMapUrl));
121+
if(currentSourceMappingURLComment !== false) {
122+
asset.__SourceMapDevTool_Data[file] = this.assets[file] = new ConcatSource(asset, currentSourceMappingURLComment.replace(/\[url\]/g, sourceMapUrl));
119123
}
120124
asset.__SourceMapDevTool_Data[sourceMapFile] = this.assets[sourceMapFile] = new RawSource(JSON.stringify(sourceMap));
121125
chunk.files.push(sourceMapFile);
122126
} else {
123-
asset.__SourceMapDevTool_Data[file] = this.assets[file] = new ConcatSource(asset, sourceMappingURLComment.replace(/\[url\]/g, "data:application/json;base64," + new Buffer(JSON.stringify(sourceMap)).toString("base64")));
127+
asset.__SourceMapDevTool_Data[file] = this.assets[file] = new ConcatSource(asset, currentSourceMappingURLComment.replace(/\[url\]/g, "data:application/json;base64," + new Buffer(JSON.stringify(sourceMap)).toString("base64")));
124128
}
125129
}, this);
126130
});

lib/SourceMapSource.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
/*
2+
MIT License http://www.opensource.org/licenses/mit-license.php
3+
Author Tobias Koppers @sokra
4+
*/
5+
module.exports = require("webpack-core/lib/SourceMapSource");

lib/optimize/RemoveParentModulesPlugin.js

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
Author Tobias Koppers @sokra
44
*/
55
function hasModule(chunk, module, checkedChunks) {
6-
if(chunk.modules.indexOf(module) >= 0) return true;
6+
if(chunk.modules.indexOf(module) >= 0) return [chunk];
77
if(chunk.entry) return false;
88
return allHaveModule(chunk.parents.filter(function(c) {
99
return checkedChunks.indexOf(c) < 0;
@@ -12,11 +12,21 @@ function hasModule(chunk, module, checkedChunks) {
1212

1313
function allHaveModule(someChunks, module, checkedChunks) {
1414
if(!checkedChunks) checkedChunks = [];
15+
var chunks = [];
1516
for(var i = 0; i < someChunks.length; i++) {
1617
checkedChunks.push(someChunks[i]);
17-
if(!hasModule(someChunks[i], module, checkedChunks)) return false;
18+
var subChunks = hasModule(someChunks[i], module, checkedChunks);
19+
if(!subChunks) return false;
20+
addToSet(chunks, subChunks);
1821
}
19-
return true;
22+
return chunks;
23+
}
24+
25+
function addToSet(set, items) {
26+
items.forEach(function(item) {
27+
if(set.indexOf(item) < 0)
28+
set.push(item);
29+
});
2030
}
2131

2232
function RemoveParentModulesPlugin() {
@@ -29,7 +39,9 @@ RemoveParentModulesPlugin.prototype.apply = function(compiler) {
2939
chunks.forEach(function(chunk) {
3040
chunk.modules.slice().forEach(function(module) {
3141
if(chunk.entry) return;
32-
if(allHaveModule(chunk.parents, module)) {
42+
var parentChunksWithModule = allHaveModule(chunk.parents, module);
43+
if(parentChunksWithModule) {
44+
module.rewriteChunkInReasons(chunk, parentChunksWithModule);
3345
chunk.removeModule(module);
3446
}
3547
});

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"url-loader": "~0.5.0",
4343
"val-loader": "~0.5.0",
4444
"i18n-webpack-plugin": "~0.2.0",
45-
"extract-text-webpack-plugin": "~0.2.3",
45+
"extract-text-webpack-plugin": "~0.3.0",
4646
"component-webpack-plugin": "~0.2.0"
4747
},
4848
"engines": {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
body{base:0}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
var should = require("should");
2+
var path = require("path");
3+
var fs = require("fs");
4+
5+
it("should behave correctly with styles extracted", function(done) {
6+
var a = require("./styleA.css");
7+
var bundle = fs.readFileSync(path.join(__dirname, "bundle0.js"), "utf-8");
8+
var style = fs.readFileSync(path.join(__dirname, "style.css"), "utf-8");
9+
bundle.should.not.match(/body\{a:1\}/);
10+
style.should.be.eql("body{base:0}body{" + "a:1}");
11+
a.should.be.eql({});
12+
require.ensure([], function(require) {
13+
var b = require("./styleB.css");
14+
(b + "").should.be.eql("body{base:0}body{b:2}");
15+
done();
16+
});
17+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "base.css";body{a:1}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
@import "base.css";body{b:2}

0 commit comments

Comments
 (0)