1
1
var glob = require ( 'glob' ) ;
2
2
var path = require ( 'path' ) ;
3
+ var os = require ( 'os' ) ;
4
+ var SingleEntryPlugin = require ( 'webpack/lib/SingleEntryPlugin' ) ;
3
5
4
6
function computeGlob ( pattern , options ) {
5
7
return new Promise ( ( resolve , reject ) => {
@@ -26,26 +28,48 @@ function WatchTestFilesPlugin(testGlobs) {
26
28
this . testGlobs = testGlobs || [ ] ;
27
29
}
28
30
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
+ } ) ;
43
52
} ) ;
53
+ }
54
+
55
+ WatchTestFilesPlugin . prototype . apply = function ( compiler ) {
44
56
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 ) ;
49
73
} ) ;
50
74
} ;
51
75
0 commit comments