sarp
sarp

Reputation: 3750

Is it possible to bundle/minimize ONLY the node_modules folder with webpack?

I have a relatively small codebase which requires not to be bundled and I need to keep the folder structure in the output. Yet, node_modules folder is quite large and I'd like to keep it bundled?/minimized with dead code elimination.

Example folder structure: - bin/file-1.js - bin/file-2.js - bin/folder-A/file-A1.js - bin/folder-A/file-A2.js - bin/folder-B/file-B1.js - bin/folder-B/file-B2.js - bin/folder-B/folder-BB/file-BB1.js - bin/folder-B/folder-BB/file-BB2.js - node_modules/module-1 - node_modules/module-2 - node_modules/module-3 - node_modules/module-4 I just need all 4 modules under node_modules minimized so that it won't take so much space when I deploy it with serverless fw to AWS lambda

Upvotes: 1

Views: 1135

Answers (2)

Khaled Osman
Khaled Osman

Reputation: 1467

You can use yarn to make use of its autoclean feature, I wrote a .yarnclean file which is a combination of the default file created by yarn in combination of the extensions deleted by node-prune.

Then using serverless-webpack's "scripts" config option you can run yarnclean to cleanup your node_modules. as described here https://github.com/serverless-heaven/serverless-webpack/issues/519#issuecomment-577727171

you can see my full setup here which includes a few more improvements you can do to get the optimal package size https://medium.com/faun/how-to-optimise-your-serverless-typescript-webpack-eslint-setup-for-performance-86d052284505

Upvotes: 0

sarp
sarp

Reputation: 3750

I ended up using node-prune. So much easier and actually reduced size is smaller than when webpack is used.

Upvotes: 0

Related Questions