Minimal example of tree-shaking not working as expected in a NextJS app when multiple routes import different things from the same file.
-
Install dependencies with
npm install -
Run
npm run buildto generate the production output -
Text-search for "string1" and "string2" in the static output folder js files (
.next/static) -
See that both strings show up in the js bundles for the
/testand/test2routes (.next/static/pages/test-[hash].js,.next/static/pages/test2-[hash].js)Expected behavior: Since
/testonly usesSTRING1and/test2only usesSTRING2, each respective route's bundled js should only include the relevantgetStringfunction.Note that
STRING3, which is also defined in the same file, is correctly omitted from all routes' bundles.