Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
209 views
in Technique[技术] by (71.8m points)

reactjs - How can I prevent duplication on chunks when I use dynamic imports?

I will contextualize my question with some example code. I created a new project using CRA (not ejected).

I have this on App.js:

import React from "react";
import "./App.css";
import DropDown from "./components/DropDown";

function App() {
  return (
    <div className="picker">
      <h1>Pick a letter</h1>
      <DropDown />
    </div>
  );
}

export default App;

As you can see it just is a title with a DropDown Component.

This is DropdownComponent.js:

import React, { useState, lazy, Suspense } from "react";

const A = lazy(() => import("./A.js"));
const B = lazy(() => import("./B.js"));
const C = lazy(() => import("./C.js"));

const DropDown = () => {
  const [option, setOption] = useState("");

  const content =
    option === "A" ? (
      <Suspense fallback="loading A...">
        <A />
      </Suspense>
    ) : option === "B" ? (
      <Suspense fallback="loading B...">
        <B />
      </Suspense>
    ) : option === "C" ? (
      <Suspense fallback="loading C...">
        <C />
      </Suspense>
    ) : (
      "No choice yet"
    );

  const pickOption = (e) => {
    setOption(e.target.value);
  };

  return (
    <>
      <select name="letters" id="letters" onChange={pickOption}>
        <option value="" disabled selected>
          Select your option
        </option>
        <option value="A">A</option>
        <option value="B">B</option>
        <option value="C">C</option>
      </select>
      {content}
    </>
  );
};

export default DropDown;

Here basically I imported A, B, and C components using dynamic imports and then depending on what you choose on the selector it shows the corresponding component (A, B, and C).

Now on each letter components, I imported (just for this exercise) a random image component called react-random-image like this: (all letter components are the same so I will show just the first one)

import React from "react";
import Image from "react-random-image";

const A = () => (
  <>
    <p>This is A component</p>
    <Image width={200} height={200} />
  </>
);

export default A;

So if I build this project and check the chunks with webpack-bundle-analyzer we found that there are the 3 chunks generated with dynamic imports but if you noticed react-random-image is present on each chunk!

chunks duplicated on 3 chunks:

chunks duplicated on 3 chunks

So my question is if there is some way to prevent that kind of duplication when you use dynamic import like this example.

question from:https://stackoverflow.com/questions/66055650/how-can-i-prevent-duplication-on-chunks-when-i-use-dynamic-imports

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...