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

Repeating require calls from the same external package #382

Closed
lxsmnsyc opened this issue Sep 12, 2020 · 3 comments
Closed

Repeating require calls from the same external package #382

lxsmnsyc opened this issue Sep 12, 2020 · 3 comments

Comments

@lxsmnsyc
Copy link

Good day,

I noticed that when bundling files with an external module defined, where one or more files imports that module, the transpiled import is repeated per bundled file.

Example output of an ESM build:

// src/useConstant.ts
import {useRef} from "react";
function useConstant(supplier) {
  const ref = useRef();
  if (!ref.current) {
    ref.current = {
      value: supplier()
    };
  }
  return ref.current.value;
}

// src/useForceUpdate.ts
import {useState} from "react";
function useForceUpdate() {
  const [, setState] = useState(false);
  const update = useConstantCallback(() => {
    setState((flag) => !flag);
  });
  return update;
}

// src/useIsomorphicEffect.ts
import {useEffect, useLayoutEffect} from "react";
const useIsomorphicEffect = typeof window === "undefined" ? useEffect : useLayoutEffect;
var useIsomorphicEffect_default = useIsomorphicEffect;

// src/useRenderCount.ts
import {useRef as useRef2} from "react";
function useRenderCount() {
  const ref = useRef2(0);
  ref.current += 1;
  return ref.current;
}

export {
  useConstant,
  useConstantCallback,
  useForceUpdate,
  useIsomorphicEffect_default as useIsomorphicEffect,
  useRefSupplier,
  useRenderCount
};
//# sourceMappingURL=react-hooks.esm.js.map

even the minified bundle for CommonJS builds are affected.

Hope that this gets fixed, it would greatly reduce the bundle output specially for an entrypoint with hundreds of files included.

@flipsasser
Copy link

I'm seeing the same behavior. It would be fantastic if we could either combine imports during bundling or remove them entirely when using a "globals" plugin (see #337).

I dug into the code a little bit to see if I could put together a PR, but I'm new to Go and having trouble figuring out where exactly this is implemented. I'm suspicious that this comment is relevant, but that makes me think this is a non-trivial change: it looks like the re-imports (e.g. useRef vs useRef2 from the above) are named based on per-thread basis.

But of course as I type that I realize that wouldn't work, because the possibility of collisions would be too high - so I'm probably not in the right place. TLDR, the Go code is above my head right now. I'll keep at it, but would love pointers if anyone has time!

@flipsasser
Copy link

Yeah I feel like my head is coming apart. I'm not smart enough to tackle this, at least certainly not this week. I'll keep working at it but there's a lot under the hood.

@evanw
Copy link
Owner

evanw commented Nov 25, 2020

Thanks for reporting this. Closing as a duplicate of #475.

@evanw evanw closed this as completed Nov 25, 2020
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

No branches or pull requests

3 participants