Closed rschristian closed 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.
It turns out it was the custom babel exclusion that wasn't using a regex pattern:
Didn't expect running through babel to break shaking, but that seems to correct it.
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
$ yarn install && yarn copy-to-root && yarn example build
$ yarn copy-to-subpackage && yarn example build
(so hoisted version won't be used)This just uses a dummy package
my-pkg
sat in the root of the project.