vercel / ncc

Compile a Node.js project into a single file. Supports TypeScript, binary addons, dynamic requires.
https://npmjs.com/@vercel/ncc
MIT License
9.27k stars 291 forks source link

bwip-js runtime require failure due to type mismatch #844

Open ZachGoldberg opened 2 years ago

ZachGoldberg commented 2 years ago

Including bwip-js in our project generates the following for bwip-js/node-fonts.js (https://github.com/metafloor/bwip-js/blob/v1.7.1/node-fonts.js#L44)

function __ncc_wildcard$0 (arg) {
  if (arg === "0") return __nccwpck_require__(555854);
  else if (arg === "1") return __nccwpck_require__(583427);
  else if (arg === "2") return __nccwpck_require__(939809);
}
....
(function() {
        try {
                for (var i = 0; i < 10; i++) {
                        var font = __ncc_wildcard$0(i);
                        fontmap.names[font.name.toUpperCase()] = i;     // font-name to fon>
                        fontmap.fontsets[i] = font;                                     // >
                }
...

Note that we're passing an integer i into __ncc_wildcard$0 but the conditions inside that function is expecting strings "0" "1" "2". This causes the code to fail at runtime.

styfle commented 2 years ago

Can you share your code?

You could add the explicit require('./fonts/fnt8-desc') to your code so the expected file is included.

ZachGoldberg commented 2 years ago

The files are included correctly by NCC, it's just the typing mismatch in the comparison of the arg parameter. For now my fix is a hack sed command... sed -i 's|__ncc_wildcard$0(i|__ncc_wildcard$0(`${i}`|g' ncc/index.js

styfle commented 2 years ago

Oh I see what you mean 👍

The bug is likely webpack-asset-relocator-loader here:

https://github.com/vercel/webpack-asset-relocator-loader/blob/4cdf8167f324d25563043c584ce195928436ee24/src/asset-relocator.js#L304-L320

Feel free to submit a PR, thanks!