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

Inlined dependency shaken differently when hoisted #946

Closed
rschristian opened this issue Apr 19, 2022 · 2 comments · Fixed by #947
Closed

Inlined dependency shaken differently when hoisted #946

rschristian opened this issue Apr 19, 2022 · 2 comments · Fixed by #947

Comments

@rschristian
Copy link
Collaborator

rschristian commented Apr 19, 2022

Summary

Bit of a weird one, but I'm seeing different results from Microbundle based on whether a dependency is hoisted or not. The module is resolved without issue in both cases.

As far as I can tell this is not specific to any one dependency.

Reproduction

  1. Clone https://github.com/rschristian/microbundle-symlink-bug
  2. $ yarn install && yarn copy-to-root && yarn example build
  3. Note that the bundle is 262 b w/ brotli
  4. $ yarn copy-to-subpackage && yarn example build (so hoisted version won't be used)
  5. Note that the bundle is now 42 b w/ brotli

This just uses a dummy package my-pkg sat in the root of the project.

@rschristian
Copy link
Collaborator Author

rschristian commented Apr 20, 2022

So I narrowed it down further, the issue is that spreading a function return into an object means that object will not be shaken out, even if those functions are marked as pure. This only happens when the module is hoisted, however.

project-root/node_modules/my-pkg/index.js

var problematicFunction = () => ({ 3: '3' });

var myObject = {
    example: {
        1: '1',
        .../* @__PURE__ */ problematicFunction(),
    }
}

export const Foo = 'foo';

project-root/packages/example/index.js

import { Foo } from 'my-pkg';

console.log(Foo);

Microbundle's output

function _extends() {
  _extends = Object.assign || function (target) {
    for (var i = 1; i < arguments.length; i++) {
      var source = arguments[i];

      for (var key in source) {
        if (Object.prototype.hasOwnProperty.call(source, key)) {
          target[key] = source[key];
        }
      }
    }

    return target;
  };

  return _extends.apply(this, arguments);
}

var problematicFunction = () => ({
  3: '3'
});

({
  example: _extends({
    1: '1'
  }, /* @__PURE__ */problematicFunction())
});
const Foo = 'foo';

console.log(Foo);

If you instead install the module into project-root/packages/examples/node_modules, the problem disappears. That object is gone entirely.

Not sure what would cause this, whether it's rollup itself or some plugin.

@rschristian rschristian changed the title Inlined dependency shaken different when symlinked Inlined dependency shaken differently when symlinked Apr 20, 2022
@rschristian rschristian changed the title Inlined dependency shaken differently when symlinked Inlined dependency shaken differently when hoisted Apr 20, 2022
@rschristian
Copy link
Collaborator Author

It turns out it was the custom babel exclusion that wasn't using a regex pattern:

exclude: 'node_modules/**',

Didn't expect running through babel to break shaking, but that seems to correct it.

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

Successfully merging a pull request may close this issue.

1 participant