33 Author Tobias Koppers @sokra
44*/
55var path = require ( "path" ) ;
6+ var ContextElementDependency = require ( "./dependencies/ContextElementDependency" ) ;
67
78function ContextReplacementPlugin ( resourceRegExp , newContentResource , newContentRecursive , newContentRegExp ) {
89 this . resourceRegExp = resourceRegExp ;
910 if ( typeof newContentResource === "function" ) {
1011 this . newContentCallback = newContentResource ;
12+ } else if ( typeof newContentResource === "string" && typeof newContentRecursive === "object" ) {
13+ this . newContentResource = newContentResource ;
14+ this . newContentCreateContextMap = function ( fs , callback ) {
15+ callback ( null , newContentRecursive )
16+ } ;
17+ } else if ( typeof newContentResource === "string" && typeof newContentRecursive === "function" ) {
18+ this . newContentResource = newContentResource ;
19+ this . newContentCreateContextMap = newContentRecursive ;
1120 } else {
1221 if ( typeof newContentResource !== "string" ) {
1322 newContentRegExp = newContentRecursive ;
@@ -30,42 +39,55 @@ ContextReplacementPlugin.prototype.apply = function(compiler) {
3039 var newContentResource = this . newContentResource ;
3140 var newContentRecursive = this . newContentRecursive ;
3241 var newContentRegExp = this . newContentRegExp ;
42+ var newContentCreateContextMap = this . newContentCreateContextMap ;
3343 compiler . plugin ( "context-module-factory" , function ( cmf ) {
3444 cmf . plugin ( "before-resolve" , function ( result , callback ) {
3545 if ( ! result ) return callback ( ) ;
3646 if ( resourceRegExp . test ( result . request ) ) {
47+ if ( typeof newContentResource !== "undefined" )
48+ result . request = newContentResource ;
49+ if ( typeof newContentRecursive !== "undefined" )
50+ result . recursive = newContentRecursive ;
51+ if ( typeof newContentRegExp !== "undefined" )
52+ result . regExp = newContentRegExp ;
3753 if ( typeof newContentCallback === "function" ) {
3854 newContentCallback ( result ) ;
39- } else {
40- if ( typeof newContentResource !== "undefined" )
41- result . request = newContentResource ;
42- if ( typeof newContentRecursive !== "undefined" )
43- result . recursive = newContentRecursive ;
44- if ( typeof newContentRegExp !== "undefined" )
45- result . regExp = newContentRegExp ;
4655 }
4756 }
4857 return callback ( null , result ) ;
4958 } ) ;
5059 cmf . plugin ( "after-resolve" , function ( result , callback ) {
5160 if ( ! result ) return callback ( ) ;
5261 if ( resourceRegExp . test ( result . resource ) ) {
62+ if ( typeof newContentResource !== "undefined" )
63+ result . resource = path . resolve ( result . resource , newContentResource ) ;
64+ if ( typeof newContentRecursive !== "undefined" )
65+ result . recursive = newContentRecursive ;
66+ if ( typeof newContentRegExp !== "undefined" )
67+ result . regExp = newContentRegExp ;
68+ if ( typeof newContentCreateContextMap === "function" )
69+ result . resolveDependencies = createResolveDependenciesFromContextMap ( newContentCreateContextMap ) ;
5370 if ( typeof newContentCallback === "function" ) {
5471 var origResource = result . resource ;
5572 newContentCallback ( result ) ;
5673 if ( result . resource !== origResource ) {
5774 result . resource = path . resolve ( origResource , result . resource ) ;
5875 }
59- } else {
60- if ( typeof newContentResource !== "undefined" )
61- result . resource = path . resolve ( result . resource , newContentResource ) ;
62- if ( typeof newContentRecursive !== "undefined" )
63- result . recursive = newContentRecursive ;
64- if ( typeof newContentRegExp !== "undefined" )
65- result . regExp = newContentRegExp ;
6676 }
6777 }
6878 return callback ( null , result ) ;
6979 } ) ;
7080 } ) ;
7181} ;
82+
83+ function createResolveDependenciesFromContextMap ( createContextMap ) {
84+ return function resolveDependenciesFromContextMap ( fs , resource , recursive , regExp , callback ) {
85+ createContextMap ( fs , function ( err , map ) {
86+ if ( err ) return callback ( err ) ;
87+ var dependencies = Object . keys ( map ) . map ( function ( key ) {
88+ return new ContextElementDependency ( map [ key ] , key ) ;
89+ } ) ;
90+ callback ( null , dependencies ) ;
91+ } ) ;
92+ }
93+ } ;
0 commit comments