Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cjs模拟esm #20

Open
Rain120 opened this issue Feb 12, 2022 · 1 comment
Open

cjs模拟esm #20

Rain120 opened this issue Feb 12, 2022 · 1 comment

Comments

@Rain120
Copy link
Owner

Rain120 commented Feb 12, 2022

思考点

  • exports = module.exports
    • exports.name => export const name 具名
    • exports {} => export default

伪代码

var $esm = {
  exports: {}
}

;(function (module, exports) {
  module.exports = () => {};
  exports.name = 'esm';
}($esm, $esm.exports));

var esm = $esm.exports;

export { esm as default };

https://q.shanyue.tech/engineering/753.html

实现: https://github.com/wessberg/cjstoesm

Rollup 插件

@rollup/plugin-commonjs

babel-plugin-transform-commonjs

CDN

https://cdn.skypack.dev/

https://jspm.org/

@Rain120
Copy link
Owner Author

Rain120 commented Feb 21, 2022

var LIB =
  (function (modules) {
    var installedModules = {};

    function __webpack_require__ (moduleId) {

      if (installedModules[moduleId]) {
        return installedModules[moduleId].exports;
      }

      var module = installedModules[moduleId] = {
        i: moduleId,
        l: false,
        exports: {}
      };

      modules[moduleId].call(module.exports, module, module.exports, __webpack_require__);

      module.l = true;

      return module.exports;
    }

    return __webpack_require__("./test.js");
  })
    ({

      "./test.js":
        (function (module, exports) {
          eval("console.log('test');\n\n//# sourceURL=webpack://LIB/./test.js?");
        })
    });
export default LIB;
export { LIB };

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant