xgqfrms™, xgqfrms® : xgqfrms's offical website of cnblogs! xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!

Node.js 中 CommonJS 模块 exports 与 module.exports 实现原理剖析 All In One

Node.js 中 CommonJS 模块 exports 与 module.exports 实现原理剖析 All In One


"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-04-26
 * @modified
 *
 * @description
 * @description
 * @difficulty Easy Medium Hard
 * @complexity O(n)
 * @time O(n)
 * @augments
 * @example
 * @link https://www.cnblogs.com/xgqfrms/p/16197024.html
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;


// Node.js 中 exports 与 module.exports 实现原理剖析

// console.log('\n module =', module);
// console.log('module keys =', Object.keys(module));

// module.exports = {}, 导出的是一个对象,不要使用 module.exports = func 覆盖 ❌

// 简化版
((window) => {
  // namespace
  if(!window.module) {
    window.module = {};
  }
  // var module = {
  window.module = {
    exports: {},
    id: '',
    path: '/Users/xgqfrms-mbp/GitHub/cjs/node.js',
    filename: '/Users/xgqfrms-mbp/GitHub/cjs/module.exports-exports-in-depth.js',
    loaded: false,
    children: [],
    paths: [
      '/Users/xgqfrms-mbp/Documents/GitHub/cjs/node_modules',
      '/Users/xgqfrms-mbp/Documents/GitHub/node_modules',
      '/Users/xgqfrms-mbp/Documents/node_modules',
      '/Users/xgqfrms-mbp/node_modules',
      '/Users/node_modules',
      '/node_modules'
    ],
  }
  // shorthand 引用类型
  var exports = module.exports;
  //
  const add = (a, b) => a + b;
  exports.add = add;
  // 等价于
  // module.exports.add = add;
  console.log('\nmodule.exports === exports', module.exports === exports);
  console.log('exports', exports);
  console.log('module.exports', module.exports);
  return window.module.exports;
})(window);

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-04-26
 * @modified
 *
 * @description
 * @description
 * @difficulty Easy Medium Hard
 * @complexity O(n)
 * @time O(n)
 * @augments
 * @example
 * @link https://www.cnblogs.com/xgqfrms/p/16197024.html
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;


((window) => {
  // namespace
  if(!window.module) {
    window.module = {};
  }
  var module = {
    exports: {},
    id: '',
    path: '/Users/xgqfrms-mbp/GitHub/cjs/node.js',
    filename: '/Users/xgqfrms-mbp/GitHub/cjs/module.exports-exports-in-depth.js',
    loaded: false,
    children: [],
    paths: [
      '/Users/xgqfrms-mbp/Documents/GitHub/cjs/node_modules',
      '/Users/xgqfrms-mbp/Documents/GitHub/node_modules',
      '/Users/xgqfrms-mbp/Documents/node_modules',
      '/Users/xgqfrms-mbp/node_modules',
      '/Users/node_modules',
      '/node_modules'
    ],
  }
  // shorthand 引用类型
  var exports = module.exports;
  const add = (a, b) => a + b;
  exports.add = add;
  // 等价于
  // module.exports.add = add;
  console.log('\nmodule.exports === exports', module.exports === exports);
  console.log('exports', exports);
  console.log('module.exports', module.exports);
  window.module = module;
  return module.exports;
})(window);

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-04-26
 * @modified
 *
 * @description
 * @description
 * @difficulty Easy Medium Hard
 * @complexity O(n)
 * @time O(n)
 * @augments
 * @example
 * @link https://www.cnblogs.com/xgqfrms/p/16197024.html
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;


((window) => {
  // namespace
  if(!window.module) {
    window.module = {
       exports: {},
    };
  }
  var module = {
    exports: {},
    id: '',
    path: '/Users/xgqfrms-mbp/GitHub/cjs/node.js',
    filename: '/Users/xgqfrms-mbp/GitHub/cjs/module.exports-exports-in-depth.js',
    loaded: false,
    children: [],
    paths: [
      '/Users/xgqfrms-mbp/Documents/GitHub/cjs/node_modules',
      '/Users/xgqfrms-mbp/Documents/GitHub/node_modules',
      '/Users/xgqfrms-mbp/Documents/node_modules',
      '/Users/xgqfrms-mbp/node_modules',
      '/Users/node_modules',
      '/node_modules'
    ],
  }
  // shorthand 引用类型
  var exports = module.exports;
  const add = (a, b) => a + b;
  exports.add = add;
  // 等价于
  // module.exports.add = add;
  console.log('\nmodule.exports === exports', module.exports === exports);
  console.log('exports', exports);
  console.log('module.exports', module.exports);
  window.module = module;
  return module.exports;
})(window);

demo

require & module.export

"use strict";

/**
 *
 * @author xgqfrms
 * @license MIT
 * @copyright xgqfrms
 * @created 2022-04-26
 * @modified
 *
 * @description
 * @description
 * @difficulty Easy Medium Hard
 * @complexity O(n)
 * @time O(n)
 * @augments
 * @example
 * @link https://www.cnblogs.com/xgqfrms/p/16197024.html
 * @solutions
 *
 * @best_solutions
 *
 */

const log = console.log;



((window, name, methods) => {
  // namespace
  if(!window.modules) {
    window.modules = {};
  }
  var module = {
    exports: {},
    id: '',
    path: '/Users/xgqfrms-mbp/GitHub/cjs/node.js',
    filename: '/Users/xgqfrms-mbp/GitHub/cjs/module.exports-exports-in-depth.js',
    loaded: false,
    children: [],
    paths: [
      '/Users/xgqfrms-mbp/Documents/GitHub/cjs/node_modules',
      '/Users/xgqfrms-mbp/Documents/GitHub/node_modules',
      '/Users/xgqfrms-mbp/Documents/node_modules',
      '/Users/xgqfrms-mbp/node_modules',
      '/Users/node_modules',
      '/node_modules'
    ],
  }
  // shorthand 引用类型
  var exports = module.exports;
  // exports methods
  // for(const method of methods) {
  //   exports[method] = method;
  // }
  const add = (a, b) => a + b;
  exports.add = add;
  // 等价于
  // module.exports.add = add;
  console.log('\nmodule.exports === exports', module.exports === exports);
  console.log('exports', exports);
  console.log('module.exports', module.exports);
  window.modules[name] = module
  // window.modules[name] = module.exports;
  return module.exports;
})(window, name = 'math', methods = []);

// wrapper `module.exports`

window.require = function(name = '') {
    return window.modules[name].exports;
}

// window.require = function(name = '') {
//     return window.modules[name];
// }

window.require('math');

refs



©xgqfrms 2012-2020

www.cnblogs.com/xgqfrms 发布文章使用:只允许注册用户才可以访问!

原创文章,版权所有©️xgqfrms, 禁止转载 🈲️,侵权必究⚠️!


posted @ 2022-04-26 22:42  xgqfrms  阅读(134)  评论(1编辑  收藏  举报