zoukankan      html  css  js  c++  java
  • webpack-Bundler源码编写(生成代码)

    接下来我们要生成真正可以在浏览器中运行的代码:

    const makeDependenciesGraph=entry=>{
        ...
    }
    const generateCode=entry=>{
        const graph=JSON.stringify(makeDependenciesGraph(entry));
        return `
            (function(graph)){}(${graph});
        `;
    }
    
    const code=generateCode('./src/index.js');
    console.log(code);

    输出:

    (function(graph)){}({"./src/index.js":{"dependencies":{"./message.js":"src/message.js"},"code":""use strict";
    
    var _message = _interopRequireDefault(require("./message.js"));
    
    function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
    
    console.log(_message["default"]);"},"src/message.js":{"dependencies":{"./word.js":"src/word.js"},"code":""use strict";
    
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports["default"] = void 0;
    
    var _word = require("./word.js");
    
    var message = "say ".concat(_word.word);
    var _default = message;
    exports["default"] = _default;"},"src/word.js":{"dependencies":{},"code":""use strict";
    
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.word = void 0;
    var word = 'hello';
    exports.word = word;"}});

    以上代码中含有require、exports等浏览器不支持的语法,还不能正确执行,所以我们还需要构造require函数和exports对象:

    const generateCode=entry=>{
        const graph=JSON.stringify(makeDependenciesGraph(entry));
        return `
            (function(graph){
                function require(module){
                    function localRequire(relativePath){
                        return require(graph[module].dependencies[relativePath])
                    }
                    var exports={};
                    (function(require,exports,code){
                        eval(code);
                    })(localRequire,exports,graph[module].code);
                    return exports
                };
                require('${entry}');
            })(${graph});
        `;
    }
    
    const code=generateCode('./src/index.js');
    console.log(code);

    输出:

    (function(graph){
        function require(module){
            function localRequire(relativePath){
                return require(graph[module].dependencies[relativePath])
            }
            var exports={};
            (function(require,exports,code){
                eval(code);
            })(localRequire,exports,graph[module].code);
            return exports
        };
        require('./src/index.js');
    })({"./src/index.js":{"dependencies":{"./message.js":"src/message.js"},"code":""use strict";
    
    var _message = _interopRequireDefault(require("./message.js"));
    
    function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { "default": obj }; }
    
    console.log(_message["default"]);"},"src/message.js":{"dependencies":{"./word.js":"src/word.js"},"code":""use strict";
    
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports["default"] = void 0;
    
    var _word = require("./word.js");
    
    var message = "say ".concat(_word.word);
    var _default = message;
    exports["default"] = _default;"},"src/word.js":{"dependencies":{},"code":""use strict";
    
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.word = void 0;
    var word = 'hello';
    exports.word = word;"}});

    将以上代码复制到控制台,能正确打印出'say hello',至此,我们已经完成bundler打包代码。

  • 相关阅读:
    线程中消费者生产者的实例代码(synchronized关键字)
    ThreadLocal管理Connection
    ThreadLocal的练习代码
    JDK中线程组ThreadGroup
    创建线程的三种方式
    ES练习代码
    window下在同一台机器上安装多个版本jdk,修改环境变量不生效问题处理办法
    ES配置文件中文版
    springmvc工作原理
    IE不兼容document.getElementsByClassName
  • 原文地址:https://www.cnblogs.com/jingouli/p/12336384.html
Copyright © 2011-2022 走看看