2013-02-14 00:00:07 +08:00
/ *
MIT License http : //www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @ sokra
* /
2016-09-30 22:24:05 +08:00
var objectAssign = require ( 'object-assign' ) ;
2013-02-14 00:00:07 +08:00
var path = require ( "path" ) ;
var ModuleParserHelpers = require ( "./ModuleParserHelpers" ) ;
var ConstDependency = require ( "./dependencies/ConstDependency" ) ;
2013-02-17 05:23:22 +08:00
var BasicEvaluatedExpression = require ( "./BasicEvaluatedExpression" ) ;
2013-10-11 16:42:25 +08:00
var UnsupportedFeatureWarning = require ( "./UnsupportedFeatureWarning" ) ;
2013-02-14 00:00:07 +08:00
2014-06-16 21:18:49 +08:00
var NullFactory = require ( "./NullFactory" ) ;
2013-12-18 06:21:49 +08:00
function NodeStuffPlugin ( options ) {
2013-02-26 20:31:05 +08:00
this . options = options ;
2013-02-14 00:00:07 +08:00
}
module . exports = NodeStuffPlugin ;
NodeStuffPlugin . prototype . apply = function ( compiler ) {
2016-09-14 18:04:42 +08:00
var options = this . options ;
compiler . plugin ( "compilation" , function ( compilation , params ) {
2014-06-16 21:18:49 +08:00
compilation . dependencyFactories . set ( ConstDependency , new NullFactory ( ) ) ;
compilation . dependencyTemplates . set ( ConstDependency , new ConstDependency . Template ( ) ) ;
2015-07-13 06:20:09 +08:00
2016-09-14 18:04:42 +08:00
params . normalModuleFactory . plugin ( "parser" , function ( parser , parserOptions ) {
2015-07-13 06:20:09 +08:00
2016-09-14 18:04:42 +08:00
if ( parserOptions . node === false )
return ;
2015-07-13 06:20:09 +08:00
2016-09-14 18:04:42 +08:00
var localOptions = options ;
if ( parserOptions . node )
2016-09-30 22:24:05 +08:00
localOptions = objectAssign ( { } , localOptions , parserOptions . node ) ;
2016-09-14 18:04:42 +08:00
function ignore ( ) {
return true ;
}
function setConstant ( expressionName , value ) {
parser . plugin ( "expression " + expressionName , function ( ) {
this . state . current . addVariable ( expressionName , JSON . stringify ( value ) ) ;
return true ;
} ) ;
2015-11-21 01:10:39 +08:00
}
2016-09-14 18:04:42 +08:00
function setModuleConstant ( expressionName , fn ) {
parser . plugin ( "expression " + expressionName , function ( ) {
this . state . current . addVariable ( expressionName , JSON . stringify ( fn ( this . state . module ) ) ) ;
return true ;
} ) ;
}
var context = compiler . context ;
if ( localOptions . _ _filename === "mock" ) {
setConstant ( "__filename" , "/index.js" ) ;
} else if ( localOptions . _ _filename ) {
setModuleConstant ( "__filename" , function ( module ) {
return path . relative ( context , module . resource ) ;
} ) ;
}
parser . plugin ( "evaluate Identifier __filename" , function ( expr ) {
if ( ! this . state . module ) return ;
var res = new BasicEvaluatedExpression ( ) ;
var resource = this . state . module . resource ;
var i = resource . indexOf ( "?" ) ;
res . setString ( i < 0 ? resource : resource . substr ( 0 , i ) ) ;
res . setRange ( expr . range ) ;
return res ;
} ) ;
if ( localOptions . _ _dirname === "mock" ) {
setConstant ( "__dirname" , "/" ) ;
} else if ( localOptions . _ _dirname ) {
setModuleConstant ( "__dirname" , function ( module ) {
return path . relative ( context , module . context ) ;
} ) ;
}
parser . plugin ( "evaluate Identifier __dirname" , function ( expr ) {
if ( ! this . state . module ) return ;
var res = new BasicEvaluatedExpression ( ) ;
res . setString ( this . state . module . context ) ;
res . setRange ( expr . range ) ;
return res ;
} ) ;
parser . plugin ( "expression require.main" , function ( expr ) {
var dep = new ConstDependency ( "__webpack_require__.c[__webpack_require__.s]" , expr . range ) ;
dep . loc = expr . loc ;
this . state . current . addDependency ( dep ) ;
return true ;
} ) ;
parser . plugin ( "expression require.extensions" , function ( expr ) {
var dep = new ConstDependency ( "(void 0)" , expr . range ) ;
dep . loc = expr . loc ;
this . state . current . addDependency ( dep ) ;
if ( ! this . state . module ) return ;
this . state . module . warnings . push ( new UnsupportedFeatureWarning ( this . state . module , "require.extensions is not supported by webpack. Use a loader instead." ) ) ;
return true ;
} ) ;
parser . plugin ( "expression module.loaded" , function ( expr ) {
var dep = new ConstDependency ( "module.l" , expr . range ) ;
dep . loc = expr . loc ;
this . state . current . addDependency ( dep ) ;
return true ;
} ) ;
parser . plugin ( "expression module.id" , function ( expr ) {
var dep = new ConstDependency ( "module.i" , expr . range ) ;
dep . loc = expr . loc ;
this . state . current . addDependency ( dep ) ;
return true ;
} ) ;
parser . plugin ( "expression module.exports" , ignore ) ;
parser . plugin ( "evaluate Identifier module.hot" , function ( expr ) {
return new BasicEvaluatedExpression ( ) . setBoolean ( false ) . setRange ( expr . range ) ;
} ) ;
parser . plugin ( "expression module" , function ( ) {
var moduleJsPath = path . join ( _ _dirname , ".." , "buildin" , "module.js" ) ;
if ( this . state . module . context ) {
moduleJsPath = path . relative ( this . state . module . context , moduleJsPath ) ;
if ( ! /^[A-Z]:/i . test ( moduleJsPath ) ) {
moduleJsPath = "./" + moduleJsPath . replace ( /\\/g , "/" ) ;
}
}
return ModuleParserHelpers . addParsedVariable ( this , "module" , "require(" + JSON . stringify ( moduleJsPath ) + ")(module)" ) ;
} ) ;
} ) ;
2013-02-14 00:00:07 +08:00
} ) ;
2015-07-08 20:39:02 +08:00
} ;