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

Scope hoisting less effective since rewrite #7481

Closed
101arrowz opened this issue Dec 24, 2021 · 6 comments · Fixed by #7833
Closed

Scope hoisting less effective since rewrite #7481

101arrowz opened this issue Dec 24, 2021 · 6 comments · Fixed by #7833

Comments

@101arrowz
Copy link
Member

🐛 bug report

Scope hoisting is dramatically less effective since #6230. This seems to be because #__PURE__ annotations don't work properly anymore.

🎛 Configuration (.babelrc, package.json, cli command)

I'm testing with zero configuration. Here's package.json:

{
  "name": "fflt",
  "version": "1.0.0",
  "license": "MIT",
  "devDependencies": {
    "parcel": "2.0.0-nightly.655"
  },
  "dependencies": {
    "fflate": "^0.7.2"
  }
}

🤔 Expected Behavior

The scope hoisting rewrite should not adversely affect scope hoisting for any case.

😯 Current Behavior

Scope hoisting is dramatically less effective in every version since ba57b65 (#6230).

💁 Possible Solution

The behavior is similar to what happens when you remove all #__PURE__ annotations from fflate. Maybe all comments are now being stripped before terser processes the code?

🔦 Context

Without the ability to utilize /*#__PURE__*/ comments, every function call is considered a side effect, which makes it impossible to tree-shake out IIFEs and variables used in exported closures that are initialized to the result of a function call.

💻 Code Sample

Here's my index.html:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>
<body>
    <script type="module" src="./index.js"></script>
</body>
</html>

Here's index.js:

import { gzipSync, strToU8 } from 'fflate';

console.log(gzipSync(strToU8('hello world')));

To build, I use parcel build index.html. When using parcel@2.0.0-nightly.654 (gitHead = fdbcbd6):
6.78kB JS file

Running the same command using parcel@2.0.0-nightly.655 (gitHead = ba57b65):
21.79kB JS file

🌍 Your Environment

Software Version(s)
Parcel Any version since 2.0.0-nightly.655
Node v16.6.2
npm/Yarn Yarn 1.22.15
Operating System Ubuntu 20.04 on WSL
@mischnic
Copy link
Member

mischnic commented Dec 24, 2021

I have identified one problem where pure comments of IIFE are stripped during the build:

// index.js
import { strToU8 } from './other.js';
console.log((strToU8('hello world')));


// other.js
var u32 = Uint32Array;
var flm = /*#__PURE__*/ hMap(flt, 9, 0);
var deo = /*#__PURE__*/ new u32([
    65540, 131080, 131088
]);
export function strToU8(a) {
    return "converted";
}
var Unzip = /*#__PURE__*/ (function () {
    console.log("UNUSED")
}());
export { Unzip };

results in

(() => {
var $da3800f92a93f12a$var$u32 = Uint32Array;
var $da3800f92a93f12a$var$flm = /*#__PURE__*/ hMap(flt, 9, 0);
var $da3800f92a93f12a$var$deo = /*#__PURE__*/ new $da3800f92a93f12a$var$u32([
    65540,
    131080,
    131088
]);
function $da3800f92a93f12a$export$366b39a6daa8ed7a(a) {
    return "converted";
}
var $da3800f92a93f12a$export$9b7485f84cbaaa56 = function() { // <-------  !!!
    console.log("UNUSED");
}();


console.log($da3800f92a93f12a$export$366b39a6daa8ed7a('hello world'));

})();
//# sourceMappingURL=index.js.map

See the linked swc issue, we'll see if there are more problems once that particular one is fixed.

@101arrowz
Copy link
Member Author

Thanks for researching the problem! For future reference, how did you get the bundled but pre-terser code?

@mischnic
Copy link
Member

parcel build ... --no-optimize

@mischnic
Copy link
Member

If I comment that expr_simplifier pass out, the output is 6.78 KB which seems to be what you were expecting.

@101arrowz
Copy link
Member Author

Since swc-project/swc#3115 has been resolved if you add paren_remover, this should be fixable now in Parcel? Or is paren_remover on by default?

@mischnic
Copy link
Member

swc needs to be bumped and that pass has be added

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants