Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implement flatMap fallback for better Node.js compatibility #26

Closed
danburzo opened this issue Aug 17, 2020 · 2 comments
Closed

Implement flatMap fallback for better Node.js compatibility #26

danburzo opened this issue Aug 17, 2020 · 2 comments
Labels
bug Something isn't working good first issue Good for newcomers released

Comments

@danburzo
Copy link
Contributor

danburzo commented Aug 17, 2020

The flatMap Array method has only been introduced in Node 11, but Node 10 will be still around until (at least) 2021. I propose a simple fallback for the missing method, to make the loader compatible with more Node.js versions:

diff --git a/src/minify-plugin.js b/src/minify-plugin.js
index 7916f1e..1e8e83f 100644
--- a/src/minify-plugin.js
+++ b/src/minify-plugin.js
@@ -3,6 +3,8 @@ const { RawSource, SourceMapSource } = require('webpack-sources')
 const isJsFile = /\.js$/i
 const pluginName = 'esbuild-minify'
 
+const flatMap = (arr, cb) => arr.flatMap ? arr.flatMap(cb) : [].concat(...arr.map(cb))
+
 class ESBuildMinifyPlugin {
   constructor(options) {
     this.options = { ...options }
@@ -39,7 +41,7 @@ class ESBuildMinifyPlugin {
       compilation.hooks.optimizeChunkAssets.tapPromise(
         pluginName,
         async (chunks) => {
-          const transforms = chunks.flatMap((chunk) => {
+          const transforms = flatMap(chunks, (chunk) => {
             return chunk.files
               .filter((file) => isJsFile.test(file))
               .map(async (file) => {

If there's interest in this, I can make a PR.

@privatenumber
Copy link
Owner

Good call. Thanks for catching!

PRs welcome!

@privatenumber privatenumber added good first issue Good for newcomers bug Something isn't working labels Aug 17, 2020
privatenumber added a commit that referenced this issue Aug 18, 2020
fix: Add flatMap() fallback; fixes #26
@github-actions
Copy link

🎉 This issue has been resolved in version 2.1.1 🎉

The release is available on:

Your semantic-release bot 📦🚀

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working good first issue Good for newcomers released
Projects
None yet
Development

No branches or pull requests

2 participants