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 build
to 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
/test
and/test2
routes (.next/static/pages/test-[hash].js
,.next/static/pages/test2-[hash].js
)Expected behavior: Since
/test
only usesSTRING1
and/test2
only usesSTRING2
, each respective route's bundled js should only include the relevantgetString
function.Note that
STRING3
, which is also defined in the same file, is correctly omitted from all routes' bundles.