markdown-toc icon indicating copy to clipboard operation
markdown-toc copied to clipboard

Not working with esbuild/vite

Open DiscreteTom opened this issue 1 year ago • 1 comments

Thanks for this cool lib!

I'm trying to use this lib in a frontend project using vite, when I build my project with esbuild I got the following error:

Illegal reassignment to import 'commonjsRequire'
file: C:/Users/xxx/node_modules/markdown-toc/lib/utils.js:10:0
 8: var utils = require('lazy-cache')(require);
 9: var fn = require;
10: require = utils;
    ^
11: 
12: /**

Looks like require is a reserved word and can't be changed, so how to solve this?

DiscreteTom avatar Mar 31 '23 12:03 DiscreteTom

In JavaScript, require is not reserved, however many bundling tools do things to make it seem like it is.

For webpack, the unlazy-loader transforms the pattern used in this utils.js file, so that require is not reassigned.

I'm not as familiar with vite (is it using esbuild?), but there might be a way to configure transformation plugins in a similar fashion. If there isn't a direct way to use a webpack plugin with vite, the unlazy-loader function could be wrapped in a way to adapter to vite.

I hope this helps, and if you try something that works (or almost works), please let us know.

doowb avatar Apr 04 '23 15:04 doowb