# webpack

By [tendersun](https://paragraph.com/@tendersun) · 2022-12-17

---

### Commonjs实现原理

    let modules = {
      "./name.js": (module) => {
        module.exports = 'name';
      }
    }
    let cache = {};  // 内存中的缓存对象
    function require(modulePath) {
      let moduleCache = cache(modulePath);
      // 如果之前加载过该模板，直接返回缓存
      if (moduleCache) {
        return moduleCache;
      }
      // moduleCache和module指向同一个引用
      let module = (moduleCache = {
        exports: {}
      })
      // 运行模块中的代码，给module赋值
      modulesmodulePath;
      return module.exports;
    }
    

[https://juejin.cn/post/7147365025047379981/#heading-6](https://juejin.cn/post/7147365025047379981/#heading-6)

---

*Originally published on [tendersun](https://paragraph.com/@tendersun/webpack)*
