babel / minify

:scissors: An ES6+ aware minifier based on the Babel toolchain (beta)
https://babeljs.io/repl
MIT License
4.39k stars 225 forks source link

Do not attempt to convert functions in constant-folding. #937

Closed coreyfarrell closed 5 years ago

coreyfarrell commented 5 years ago

Fixes #936

coreyfarrell commented 5 years ago

I've switched to terser so I'm closing this. I'm about to delete my fork but wanted to leave the code here in-case someone later decides they want to merge this.

From 089f6057858030d685b17eb7b5f60e954c1fd006 Mon Sep 17 00:00:00 2001
From: Corey Farrell <git@cfware.com>
Date: Tue, 22 Jan 2019 13:31:27 -0500
Subject: [PATCH] Do not attempt to convert functions in constant-folding.

Fixes #936
---
 .../__tests__/fixtures/issue-936/actual.js                 | 2 ++
 .../__tests__/fixtures/issue-936/expected.js               | 2 ++
 packages/babel-plugin-minify-constant-folding/src/index.js | 7 +++++++
 3 files changed, 11 insertions(+)
 create mode 100644 packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-936/actual.js
 create mode 100644 packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-936/expected.js

diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-936/actual.js b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-936/actual.js
new file mode 100644
index 0000000..bc8e55a
--- /dev/null
+++ b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-936/actual.js
@@ -0,0 +1,2 @@
+var trim = ''.trim;
+console.log(trim.call(' test '));
diff --git a/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-936/expected.js b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-936/expected.js
new file mode 100644
index 0000000..b1af9a7
--- /dev/null
+++ b/packages/babel-plugin-minify-constant-folding/__tests__/fixtures/issue-936/expected.js
@@ -0,0 +1,2 @@
+var trim = ''.trim;
+console.log(trim.call(' test '));
\ No newline at end of file
diff --git a/packages/babel-plugin-minify-constant-folding/src/index.js b/packages/babel-plugin-minify-constant-folding/src/index.js
index 7276456..e8d9caa 100644
--- a/packages/babel-plugin-minify-constant-folding/src/index.js
+++ b/packages/babel-plugin-minify-constant-folding/src/index.js
@@ -175,6 +175,13 @@ module.exports = babel => {
             return;
           }

+          // Some ponyfill's use native functions if available by grabbing
+          // a function reference from a constant.  t.valueToNode throws an
+          // exception if the argument is a function.
+          if (typeof res.value === "function") {
+            return;
+          }
+
           const node = t.valueToNode(res.value);
           node[seen] = true;
           path.replaceWith(node);
-- 
2.17.2