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-01-12 01:58:05 +08:00
|
|
|
const ImportContextDependency = require("./ImportContextDependency");
|
|
|
|
const ImportDependenciesBlock = require("./ImportDependenciesBlock");
|
|
|
|
const ContextDependencyHelpers = require("./ContextDependencyHelpers");
|
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-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")
|
|
|
|
throw new Error(`\`webpackChunkName\` expected a string, but received: ${importOptions.webpackChunkName}.`);
|
|
|
|
chunkName = importOptions.webpackChunkName;
|
2017-03-26 17:07:52 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-01-12 01:58:05 +08:00
|
|
|
if(param.isString()) {
|
2017-03-26 17:07:52 +08:00
|
|
|
const depBlock = new ImportDependenciesBlock(param.string, expr.range, chunkName, parser.state.module, expr.loc);
|
2017-01-12 01:58:05 +08:00
|
|
|
parser.state.current.addBlock(depBlock);
|
|
|
|
return true;
|
|
|
|
} else {
|
2017-03-26 17:07:52 +08:00
|
|
|
const dep = ContextDependencyHelpers.create(ImportContextDependency, 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;
|
|
|
|
parser.state.current.addDependency(dep);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
module.exports = ImportParserPlugin;
|