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

should non exports namespace object keep __esModule flag? #2126

Closed
hardfist opened this issue Mar 25, 2022 · 2 comments
Closed

should non exports namespace object keep __esModule flag? #2126

hardfist opened this issue Mar 25, 2022 · 2 comments

Comments

@hardfist
Copy link
Contributor

It seems that esbuild only add __esModule for export namespace object and not for internal namespace object.

input.ts

import * as answer from './answer'
globalThis.obj = {
  answer
}
console.log('ojb:', globalThis.obj)
export {
  answer
}

build.sh

 esbuild src/index.js --bundle --format=cjs --outfile=bundle.js

bundle.js

var __defProp = Object.defineProperty;
var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
var __getOwnPropNames = Object.getOwnPropertyNames;
var __hasOwnProp = Object.prototype.hasOwnProperty;
var __markAsModule = (target) => __defProp(target, "__esModule", { value: true });
var __export = (target, all) => {
  for (var name in all)
    __defProp(target, name, { get: all[name], enumerable: true });
};
var __reExport = (target, module2, copyDefault, desc) => {
  if (module2 && typeof module2 === "object" || typeof module2 === "function") {
    for (let key of __getOwnPropNames(module2))
      if (!__hasOwnProp.call(target, key) && (copyDefault || key !== "default"))
        __defProp(target, key, { get: () => module2[key], enumerable: !(desc = __getOwnPropDesc(module2, key)) || desc.enumerable });
  }
  return target;
};
var __toCommonJS = /* @__PURE__ */ ((cache) => {
  return (module2, temp) => {
    return cache && cache.get(module2) || (temp = __reExport(__markAsModule({}), module2, 1), cache && cache.set(module2, temp), temp);
  };
})(typeof WeakMap !== "undefined" ? /* @__PURE__ */ new WeakMap() : 0);

// src/index.js
var src_exports = {};
__export(src_exports, {
  answer: () => answer_exports
});

// src/answer.js
var answer_exports = {};
__export(answer_exports, {
  default: () => answer_default
});
var answer = 42;
var answer_default = answer;

// src/index.js
globalThis.obj = {
  answer: answer_exports
};
console.log("ojb:", globalThis.obj);
module.exports = __toCommonJS(src_exports);

The globalThis.obj.answer doesn't contains __ESModule flags, which cause problem when globalThis.obj.answer is used in another module loader(like amd).

but webpack generated __esModule flags for both internal and exports namespace object.

@evanw
Copy link
Owner

evanw commented Mar 25, 2022

This is very deliberate. People want to be able to use __esModule as an ESM export name via export { __esModule }: #1591. So esbuild no longer introduces __esModule into ESM exports at all. If you want __esModule you should import the module with require() instead of import. Only CommonJS-style imports get __esModule.

@hardfist
Copy link
Contributor Author

Oh, that make sense,so it's not an good idea for runtime to rely on this feature

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

2 participants