Skip to content

Commit e0d373b

Browse files
committed
Create some sub compilations for tests
This result will not be used for serving the pages but will be used for tracking lint/type errors in those files
1 parent 19f86d1 commit e0d373b

File tree

2 files changed

+48
-18
lines changed

2 files changed

+48
-18
lines changed

packages/react-dev-utils/WatchTestFilesPlugin.js

+42-18
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
var glob = require('glob');
22
var path = require('path');
3+
var os = require('os');
4+
var SingleEntryPlugin = require('webpack/lib/SingleEntryPlugin');
35

46
function computeGlob(pattern, options) {
57
return new Promise((resolve, reject) => {
@@ -26,26 +28,48 @@ function WatchTestFilesPlugin(testGlobs) {
2628
this.testGlobs = testGlobs || [];
2729
}
2830

29-
WatchTestFilesPlugin.prototype.apply = function(compiler) {
30-
var testFiles = [];
31-
compiler.plugin('make', (compilation, callback) => {
32-
getGlobs(this.testGlobs, compiler.options.context)
33-
.then(foundFiles => {
34-
testFiles = foundFiles;
35-
return Promise.all(
36-
testFiles.map(filename => new Promise((resolve, reject) => {
37-
// TODO: add to modules
38-
resolve(filename);
39-
}))
40-
)
41-
})
42-
.then(callback);
31+
function compileTestFile(compiler, compilation, context, testFile) {
32+
var outputOptions = {
33+
filename: path.join(os.tmpdir(), '__compiledTests__', testFile),
34+
publicPath: compilation.outputOptions.publicPath,
35+
};
36+
var compilerName = "WatchTestFiles compilation for " + testFile;
37+
var childCompiler = compilation.createChildCompiler(compilerName, outputOptions);
38+
childCompiler.context = context;
39+
childCompiler.apply(
40+
new SingleEntryPlugin(context, path.join(compiler.options.context, testFile))
41+
);
42+
return new Promise((resolve, reject) => {
43+
childCompiler.runAsChild((err, entries, childCompilation) => {
44+
if (err) {
45+
return reject(err);
46+
}
47+
resolve({
48+
errors: childCompilation.errors,
49+
warnings: childCompilation.warnings,
50+
});
51+
});
4352
});
53+
}
54+
55+
WatchTestFilesPlugin.prototype.apply = function(compiler) {
4456
compiler.plugin('emit', (compilation, callback) => {
45-
testFiles.forEach(testFile => {
46-
compilation.fileDependencies.push(path.join(compiler.options.context, testFile));
47-
});
48-
callback();
57+
getGlobs(this.testGlobs, compiler.options.context)
58+
.then(foundFiles => Promise.all(
59+
foundFiles.map(filename => {
60+
// Add them to the list of watched files (for auto-reloading)
61+
compilation.fileDependencies.push(path.join(compiler.options.context, filename));
62+
// Create and run a sub-compiler for the file to send it through the loaders
63+
return compileTestFile(compiler, compilation, compiler.context, filename)
64+
})
65+
))
66+
.then((results) => {
67+
var errors = results.reduce((list, res) => list.concat(res.errors || []), []);
68+
var warnings = results.reduce((list, res) => list.concat(res.warnings || []), []);
69+
compilation.errors = compilation.errors.concat(errors);
70+
compilation.warnings = compilation.warnings.concat(warnings);
71+
callback();
72+
}, callback);
4973
});
5074
};
5175

packages/react-dev-utils/package.json

+6
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,11 @@
3232
"opn": "4.0.2",
3333
"sockjs-client": "1.0.3",
3434
"strip-ansi": "3.0.1"
35+
},
36+
"devDependencies": {
37+
"webpack": "1.14.0"
38+
},
39+
"peerDependencies": {
40+
"webpack": "1.14.0"
3541
}
3642
}

0 commit comments

Comments
 (0)