esbuild icon indicating copy to clipboard operation
esbuild copied to clipboard

Specific module not imported/wrapped in _commonJS definition

Open pachomski opened this issue 3 years ago • 1 comments

I'm currently attempting to take a codebase written in cjs with date-fns as a dependency, that needs to be tree shaken, and convert it to esm for the browser.

All of the code bundles and is transformed into _commonJS() wrapped modules except for the getTimezoneOffsetInMilliseconds/index.js module defined as _index7.

// node_modules/date-fns/_lib/protectedTokens/index.js
var require_protectedTokens = __commonJS({
  "node_modules/date-fns/_lib/protectedTokens/index.js"(exports) {
    "use strict";
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.isProtectedDayOfYearToken = isProtectedDayOfYearToken;
    exports.isProtectedWeekYearToken = isProtectedWeekYearToken;
    exports.throwProtectedError = throwProtectedError;
    var protectedDayOfYearTokens = ["D", "DD"];
    var protectedWeekYearTokens = ["YY", "YYYY"];
    function isProtectedDayOfYearToken(token) {
      return protectedDayOfYearTokens.indexOf(token) !== -1;
    }
    function isProtectedWeekYearToken(token) {
      return protectedWeekYearTokens.indexOf(token) !== -1;
    }
    function throwProtectedError(token, format, input) {
      if (token === "YYYY") {
        throw new RangeError("Use `yyyy` instead of `YYYY` (in `".concat(format, "`) for formatting years to the input `").concat(input, "`; see: https://git.io/fxCyr"));
      } else if (token === "YY") {
        throw new RangeError("Use `yy` instead of `YY` (in `".concat(format, "`) for formatting years to the input `").concat(input, "`; see: https://git.io/fxCyr"));
      } else if (token === "D") {
        throw new RangeError("Use `d` instead of `D` (in `".concat(format, "`) for formatting days of the month to the input `").concat(input, "`; see: https://git.io/fxCyr"));
      } else if (token === "DD") {
        throw new RangeError("Use `dd` instead of `DD` (in `".concat(format, "`) for formatting days of the month to the input `").concat(input, "`; see: https://git.io/fxCyr"));
      }
    }
  }
});

// node_modules/date-fns/format/index.js
var require_format = __commonJS({
  "node_modules/date-fns/format/index.js"(exports, module) {
    "use strict";
    Object.defineProperty(exports, "__esModule", {
      value: true
    });
    exports.default = format;
    var _index = _interopRequireDefault(require_isValid());
    var _index2 = _interopRequireDefault(require_en_US());
    var _index3 = _interopRequireDefault(require_subMilliseconds());
    var _index4 = _interopRequireDefault(require_toDate());
    var _index5 = _interopRequireDefault(require_formatters());
    var _index6 = _interopRequireDefault(require_longFormatters());
    var _index7 = _interopRequireDefault(__require("../_lib/getTimezoneOffsetInMilliseconds/index.js"));
    var _index8 = require_protectedTokens();
    var _index9 = _interopRequireDefault(require_toInteger());
    var _index10 = _interopRequireDefault(require_requiredArgs());
    function _interopRequireDefault(obj) {
      return obj && obj.__esModule ? obj : { default: obj };
      

Is there a specific reason why this one module would be treated differently or am I missing a configuration detail to handle libraries like date-fns?

Here are the esbuild.build configuration settings used:

await esbuild.build({
    bundle: true,
    entryPoints: opts.entryPoints,
    entryNames: opts.generatedFileNamePattern,
    format: "esm",
    metafile: true,
    splitting: true,
    minify: false,
    outdir: targets.publicJS,
    // platform: opts.platform || "browser",
    // plugins: opts.plugins,
    mainFields: ["browser", "main"], // in order of precedence, "browser" having highest.
    platform: "browser",
    sourcemap: "linked",
    sourceRoot: targets.publicJS,
    treeShaking: true,

pachomski avatar Jul 18 '22 20:07 pachomski

You can use logLevel: 'verbose' to tell esbuild to print out all details about path resolution. That might help you figure out why getTimezoneOffsetInMilliseconds/index.js is not being included in the bundle.

Also note that by excluding "module" from mainFields, your are likely opting out of ESM for certain packages like date-fns. This is problematic for tree-shaking because esbuild only does tree-shaking with ESM.

evanw avatar Jul 18 '22 20:07 evanw

Closing due to lack of a reply.

evanw avatar Dec 04 '22 04:12 evanw