这里终于把 提取到cube和square的源头给找到了:
const result = this.parser.parse(
this._ast || this._source.source(),
{
current: this,
module: this,
compilation: compilation,
options: options
},
(err, result) => {
if (err) {
handleParseError(err);
} else {
handleParseResult(result);
}
}
);
if (result !== undefined) {
// parse is sync
handleParseResult(result);
}
this.parser.parse:
神秘的 parser:
// Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API
const acorn = require("acorn");
解析AST:
ast = Parser.parse(source, {
sourceType: this.sourceType,
onComment: comments
});
Parser.parse如下:
static parse(code, options) {
const type = options ? options.sourceType : "module";
const parserOptions = Object.assign(
Object.create(null),
defaultParserOptions,
options
);
if (type === "auto") {
parserOptions.sourceType = "module";
} else if (parserOptions.sourceType === "script") {
parserOptions.allowReturnOutsideFunction = true;
}
let ast;
let error;
let threw = false;
try {
ast = acornParser.parse(code, parserOptions);
} catch (e) {
error = e;
threw = true;
}
if (threw && type === "auto") {
parserOptions.sourceType = "script";
parserOptions.allowReturnOutsideFunction = true;
if (Array.isArray(parserOptions.onComment)) {
parserOptions.onComment.length = 0;
}
try {
ast = acornParser.parse(code, parserOptions);
threw = false;
} catch (e) {
threw = true;
}
}
if (threw) {
throw error;
}
return ast;
}
导出的变量还可以时其他地方import进来的:
if (statement.source) {
source = statement.source.value;
this.hooks.exportImport.call(statement, source);
} else {
this.hooks.export.call(statement);
}
**粗体** _斜体_ [链接](http://example.com) `代码` - 列表 > 引用
。你还可以使用@
来通知其他用户。