1

这里终于把 提取到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);
                }

clipboard.png

this.parser.parse:

clipboard.png

神秘的 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);
        }

webpack_devsave
16 声望0 粉丝

a