2016-12-03 18:36:10 +08:00
/ *
MIT License http : //www.opensource.org/licenses/mit-license.php
Author Tobias Koppers @ sokra
* /
2017-01-12 01:58:05 +08:00
"use strict" ;
2016-12-03 18:36:10 +08:00
2017-05-05 00:37:25 +08:00
const ImportEagerContextDependency = require ( "./ImportEagerContextDependency" ) ;
2017-07-26 20:49:37 +08:00
const ImportWeakDependency = require ( "./ImportWeakDependency" ) ;
const ImportWeakContextDependency = require ( "./ImportWeakContextDependency" ) ;
2017-05-05 00:37:25 +08:00
const ImportLazyOnceContextDependency = require ( "./ImportLazyOnceContextDependency" ) ;
const ImportLazyContextDependency = require ( "./ImportLazyContextDependency" ) ;
2017-01-12 01:58:05 +08:00
const ImportDependenciesBlock = require ( "./ImportDependenciesBlock" ) ;
2017-05-05 00:37:25 +08:00
const ImportEagerDependency = require ( "./ImportEagerDependency" ) ;
2017-01-12 01:58:05 +08:00
const ContextDependencyHelpers = require ( "./ContextDependencyHelpers" ) ;
2017-05-05 00:37:25 +08:00
const UnsupportedFeatureWarning = require ( "../UnsupportedFeatureWarning" ) ;
2016-12-03 18:36:10 +08:00
2017-01-12 01:58:05 +08:00
class ImportParserPlugin {
constructor ( options ) {
this . options = options ;
}
2016-12-03 18:36:10 +08:00
2017-01-12 01:58:05 +08:00
apply ( parser ) {
const options = this . options ;
2017-03-26 17:07:52 +08:00
2017-01-12 01:58:05 +08:00
parser . plugin ( [ "call System.import" , "import-call" ] , ( expr ) => {
if ( expr . arguments . length !== 1 )
throw new Error ( "Incorrect number of arguments provided to 'import(module: string) -> Promise'." ) ;
2017-03-26 17:07:52 +08:00
2017-01-12 01:58:05 +08:00
const param = parser . evaluateExpression ( expr . arguments [ 0 ] ) ;
2017-04-10 17:35:32 +08:00
2017-03-26 17:07:52 +08:00
let chunkName = null ;
2017-05-05 00:37:25 +08:00
let mode = "lazy" ;
2017-10-12 05:30:54 +08:00
let filter = null ;
2017-10-13 05:35:23 +08:00
let exclude = null ;
2017-03-26 17:07:52 +08:00
2017-04-10 17:35:32 +08:00
const importOptions = parser . getCommentOptions ( expr . range ) ;
if ( importOptions ) {
if ( typeof importOptions . webpackChunkName !== "undefined" ) {
if ( typeof importOptions . webpackChunkName !== "string" )
2017-05-05 00:37:25 +08:00
parser . state . module . warnings . push ( new UnsupportedFeatureWarning ( parser . state . module , ` \` webpackChunkName \` expected a string, but received: ${ importOptions . webpackChunkName } . ` ) ) ;
else
chunkName = importOptions . webpackChunkName ;
}
if ( typeof importOptions . webpackMode !== "undefined" ) {
if ( typeof importOptions . webpackMode !== "string" )
parser . state . module . warnings . push ( new UnsupportedFeatureWarning ( parser . state . module , ` \` webpackMode \` expected a string, but received: ${ importOptions . webpackMode } . ` ) ) ;
else
mode = importOptions . webpackMode ;
2017-03-26 17:07:52 +08:00
}
2017-10-12 05:30:54 +08:00
if ( typeof importOptions . webpackFilter !== "undefined" ) {
if ( typeof importOptions . webpackFilter !== "string" ) {
parser . state . module . warnings . push ( new UnsupportedFeatureWarning ( parser . state . module , ` \` webpackFilter \` expected a string, but received: ${ importOptions . webpackFilter } . ` ) ) ;
} else {
filter = new RegExp ( importOptions . webpackFilter ) ;
}
}
2017-10-13 05:35:23 +08:00
if ( typeof importOptions . webpackExclude !== "undefined" ) {
if ( typeof importOptions . webpackExclude !== "string" ) {
parser . state . module . warnings . push ( new UnsupportedFeatureWarning ( parser . state . module , ` \` webpackExclude \` expected a string, but received: ${ importOptions . webpackExclude } . ` ) ) ;
} else {
exclude = new RegExp ( importOptions . webpackExclude ) ;
}
}
2017-03-26 17:07:52 +08:00
}
2017-01-12 01:58:05 +08:00
if ( param . isString ( ) ) {
2017-07-26 20:49:37 +08:00
if ( mode !== "lazy" && mode !== "eager" && mode !== "weak" ) {
parser . state . module . warnings . push ( new UnsupportedFeatureWarning ( parser . state . module , ` \` webpackMode \` expected 'lazy', 'eager' or 'weak', but received: ${ mode } . ` ) ) ;
2017-05-05 00:37:25 +08:00
}
if ( mode === "eager" ) {
const dep = new ImportEagerDependency ( param . string , expr . range ) ;
parser . state . current . addDependency ( dep ) ;
2017-07-26 20:49:37 +08:00
} else if ( mode === "weak" ) {
const dep = new ImportWeakDependency ( param . string , expr . range ) ;
parser . state . current . addDependency ( dep ) ;
2017-05-05 00:37:25 +08:00
} else {
2017-07-26 20:49:37 +08:00
const depBlock = new ImportDependenciesBlock ( param . string , expr . range , chunkName , parser . state . module , expr . loc ) ;
2017-05-05 00:37:25 +08:00
parser . state . current . addBlock ( depBlock ) ;
}
2017-01-12 01:58:05 +08:00
return true ;
} else {
2017-07-26 20:49:37 +08:00
if ( mode !== "lazy" && mode !== "lazy-once" && mode !== "eager" && mode !== "weak" ) {
parser . state . module . warnings . push ( new UnsupportedFeatureWarning ( parser . state . module , ` \` webpackMode \` expected 'lazy', 'lazy-once', 'eager' or 'weak', but received: ${ mode } . ` ) ) ;
2017-05-05 00:37:25 +08:00
}
let Dep = ImportLazyContextDependency ;
if ( mode === "eager" ) {
Dep = ImportEagerContextDependency ;
2017-07-26 20:49:37 +08:00
} else if ( mode === "weak" ) {
Dep = ImportWeakContextDependency ;
2017-05-05 00:37:25 +08:00
} else if ( mode === "lazy-once" ) {
Dep = ImportLazyOnceContextDependency ;
}
2017-10-13 05:35:23 +08:00
const dep = ContextDependencyHelpers . create ( Dep , expr . range , param , expr , options , chunkName ) ;
2017-01-12 01:58:05 +08:00
if ( ! dep ) return ;
dep . loc = expr . loc ;
dep . optional = ! ! parser . scope . inTry ;
2017-10-13 05:35:23 +08:00
dep . filter = filter ;
dep . exclude = exclude ;
2017-01-12 01:58:05 +08:00
parser . state . current . addDependency ( dep ) ;
return true ;
}
} ) ;
}
}
module . exports = ImportParserPlugin ;