richardtallent / vite-plugin-singlefile

Vite plugin for inlining JavaScript and CSS resources
MIT License
808 stars 53 forks source link

Replace `replaceAll()` to support Node.JS v14 #63

Closed aloisklink closed 1 year ago

aloisklink commented 1 year ago

String.prototype.replaceAll() was only added in Node.JS v15, and so doesn't work in Node.JS v14, see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/String/replaceAll

However, it is easy to replace with a Regex that has the /g (global) flag enabled for Node.JS v14 support.

Additionally, I modified your tsconfig-base.json so that it copies the recommended lib: [es2020] for Node.JS v14, see https://github.com/tsconfig/bases/blob/main/bases/node14.json This means that TypeScript will throw an error if a non-supported function is used, like replaceAll().

I've also updated the engines.node field in the package.json so that it matches that one that Vite v3 has, see https://github.com/vitejs/vite/blob/9c808cdec89b807bff33eef37dfbb03557291ec1/packages/vite/package.json#L33-L35

richardtallent commented 1 year ago

Is there a serious use case for continuing to use Node 14? Node 15 came out over 2 years ago.

aloisklink commented 1 year ago

Is there a serious use case for continuing to use Node 14? Node 15 came out over 2 years ago.

It's still an officially supported Node.JS LTS version until 2023-04-30 https://github.com/nodejs/release#release-schedule

Most importantly, it's still supported by Vite v3, see https://github.com/vitejs/vite/blob/9c808cdec89b807bff33eef37dfbb03557291ec1/packages/vite/package.json#L34

Generally plugins support the same version of Node.JS as their main library.

I'm trying to convert an open-source projects to use Vite and this plugin, but because we officially support Node.JS v14, and can't drop support for it unless we make a BREAKING CHANGE, it'll be super helpful if this plugin also supported Node.JS v14!

richardtallent commented 1 year ago

That's fair! I knew that Vite still supported it, but the user base of this plug-in is orders of magnitude smaller, so I don't want to deal with extra headaches and gotchas and language limits if there's no actual use case, just compatibility for compatibility's sake.

(I dropped IE11 support on everything I do WAY before it finally died lol)

I'll look this over and see if I can push it out before I go on vacation this weekend.

Thanks!