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 interop] Behavior of requiring ESM from CJS is different with @rollup/plugin-commonjs #1333

Closed
haoqunjiang opened this issue May 31, 2021 · 1 comment · Fixed by #1338

Comments

@haoqunjiang
Copy link

haoqunjiang commented May 31, 2021

Requiring ESM from CJS + TypeScript __importDefault helper results in a nested structure, all the exports of the ESM file are wrapped in the .default property of the required variable.

See https://github.com/sodatea/cjs-require-esm-tests.git for a minimal reproduction.

Rollup fixes this issue with rollup/plugins#552

It is also the cause of #1323

@haoqunjiang haoqunjiang changed the title [CJS-ESM interop] Behavior of requiring ESM from CJS is different from @rollup/plugin-commonjs [CJS-ESM interop] Behavior of requiring ESM from CJS is confusing, and different with @rollup/plugin-commonjs May 31, 2021
@haoqunjiang haoqunjiang changed the title [CJS-ESM interop] Behavior of requiring ESM from CJS is confusing, and different with @rollup/plugin-commonjs [CJS-ESM interop] Behavior of requiring ESM from CJS is different with @rollup/plugin-commonjs May 31, 2021
@zhangyuang
Copy link

supplement something, for above repo, esbuild@0.9.7 will output different code from esbuild@0.12.5

// source code
var __importDefault = (this && this.__importDefault) || function (mod) {
  console.log('mod', mod.__esModule)
  return (mod && mod.__esModule) ? mod : { "default": mod };
};
var dep = __importDefault(require("./dep.mjs"));

console.log('dep', dep)
console.log('dep.default', dep.default)
console.log('dep.name', dep.name)
console.log()
// esbuild@0.9.7
var __defProp = Object.defineProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", {value: true});
var __commonJS = (callback, module) => () => {
  if (!module) {
    module = {exports: {}};
    callback(module.exports, module);
  }
  return module.exports;
};
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, {get: all[name], enumerable: true});
};

// dep.mjs
var require_dep = __commonJS((exports) => {
  __markAsModule(exports);
  __export(exports, {
    default: () => dep_default,
    name: () => name
  });
  var name = "dep";
  var dep_default = name;
});

// main.cjs
var require_main = __commonJS((exports) => {
  var __importDefault = exports && exports.__importDefault || function(mod) {
    console.log("mod", mod.__esModule);
    return mod && mod.__esModule ? mod : {default: mod};
  };
  var dep = __importDefault(require_dep());
  console.log("dep", dep);
  console.log("dep.default", dep.default);
  console.log("dep.name", dep.name);
  console.log();
});
export default require_main();
// esbuild@0.12.5
var __defProp = Object.defineProperty;
var __esm = (fn, res) => function __init() {
  return fn && (res = (0, fn[Object.keys(fn)[0]])(fn = 0)), res;
};
var __commonJS = (cb, mod) => function __require() {
  return mod || (0, cb[Object.keys(cb)[0]])((mod = { exports: {} }).exports, mod), mod.exports;
};
var __export = (target, all) => {
  for (var name2 in all)
    __defProp(target, name2, { get: all[name2], enumerable: true });
};

// dep.mjs
var dep_exports = {};
__export(dep_exports, {
  default: () => dep_default,
  name: () => name
});
var name, dep_default;
var init_dep = __esm({
  "dep.mjs"() {
    name = "dep";
    dep_default = name;
  }
});

// main.cjs
var require_main = __commonJS({
  "main.cjs"(exports) {
    var __importDefault = exports && exports.__importDefault || function(mod) {
      console.log("mod", mod.__esModule);
      return mod && mod.__esModule ? mod : { "default": mod };
    };
    var dep = __importDefault((init_dep(), dep_exports));
    console.log("dep", dep);
    console.log("dep.default", dep.default);
    console.log("dep.name", dep.name);
    console.log();
  }
});
export default require_main();

lbwa added a commit to lbwa/esbuild that referenced this issue Jun 1, 2021
fix evanw#1333 evanw#1323

Signed-off-by: Liu Bowen <mr_lbw@outlook.com>
lbwa added a commit to lbwa/esbuild that referenced this issue Jun 1, 2021
fix evanw#1333 evanw#1323

Signed-off-by: Liu Bowen <mr_lbw@outlook.com>
lbwa added a commit to lbwa/esbuild that referenced this issue Jun 1, 2021
fix evanw#1333 evanw#1323

Signed-off-by: Liu Bowen <mr_lbw@outlook.com>
lbwa added a commit to lbwa/esbuild that referenced this issue Jun 1, 2021
fix evanw#1333 evanw#1323

Signed-off-by: Liu Bowen <mr_lbw@outlook.com>
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

Successfully merging a pull request may close this issue.

2 participants