Skip to content

Commit 3eba627

Browse files
committed
adjusted tests to use css-loader, added tests for allChunks, remove module from initial chunk
1 parent 09ac8cb commit 3eba627

File tree

30 files changed

+216
-56
lines changed

30 files changed

+216
-56
lines changed

index.js

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -274,10 +274,8 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
274274
if(meta && (!meta.options.id || meta.options.id === id)) {
275275
var wasExtracted = Array.isArray(meta.content);
276276
if(shouldExtract !== wasExtracted) {
277-
if (shouldExtract) {
278-
chunk.removeModule(module);
279-
return callback();
280-
}
277+
// don't remove
278+
module.loaders[0].options.remove = !shouldExtract;
281279
module[NS + "/extract"] = shouldExtract; // eslint-disable-line no-path-concat
282280
compilation.rebuildModule(module, function(err) {
283281
if(err) {
@@ -291,8 +289,11 @@ ExtractTextPlugin.prototype.apply = function(compiler) {
291289
compilation.errors.push(err);
292290
return callback();
293291
}
294-
if(meta.content)
292+
if(meta.content) {
295293
extractCompilation.addResultToChunk(module.identifier(), meta.content, module, extractedChunk);
294+
// remove generated result from chunk
295+
chunk.removeModule(module);
296+
}
296297
callback();
297298
});
298299
} else {

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
"mocha": "^3.2.0",
3333
"mocha-lcov-reporter": "1.3.0",
3434
"raw-loader": "^0.5.1",
35+
"rimraf": "^2.6.1",
3536
"should": "^11.1.2",
3637
"standard-version": "^4.1.0",
3738
"style-loader": "^0.18.2",

test/TestCases.test.js

Lines changed: 33 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ var vm = require("vm");
33
var path = require("path");
44
var webpack = require("webpack");
55
var should = require("should");
6+
var rimraf = require('rimraf');
67
var ExtractTextPlugin = require("../");
78

89
var cases = process.env.CASES ? process.env.CASES.split(",") : fs.readdirSync(path.join(__dirname, "cases"));
@@ -12,36 +13,39 @@ describe("TestCases", function() {
1213
it(testCase, function(done) {
1314
var testDirectory = path.join(__dirname, "cases", testCase);
1415
var outputDirectory = path.join(__dirname, "js", testCase);
15-
var options = { entry: { test: "./index.js" } };
16-
var configFile = path.join(testDirectory, "webpack.config.js");
17-
if(fs.existsSync(configFile))
18-
options = require(configFile);
19-
options.context = testDirectory;
20-
if(!options.module) options.module = {};
21-
if(!options.module.loaders) options.module.loaders = [
22-
{ test: /\.txt$/, loader: ExtractTextPlugin.extract("raw-loader") }
23-
];
24-
if(!options.output) options.output = { filename: "[name].js" };
25-
if(!options.output.path) options.output.path = outputDirectory;
26-
if(process.env.CASES) {
27-
console.log("\nwebpack." + testCase + ".config.js " + JSON.stringify(options, null, 2));
28-
}
29-
webpack(options, function(err, stats) {
30-
if(err) return done(err);
31-
if(stats.hasErrors()) return done(new Error(stats.toString()));
32-
var testFile = path.join(outputDirectory, "test.js");
33-
if(fs.existsSync(testFile))
34-
require(testFile)(suite);
35-
var expectedDirectory = path.join(testDirectory, "expected");
36-
fs.readdirSync(expectedDirectory).forEach(function(file) {
37-
var filePath = path.join(expectedDirectory, file);
38-
var actualPath = path.join(outputDirectory, file);
39-
readFileOrEmpty(actualPath).should.be.eql(
40-
readFileOrEmpty(filePath),
41-
file + " should be correct");
16+
function test() {
17+
var options = { entry: { test: "./index.js" } };
18+
var configFile = path.join(testDirectory, "webpack.config.js");
19+
if (fs.existsSync(configFile))
20+
options = require(configFile);
21+
options.context = testDirectory;
22+
if (!options.module) options.module = {};
23+
if (!options.module.loaders) options.module.loaders = [
24+
{ test: /\.txt$/, loader: ExtractTextPlugin.extract("raw-loader") }
25+
];
26+
if (!options.output) options.output = { filename: "[name].js" };
27+
if (!options.output.path) options.output.path = outputDirectory;
28+
if (process.env.CASES) {
29+
//console.log("\nwebpack." + testCase + ".config.js " + JSON.stringify(options, null, 2));
30+
}
31+
webpack(options, function (err, stats) {
32+
if (err) return done(err);
33+
if (stats.hasErrors()) return done(new Error(stats.toString()));
34+
var testFile = path.join(outputDirectory, "test.js");
35+
if (fs.existsSync(testFile))
36+
require(testFile)(suite);
37+
var expectedDirectory = path.join(testDirectory, "expected");
38+
fs.readdirSync(expectedDirectory).forEach(function (file) {
39+
var filePath = path.join(expectedDirectory, file);
40+
var actualPath = path.join(outputDirectory, file);
41+
readFileOrEmpty(actualPath).should.be.eql(
42+
readFileOrEmpty(filePath),
43+
file + " should be correct");
44+
});
45+
done();
4246
});
43-
done();
44-
});
47+
}
48+
rimraf(outputDirectory, test);
4549
});
4650
});
4751
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: red;
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('../router');
2+
require('../routes/contact');
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
require('../router');
2+
require('../routes/homepage');
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
webpackJsonp([0],{
2+
3+
/***/ 2:
4+
/***/ (function(module, exports, __webpack_require__) {
5+
6+
__webpack_require__(7);
7+
8+
modules.export = function() {
9+
return 'Route Homepage';
10+
};
11+
12+
13+
/***/ }),
14+
15+
/***/ 7:
16+
/***/ (function(module, exports) {
17+
18+
// removed by extract-text-webpack-plugin
19+
20+
/***/ })
21+
22+
});
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
webpackJsonp([1],{
2+
3+
/***/ 1:
4+
/***/ (function(module, exports, __webpack_require__) {
5+
6+
__webpack_require__(6);
7+
8+
modules.export = function() {
9+
return 'Route Contact';
10+
};
11+
12+
13+
/***/ }),
14+
15+
/***/ 6:
16+
/***/ (function(module, exports) {
17+
18+
// removed by extract-text-webpack-plugin
19+
20+
/***/ })
21+
22+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body {
2+
background: red;
3+
}
4+
.contact {
5+
color: black;
6+
}
7+
.homepage {
8+
color: black;
9+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
body {
2+
background: red;
3+
}
4+
.contact {
5+
color: black;
6+
}
7+
.homepage {
8+
color: black;
9+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
require('./default-styles.css');
2+
module.export = function (route) {
3+
return import(/* webpackChunkName: "route-[request]" */ './routes/' + route + 'index.js').then(function (route) {
4+
return route;
5+
});
6+
};
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require('./styles.css');
2+
3+
modules.export = function() {
4+
return 'Route Contact';
5+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.contact {
2+
color: black;
3+
}
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
require('./styles.css');
2+
3+
modules.export = function() {
4+
return 'Route Homepage';
5+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.homepage {
2+
color: black;
3+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
var ExtractTextPlugin = require("../../../");
2+
module.exports = {
3+
entry: {
4+
'homepage': "./entries/homepage.js",
5+
'contact': "./entries/contact.js"
6+
},
7+
module: {
8+
loaders: [
9+
{ test: /\.css$/, use: ExtractTextPlugin.extract({
10+
fallback: "style-loader",
11+
use: { loader: "css-loader", options: {
12+
sourceMap: true
13+
} }
14+
}) }
15+
]
16+
},
17+
plugins: [
18+
new ExtractTextPlugin({
19+
filename: '[name].css',
20+
allChunks: true
21+
})
22+
]
23+
};
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
body {
2+
background: red;
3+
}

test/cases/multiple-entries-async/default-styles.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/cases/multiple-entries-async/expected/0.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,29 @@
11
webpackJsonp([0],{
22

3-
/***/ 2:
3+
/***/ 11:
44
/***/ (function(module, exports, __webpack_require__) {
55

6-
__webpack_require__(9);
6+
exports = module.exports = __webpack_require__(5)(true);
7+
// imports
78

8-
modules.export = function() {
9-
return 'Route Homepage';
10-
};
9+
10+
// module
11+
exports.push([module.i, ".homepage {\n\tcolor: black;\n}\n", "", {"version":3,"sources":["/Users/bowlingx/Projekte/extract-text-webpack-plugin/test/cases/multiple-entries-async/routes/homepage/styles.css"],"names":[],"mappings":"AAAA;CACC,aAAa;CACb","file":"styles.css","sourcesContent":[".homepage {\n\tcolor: black;\n}\n"],"sourceRoot":""}]);
12+
13+
// exports
1114

1215

1316
/***/ }),
1417

15-
/***/ 9:
16-
/***/ (function(module, exports) {
18+
/***/ 2:
19+
/***/ (function(module, exports, __webpack_require__) {
20+
21+
__webpack_require__(11);
22+
23+
modules.export = function() {
24+
return 'Route Homepage';
25+
};
1726

18-
module.exports = "styles-homepage\n"
1927

2028
/***/ })
2129

test/cases/multiple-entries-async/expected/1.js

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ webpackJsonp([1],{
33
/***/ 1:
44
/***/ (function(module, exports, __webpack_require__) {
55

6-
__webpack_require__(8);
6+
__webpack_require__(10);
77

88
modules.export = function() {
99
return 'Route Contact';
@@ -12,10 +12,18 @@ modules.export = function() {
1212

1313
/***/ }),
1414

15-
/***/ 8:
16-
/***/ (function(module, exports) {
15+
/***/ 10:
16+
/***/ (function(module, exports, __webpack_require__) {
17+
18+
exports = module.exports = __webpack_require__(5)(true);
19+
// imports
20+
21+
22+
// module
23+
exports.push([module.i, ".contact {\n\tcolor: black;\n}\n", "", {"version":3,"sources":["/Users/bowlingx/Projekte/extract-text-webpack-plugin/test/cases/multiple-entries-async/routes/contact/styles.css"],"names":[],"mappings":"AAAA;CACC,aAAa;CACb","file":"styles.css","sourcesContent":[".contact {\n\tcolor: black;\n}\n"],"sourceRoot":""}]);
24+
25+
// exports
1726

18-
module.exports = "styles-contact\n"
1927

2028
/***/ })
2129

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
default-styles
2-
styles-contact
1+
body {
2+
background: red;
3+
}
4+
.contact {
5+
color: black;
6+
}
Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1-
default-styles
2-
styles-homepage
1+
body {
2+
background: red;
3+
}
4+
.homepage {
5+
color: black;
6+
}

test/cases/multiple-entries-async/router.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require('./default-styles.txt');
1+
require('./default-styles.css');
22
module.export = function (route) {
33
return import(/* webpackChunkName: "route-[request]" */ './routes/' + route + 'index.js').then(function (route) {
44
return route;

test/cases/multiple-entries-async/routes/contact/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require('./styles.txt');
1+
require('./styles.css');
22

33
modules.export = function() {
44
return 'Route Contact';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.contact {
2+
color: black;
3+
}

test/cases/multiple-entries-async/routes/contact/styles.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/cases/multiple-entries-async/routes/homepage/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require('./styles.txt');
1+
require('./styles.css');
22

33
modules.export = function() {
44
return 'Route Homepage';
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.homepage {
2+
color: black;
3+
}

test/cases/multiple-entries-async/routes/homepage/styles.txt

Lines changed: 0 additions & 1 deletion
This file was deleted.

test/cases/multiple-entries-async/webpack.config.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,16 @@ module.exports = {
44
'homepage': "./entries/homepage.js",
55
'contact': "./entries/contact.js"
66
},
7+
module: {
8+
loaders: [
9+
{ test: /\.css$/, use: ExtractTextPlugin.extract({
10+
fallback: "style-loader",
11+
use: { loader: "css-loader", options: {
12+
sourceMap: true
13+
} }
14+
}) }
15+
]
16+
},
717
plugins: [
818
new ExtractTextPlugin({
919
filename: '[name].css',

0 commit comments

Comments
 (0)