@@ -20,7 +20,7 @@ function camelize(str) {
20
20
// to use the same functionality as gulp-util for backwards compatibility
21
21
// with gulp 3x cli
22
22
function logger ( ) {
23
- if ( hasGulplog ( ) ) {
23
+ if ( hasGulplog ( ) ) {
24
24
// specifically deferring loading here to keep from registering it globally
25
25
var gulplog = require ( 'gulplog' ) ;
26
26
gulplog . info . apply ( gulplog , arguments ) ;
@@ -39,7 +39,7 @@ module.exports = function(options) {
39
39
40
40
var DEBUG = options . DEBUG || false ;
41
41
var pattern = arrayify ( options . pattern || [ 'gulp-*' , 'gulp.*' , '@*/gulp{-,.}*' ] ) ;
42
- var config = options . config || findup ( 'package.json' , { cwd : parentDir } ) ;
42
+ var config = options . config || findup ( 'package.json' , { cwd : parentDir } ) ;
43
43
var scope = arrayify ( options . scope || [ 'dependencies' , 'devDependencies' , 'peerDependencies' ] ) ;
44
44
var replaceString = options . replaceString || / ^ g u l p ( - | \. ) / ;
45
45
var camelizePluginName = options . camelize !== false ;
@@ -48,15 +48,17 @@ module.exports = function(options) {
48
48
49
49
logDebug ( 'Debug enabled with options: ' + JSON . stringify ( options ) ) ;
50
50
51
- var renameFn = options . renameFn || function ( name ) {
51
+ var renameFn = options . renameFn || function ( name ) {
52
52
name = name . replace ( replaceString , '' ) ;
53
53
return camelizePluginName ? camelize ( name ) : name ;
54
54
} ;
55
55
56
- if ( typeof options . requireFn === 'function' ) {
56
+ var postRequireTransforms = options . postRequireTransforms || { } ;
57
+
58
+ if ( typeof options . requireFn === 'function' ) {
57
59
requireFn = options . requireFn ;
58
- } else if ( typeof config === 'string' ) {
59
- requireFn = function ( name ) {
60
+ } else if ( typeof config === 'string' ) {
61
+ requireFn = function ( name ) {
60
62
// This searches up from the specified package.json file, making sure
61
63
// the config option behaves as expected. See issue #56.
62
64
var src = resolve . sync ( name , { basedir : path . dirname ( config ) } ) ;
@@ -68,7 +70,7 @@ module.exports = function(options) {
68
70
69
71
configObject = ( typeof config === 'string' ) ? require ( config ) : config ;
70
72
71
- if ( ! configObject ) {
73
+ if ( ! configObject ) {
72
74
throw new Error ( 'Could not find dependencies. Do you have a package.json file in your project?' ) ;
73
75
}
74
76
@@ -81,36 +83,36 @@ module.exports = function(options) {
81
83
pattern . push ( '!gulp-load-plugins' ) ;
82
84
83
85
function logDebug ( message ) {
84
- if ( DEBUG ) {
86
+ if ( DEBUG ) {
85
87
logger ( 'gulp-load-plugins: ' + message ) ;
86
88
}
87
89
}
88
90
89
- function defineProperty ( object , requireName , name ) {
90
- if ( object [ requireName ] ) {
91
+ function defineProperty ( object , transform , requireName , name ) {
92
+ if ( object [ requireName ] ) {
91
93
logDebug ( 'error: defineProperty ' + name ) ;
92
- throw new Error ( 'Could not define the property \ "' + requireName + '\ ", you may have repeated dependencies in your package.json like' + ' "gulp-' + requireName + '" and ' + '"' + requireName + '"' ) ;
94
+ throw new Error ( 'Could not define the property "' + requireName + '", you may have repeated dependencies in your package.json like' + ' "gulp-' + requireName + '" and ' + '"' + requireName + '"' ) ;
93
95
}
94
96
95
- if ( lazy ) {
97
+ if ( lazy ) {
96
98
logDebug ( 'lazyload: adding property ' + requireName ) ;
97
99
Object . defineProperty ( object , requireName , {
98
100
enumerable : true ,
99
101
get : function ( ) {
100
102
logDebug ( 'lazyload: requiring ' + name + '...' ) ;
101
- return requireFn ( name ) ;
103
+ return transform ( requireName , requireFn ( name ) ) ;
102
104
}
103
105
} ) ;
104
106
} else {
105
107
logDebug ( 'requiring ' + name + '...' ) ;
106
- object [ requireName ] = requireFn ( name ) ;
108
+ object [ requireName ] = transform ( requireName , requireFn ( name ) ) ;
107
109
}
108
110
}
109
111
110
112
function getRequireName ( name ) {
111
113
var requireName ;
112
114
113
- if ( renameObj [ name ] ) {
115
+ if ( renameObj [ name ] ) {
114
116
requireName = options . rename [ name ] ;
115
117
} else {
116
118
requireName = renameFn ( name ) ;
@@ -121,23 +123,33 @@ module.exports = function(options) {
121
123
return requireName ;
122
124
}
123
125
126
+ function applyTransform ( requireName , plugin ) {
127
+ var transform = postRequireTransforms [ requireName ] ;
128
+
129
+ if ( transform && typeof transform === 'function' ) { // if postRequireTransform function is passed, pass it the plugin and return it
130
+ logDebug ( 'transforming ' + requireName ) ;
131
+ return transform ( plugin ) ;
132
+ } else {
133
+ return plugin ; // if no postRequireTransform function passed, return the plugin as is
134
+ }
135
+ }
136
+
124
137
var scopeTest = new RegExp ( '^@' ) ;
125
138
var scopeDecomposition = new RegExp ( '^@(.+)/(.+)' ) ;
126
139
127
140
unique ( micromatch ( names , pattern ) ) . forEach ( function ( name ) {
128
141
var decomposition ;
129
- if ( scopeTest . test ( name ) ) {
142
+ if ( scopeTest . test ( name ) ) {
130
143
decomposition = scopeDecomposition . exec ( name ) ;
131
144
132
- if ( ! finalObject . hasOwnProperty ( decomposition [ 1 ] ) ) {
145
+ if ( ! finalObject . hasOwnProperty ( decomposition [ 1 ] ) ) {
133
146
finalObject [ decomposition [ 1 ] ] = { } ;
134
147
}
135
148
136
- defineProperty ( finalObject [ decomposition [ 1 ] ] , getRequireName ( decomposition [ 2 ] ) , name ) ;
149
+ defineProperty ( finalObject [ decomposition [ 1 ] ] , applyTransform , getRequireName ( decomposition [ 2 ] ) , name ) ;
137
150
} else {
138
- defineProperty ( finalObject , getRequireName ( name ) , name ) ;
151
+ defineProperty ( finalObject , applyTransform , getRequireName ( name ) , name ) ;
139
152
}
140
-
141
153
} ) ;
142
154
143
155
return finalObject ;
0 commit comments