Next.js: why commons.xxxxxx.js is so bigger?

Created on 19 Sep 2019  路  5Comments  路  Source: vercel/next.js

Question about Next.js

As you see, I don't know why after I build my project, when I open website, commons.xxxxxx.js is so bigger ?
issue
How can I reduce it ?
Thank you !

Most helpful comment

webpack by default will look for es modules inside node_modules, nextjs disables this, so libraries that are normally tree shakable are no longer tree shakable using next's defaults. Which kind of makes sense because its gotta exec the code inside node.

This is incorrect. As you can see here: https://github.com/zeit/next.js/blob/canary/packages/next/build/webpack-config.ts#L160

browser and module are preferred and tree shaking works.

Instead of trying to make next's mysterious config and build work for you , try using rollup as an intermediate step and only let nextjs see single components per page (which you import from the rollup output) . it adds some complexity but has been worth it , at least for my use case. rollup has two cool options, 'preserveModules' and inlineDynamicImport (helps with reshaping if you need to).

The config is available here: https://github.com/zeit/next.js/blob/canary/packages/next/build/webpack-config.ts

I would highly recommend not adding a needless intermediate steps.
This recommendation might be useful when you're building a completely separate npm library of components. But in that case it's probably better to use microbundle.


@kavience you might want to try out https://github.com/zeit/next.js/pull/7696

module.exports = {
  experimental: {
    granularChunks: true
  }
}

All 5 comments

Try next-bundle-analyzer

webpack by default will look for es modules inside node_modules, nextjs disables this, so libraries that are normally tree shakable are no longer tree shakable using next's defaults. Which kind of makes sense because its gotta exec the code inside node.

Instead of trying to make next's mysterious config and build work for you , try using rollup as an intermediate step and only let nextjs see single components per page (which you import from the rollup output) . it adds some complexity but has been worth it , at least for my use case. rollup has two cool options, 'preserveModules' and inlineDynamicImport (helps with reshaping if you need to).

@YUFENGWANG @jeremy-coleman
Thank guys reply, I think maybe I import many packages , therefore after nextjs builded, commons.xxxxx.js is too bigger . but I find another way to solved it.

by compressionjs the commons.xxxxx.js is less then 1.5 M, before is 4.5 M .

webpack by default will look for es modules inside node_modules, nextjs disables this, so libraries that are normally tree shakable are no longer tree shakable using next's defaults. Which kind of makes sense because its gotta exec the code inside node.

This is incorrect. As you can see here: https://github.com/zeit/next.js/blob/canary/packages/next/build/webpack-config.ts#L160

browser and module are preferred and tree shaking works.

Instead of trying to make next's mysterious config and build work for you , try using rollup as an intermediate step and only let nextjs see single components per page (which you import from the rollup output) . it adds some complexity but has been worth it , at least for my use case. rollup has two cool options, 'preserveModules' and inlineDynamicImport (helps with reshaping if you need to).

The config is available here: https://github.com/zeit/next.js/blob/canary/packages/next/build/webpack-config.ts

I would highly recommend not adding a needless intermediate steps.
This recommendation might be useful when you're building a completely separate npm library of components. But in that case it's probably better to use microbundle.


@kavience you might want to try out https://github.com/zeit/next.js/pull/7696

module.exports = {
  experimental: {
    granularChunks: true
  }
}

@kavience or anyone else, any feedback on granular chunks?

Was this page helpful?
0 / 5 - 0 ratings

Related issues

renatorib picture renatorib  路  3Comments

pie6k picture pie6k  路  3Comments

knipferrc picture knipferrc  路  3Comments

swrdfish picture swrdfish  路  3Comments

kenji4569 picture kenji4569  路  3Comments