rspack: hexoid crashes if imported using commonjs

System Info

System: OS: macOS 13.3.1 CPU: (10) arm64 Apple M1 Pro Memory: 404.19 MB / 32.00 GB Shell: 5.9 - /bin/zsh Binaries: Node: 18.16.0 - ~/.nvm/versions/node/v18.16.0/bin/node npm: 8.19.2 - ~/node_modules/.bin/npm Browsers: Chrome: 113.0.5672.126 Edge: 113.0.1774.35 Safari: 16.4 npmPackages: @rspack/cli: 0.1.11 => 0.1.11 @rspack/core: 0.1.11 => 0.1.11 @rspack/dev-server: ~0.1.0 => 0.1.11 @rspack/plugin-minify: ~0.1.0 => 0.1.11

Details

https://www.npmjs.com/package/hexoid is delivered as:

CommonJS: dist/index.js ES Module: dist/index.mjs UMD: dist/index.min.js

When it is consumed through CommonJS, it crashes at runtime. When it is consumed through EMS, it works fine

Compilation output

ESM

(function() {
var __webpack_modules__ = {
"56": function (module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "default", {
    enumerable: true,
    get: function() {
        return _default;
    }
});
var IDX = 256, HEX = [];
while(IDX--)HEX[IDX] = (IDX + 256).toString(16).substring(1);
function _default(len) {
    len = len || 16;
    var str = '', num = 0;
    return function() {
        if (!str || num === 256) {
            str = '';
            num = (1 + len) / 2 | 0;
            while(num--)str += HEX[256 * Math.random() | 0];
            str = str.substring(num = 0, len - 2);
        }
        return str + HEX[num++];
    };
}
},
"821": function (module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
var _56 = __webpack_require__.ir(__webpack_require__("56"));
const toID = (0, _56.default)();
toID();
},

}

CommonJS

var __webpack_modules__ = {
"56": function (module, exports, __webpack_require__) {
"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
Object.defineProperty(exports, "default", {
    enumerable: true,
    get: function() {
        return _default;
    }
});
var IDX = 256, HEX = [];
while(IDX--)HEX[IDX] = (IDX + 256).toString(16).substring(1);
function _default(len) {
    len = len || 16;
    var str = '', num = 0;
    return function() {
        if (!str || num === 256) {
            str = '';
            num = (1 + len) / 2 | 0;
            while(num--)str += HEX[256 * Math.random() | 0];
            str = str.substring(num = 0, len - 2);
        }
        return str + HEX[num++];
    };
}
},
"821": function (module, exports, __webpack_require__) {
const hexoid = __webpack_require__("56");
const toID = hexoid();
toID();
},

}

Reproduce link

No response

Reproduce Steps

This will crash at runtime:

const hexoid = require('hexoid');

const toID = hexoid();
toID(); //=> '52032fedb951da00'

This works

import hexoid from 'hexoid';

const toID = hexoid();
toID(); //=> '52032fedb951da00'

About this issue

  • Original URL
  • State: closed
  • Created a year ago
  • Comments: 16 (9 by maintainers)

Most upvoted comments

@alexander-akait @hardfist Thank you for the follow-up πŸ™ŒπŸ» Next time I will create the reproduction using the rspack template πŸ‘πŸ»

I will create an issue on the hexoid repo to add the exports in the package.json on npm.

since it’s not a bug of rspack, gonna close the issue

https://github.com/web-infra-dev/rspack-repro maybe you can provide demo using rspack repro template, so we can compare the result of rspack and webpack