-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
50 lines (39 loc) · 1.66 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
module.exports = function( input ) {
var finalDependencies;
this.cacheable();
var options = this.options.amdInjectLoader || {};
var istanbul = options.istanbul === true;
var stripComments = options.stripComments === true;
// Match AMD define and function
var rCapture = /(^|;)\s*define\((?:[ ]?[^,]*,)?[ ]?(\[[\s\S]*?\]),[ ]?function[ ]?\(([^)]+)?\)[ ]?{/m;
var matched = rCapture.exec( input );
if ( !matched ) {
throw new Error( "The amd-inject-loader only supports AMD files with dependencies." );
}
var rawDependencies = matched[ 2 ];
if ( stripComments ) {
rawDependencies = rawDependencies.replace(/\/\/.+/ig, '');
}
try {
finalDependencies = JSON.parse( rawDependencies.replace( /'/g, "\"" ) );
} catch (e) {
throw new Error( "JSON parsing failed in amd-inject-loader." );
}
var args = ( matched[ 3 ] || "" ).trim().split( /,[ ]?/g );
var injectorCode = [];
// Build list of CommonJS style require statements
finalDependencies.forEach( function( dep, index ) {
var arg = args[ index ];
if ( istanbul ) {
injectorCode.push( "/* istanbul ignore next - the following line of code is used for dependency injection */" );
}
if ( !arg ) {
injectorCode.push( "( injections && injections.hasOwnProperty(\"" + dep + "\") ) || require( \"" + dep + "\" );" );
} else {
injectorCode.push( "var " + arg + " = ( injections && injections.hasOwnProperty(\"" + dep + "\") ) ? injections[\"" + dep + "\"] : require( \"" + dep + "\" );" );
}
} );
// Swap out define call with new injection style
input = input.replace( rCapture, matched[1] + "module.exports = ( function ( injections ) { \n\t" + injectorCode.join( "\n\t" ) );
return input;
};