2013-02-14 00:00:07 +08:00
/ *
MIT License http : //www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @ sokra
* /
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 ) {
2015-04-24 05:55:50 +08:00
compiler . plugin ( "compilation" , function ( compilation ) {
2014-06-16 21:18:49 +08:00
compilation . dependencyFactories . set ( ConstDependency , new NullFactory ( ) ) ;
compilation . dependencyTemplates . set ( ConstDependency , new ConstDependency . Template ( ) ) ;
} ) ;
2013-02-14 00:00:07 +08:00
function ignore ( ) { return true ; }
2014-07-04 18:13:44 +08:00
function setConstant ( expressionName , value ) {
2015-04-24 05:55:50 +08:00
compiler . parser . plugin ( "expression " + expressionName , function ( ) {
2014-07-04 18:13:44 +08:00
this . state . current . addVariable ( expressionName , JSON . stringify ( value ) ) ;
2013-02-26 20:31:05 +08:00
return true ;
2014-07-04 18:13:44 +08:00
} ) ;
2014-06-25 00:53:32 +08:00
}
2014-07-04 18:13:44 +08:00
function setModuleConstant ( expressionName , fn ) {
2015-04-24 05:55:50 +08:00
compiler . parser . plugin ( "expression " + expressionName , function ( ) {
2014-07-04 18:13:44 +08:00
this . state . current . addVariable ( expressionName , JSON . stringify ( fn ( this . state . module ) ) ) ;
2013-02-26 20:31:05 +08:00
return true ;
2014-07-04 18:13:44 +08:00
} ) ;
2014-06-25 00:53:32 +08:00
}
var context = compiler . context ;
if ( this . options . _ _filename === "mock" ) {
setConstant ( "__filename" , "/index.js" ) ;
} else if ( this . options . _ _filename ) {
setModuleConstant ( "__filename" , function ( module ) {
return path . relative ( context , module . resource ) ;
2013-02-26 20:31:05 +08:00
} ) ;
}
2013-02-17 05:23:22 +08:00
compiler . parser . plugin ( "evaluate Identifier __filename" , function ( expr ) {
if ( ! this . state . module ) return ;
var res = new BasicEvaluatedExpression ( ) ;
res . setString ( this . state . module . splitQuery ( this . state . module . resource ) [ 0 ] ) ;
res . setRange ( expr . range ) ;
return res ;
} ) ;
2014-06-25 00:53:32 +08:00
if ( this . options . _ _dirname === "mock" ) {
setConstant ( "__dirname" , "/" ) ;
2013-02-26 20:31:05 +08:00
} else if ( this . options . _ _dirname ) {
2014-06-25 00:53:32 +08:00
setModuleConstant ( "__dirname" , function ( module ) {
return path . relative ( context , module . context ) ;
2013-02-26 20:31:05 +08:00
} ) ;
}
2013-02-17 05:23:22 +08:00
compiler . 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 ;
} ) ;
2013-02-14 00:00:07 +08:00
compiler . parser . plugin ( "expression require.main" , function ( expr ) {
2014-03-03 21:56:17 +08:00
var dep = new ConstDependency ( "__webpack_require__.c[0]" , expr . range ) ;
2013-02-14 00:00:07 +08:00
dep . loc = expr . loc ;
this . state . current . addDependency ( dep ) ;
return true ;
} ) ;
2013-10-11 16:42:25 +08:00
compiler . parser . plugin ( "expression require.extensions" , function ( expr ) {
2014-03-20 06:27:18 +08:00
var dep = new ConstDependency ( "(void 0)" , expr . range ) ;
dep . loc = expr . loc ;
this . state . current . addDependency ( dep ) ;
2013-10-11 16:42:25 +08:00
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 ;
} ) ;
2013-02-14 00:00:07 +08:00
compiler . parser . plugin ( "expression module.exports" , ignore ) ;
compiler . parser . plugin ( "expression module.loaded" , ignore ) ;
compiler . parser . plugin ( "expression module.id" , ignore ) ;
2013-12-18 06:21:49 +08:00
compiler . parser . plugin ( "evaluate Identifier module.hot" , function ( expr ) {
return new BasicEvaluatedExpression ( ) . setBoolean ( false ) . setRange ( expr . range ) ;
} ) ;
2015-04-24 05:55:50 +08:00
compiler . parser . plugin ( "expression module" , function ( ) {
2013-02-14 00:00:07 +08:00
return ModuleParserHelpers . addParsedVariable ( this , "module" , "require(" + JSON . stringify ( path . join ( _ _dirname , ".." , "buildin" , "module.js" ) ) + ")(module)" ) ;
} ) ;
} ;