developit / microbundle

📦 Zero-configuration bundler for tiny modules.
https://npm.im/microbundle
MIT License
8.03k stars 362 forks source link

Inlined dependency shaken differently when hoisted #946

Closed rschristian closed 2 years ago

rschristian commented 2 years ago

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 commented 2 years ago

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
```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 commented 2 years ago

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

https://github.com/developit/microbundle/blob/b51b855794866f3e6a0ef7dfc5672d5c1c717831/src/index.js#L572

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