[webpack]Parser 解析

這裏終於把 提取到cube和square的源頭給找到了:ide

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:ui

clipboard.png

神祕的 parser:this

// Syntax: https://developer.mozilla.org/en/SpiderMonkey/Parser_API

const acorn = require("acorn");

解析AST:spa

ast = Parser.parse(source, {
                sourceType: this.sourceType,
                onComment: comments
            });

Parser.parse以下:code

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進來的:blog

if (statement.source) {
            source = statement.source.value;
            this.hooks.exportImport.call(statement, source);
        } else {
            this.hooks.export.call(statement);
        }
相關文章
相關標籤/搜索