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

[tree-shaking] function with no side-effects & args included in output #2539

Open
leeoniya opened this issue Oct 31, 2018 · 2 comments
Open

Comments

@leeoniya
Copy link

// main.js

import { extend } from './extend.js';

function Foo() {}
function Bar() {}
extend(Foo, Bar, {});
const baz = 5;
export default baz;
// extend.js

export function extend(parent, child, props) {
  child.__proto__ = parent;
  var proto = child.prototype = Object.create(parent.prototype);
  proto.constructor = child;
  for (var p in props)
    proto[p] = props[p];
}
// output

var baz = (function () {
  'use strict';

  function extend(parent, child, props) {
    child.__proto__ = parent;
    var proto = child.prototype = Object.create(parent.prototype);
    proto.constructor = child;
    for (var p in props)
      proto[p] = props[p];
  }

  function Foo() {}

  function Bar() {}

  extend(Foo, Bar, {});

  const baz = 5;

  return baz;

}());
// expected

var baz = (function () {
  'use strict';

  const baz = 5;

  return baz;
}());

Repro:

https://rollupjs.org/repl?version=0.66.6&shareable=JTdCJTIybW9kdWxlcyUyMiUzQSU1QiU3QiUyMm5hbWUlMjIlM0ElMjJtYWluLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmltcG9ydCUyMCU3QiUyMGV4dGVuZCUyMCU3RCUyMGZyb20lMjAnLiUyRmV4dGVuZC5qcyclM0IlNUNuJTVDbmZ1bmN0aW9uJTIwRm9vKCklMjAlN0IlN0QlNUNuJTVDbmZ1bmN0aW9uJTIwQmFyKCklMjAlN0IlN0QlNUNuJTVDbmV4dGVuZChGb28lMkMlMjBCYXIlMkMlMjAlN0IlN0QpJTNCJTVDbiU1Q25jb25zdCUyMGJheiUyMCUzRCUyMDUlM0IlNUNuJTVDbmV4cG9ydCUyMGRlZmF1bHQlMjBiYXolM0IlMjIlN0QlMkMlN0IlMjJuYW1lJTIyJTNBJTIyZXh0ZW5kLmpzJTIyJTJDJTIyY29kZSUyMiUzQSUyMmV4cG9ydCUyMGZ1bmN0aW9uJTIwZXh0ZW5kKHBhcmVudCUyQyUyMGNoaWxkJTJDJTIwcHJvcHMpJTIwJTdCJTVDbiU1Q3RjaGlsZC5fX3Byb3RvX18lMjAlM0QlMjBwYXJlbnQlM0IlNUNuJTVDdHZhciUyMHByb3RvJTIwJTNEJTIwY2hpbGQucHJvdG90eXBlJTIwJTNEJTIwT2JqZWN0LmNyZWF0ZShwYXJlbnQucHJvdG90eXBlKSUzQiU1Q24lNUN0cHJvdG8uY29uc3RydWN0b3IlMjAlM0QlMjBjaGlsZCUzQiU1Q24lNUN0Zm9yJTIwKHZhciUyMHAlMjBpbiUyMHByb3BzKSU1Q24lNUN0JTVDdHByb3RvJTVCcCU1RCUyMCUzRCUyMHByb3BzJTVCcCU1RCUzQiU1Q24lN0QlMjIlN0QlNUQlMkMlMjJvcHRpb25zJTIyJTNBJTdCJTIyZm9ybWF0JTIyJTNBJTIyaWlmZSUyMiUyQyUyMm5hbWUlMjIlM0ElMjJiYXolMjIlMkMlMjJhbWQlMjIlM0ElN0IlMjJpZCUyMiUzQSUyMiUyMiU3RCU3RCUyQyUyMmV4YW1wbGUlMjIlM0FudWxsJTdE

@leeoniya
Copy link
Author

leeoniya commented Oct 31, 2018

hmm, i guess the title is not quite correct. the function does mutate the arguments, but those args are never used or exported afterwards.

this might be a tricky case, not even Closure Compiler is able to strip this out in ADVANCED mode :(

support for the /*#__PURE__*/ annotation could maybe help here?

#1293, #2429

for the time being i'm able to work around this by using a regex replace plugin [1] to create a no-op extend shell:

replace({
  patterns: [
    {
      test: /^const.*require.*extend.*/gm,
      replace: 'const extend = () => {};',
    }
  ]
})

[1] https://github.com/jetiny/rollup-plugin-re

@antfu
Copy link
Contributor

antfu commented Jun 7, 2023

I guess static side-effects analysis will always reach limitations for dynamic parts. #5024 introduced the annotation to manually mark functions as side-effects-free, which I think should be able to solve this issue.

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

No branches or pull requests

4 participants