Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: remove dev node_modules deps #242

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

khuezy
Copy link
Contributor

@khuezy khuezy commented Sep 20, 2023

fixes: #166

This reduces my zipped lambda from 31MB => 10MB

I investigated the use of next.config's outputFileTracingExcludes but that is hard to merge w/ the user's config file since the config is js and may contain functions - which we can't easily stringify. Doing a complex regex to replace is very tricky and brittle if the user's config file is non standard too...

So the solution is to do the node_modules cleanup AFTER next has built.

@vercel
Copy link

vercel bot commented Sep 20, 2023

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
open-next ✅ Ready (Inspect) Visit Preview 💬 Add feedback Sep 20, 2023 9:50pm

@changeset-bot
Copy link

changeset-bot bot commented Sep 20, 2023

⚠️ No Changeset found

Latest commit: e8e8ea7

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

if (options.minify) {
removeNodeModule(path.join(outputPath, "node_modules"), [
"@esbuild",
"prisma/libquery_engine-darwin-arm64.dylib.node",
Copy link
Contributor

@sladg sladg Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we include all prisma engines?

meaning all other OS packages not compatible with Lambda

Copy link
Contributor Author

@khuezy khuezy Sep 21, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you know where I can get a list of all non-lambda engines?
I might need to update this and use micromatch to allow for negation...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Honestly, what the hell is wrong with prisma?

// "react", // TODO: remove react/react-dom when nextjs updates its precompile versions
// "react-dom",
"@webassemblyjs",
"uglify-js",
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicate here, it's also listed above!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thx, I'll have to retest this w/ Next 14, which seems to have fixed the dev deps getting traced to the standalone output.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Been busy last week and caught a cold this week... I'll eventually test this to confirm... @mathisobadia was still seeing these dev modules but I didn't...

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here's my server function lambda zip, on Next 14.0.2:

image

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@revmischa This is inside your server-function lambda ? There is a lot of things that you should be able to remove, you can use experimental.outputFileTracingExcludes from next config to remove all of those.
You should probably create an issue in next repo as well, those dev deps should have been removed.

@mathisobadia
Copy link
Contributor

@khuezy The docs mention that next 14+ doesn't have the issue anymore but I that doesn't seem to be the case for me. So this PR is still valuable

@seawatts
Copy link

Any progress on this?

@khuezy
Copy link
Contributor Author

khuezy commented Dec 11, 2023

Any progress on this?

Not much, for now, you can try using outputFileTracingExcludes:

 outputFileTracingExcludes: {
   '*': [
     '**@swc/core**',
     '**esbuild**',
    '**uglify**',
      'webassemblyjs',
     '**sass**',
     '**caniuse-lite**',
     "@aws-sdk"
   ],
},

It's hard to know which to remove so it might be better for the user to manage that. For example, if you add "watchpack", it'll break server actions even though it looks like a dev dep.

@revmischa
Copy link
Contributor

Any progress on this?

Not much, for now, you can try using outputFileTracingExcludes:

 outputFileTracingExcludes: {
   '*': [
     '**@swc/core**',
     '**esbuild**',
    '**uglify**',
      'webassemblyjs',
     '**sass**',
     '**caniuse-lite**',
     "@aws-sdk"
   ],
},

I'm trying to use this but I get a "RangeError: Maximum call stack size exceeded" error: vercel/next.js#42641 (comment)

@khuezy
Copy link
Contributor Author

khuezy commented Dec 30, 2023

Something change in the later version of next (not sure if bug or by design.) Using the "**" causes an infinite loop. You have remove the wildcard.

@revmischa
Copy link
Contributor

Something change in the later version of next (not sure if bug or by design.) Using the "**" causes an infinite loop. You have remove the wildcard.

Hm, I tried

    outputFileTracingExcludes: {
      "*": [
        "@swc/core",
        "webpack",
        "docx",
       ]

And get the same error

@enroly-mike
Copy link

Maybe im doing something weird here, but on nextjs 14.1 and pnpm ... I've tried to manually remove the node-modules from the server function you have listed above and my lambda fails pretty hard.

{ "errorType": "Error", "errorMessage": "Cannot find module 'styled-jsx/package.json'\nRequire stack:\n- /var/task/typescript/frontend/insights/node_modules/next/dist/server/require-hook.js\n- /var/task/typescript/frontend/insights/node_modules/next/dist/server/next-server.js", "code": "MODULE_NOT_FOUND", "requireStack": [ "/var/task/typescript/frontend/insights/node_modules/next/dist/server/require-hook.js", "/var/task/typescript/frontend/insights/node_modules/next/dist/server/next-server.js" ], "stack": [ "Error: Cannot find module 'styled-jsx/package.json'", "Require stack:", "- /var/task/typescript/frontend/insights/node_modules/next/dist/server/require-hook.js", "- /var/task/typescript/frontend/insights/node_modules/next/dist/server/next-server.js", " at Module._resolveFilename (node:internal/modules/cjs/loader:1134:15)", " at resolve (node:internal/modules/helpers:188:19)", " at Object.<anonymous> (/var/task/typescript/frontend/insights/node_modules/next/dist/server/require-hook.js:38:32)", " at Module._compile (node:internal/modules/cjs/loader:1356:14)", " at Module._extensions..js (node:internal/modules/cjs/loader:1414:10)", " at Module.load (node:internal/modules/cjs/loader:1197:32)", " at Module._load (node:internal/modules/cjs/loader:1013:12)", " at Module.require (node:internal/modules/cjs/loader:1225:19)", " at require (node:internal/modules/helpers:177:18)", " at Object.<anonymous> (/var/task/typescript/frontend/insights/node_modules/next/dist/server/next-server.js:13:1)" ] }

@khuezy
Copy link
Contributor Author

khuezy commented Feb 4, 2024

I've tried to manually remove the node-modules

Did you remove the entire folder? You should only remove the dev subfolders, not the entire node_modules


if (options.minify) {
removeNodeModule(path.join(outputPath, "node_modules"), [
"@esbuild",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my logs:
image
can we remove some of them also?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Very likely yes, but you'll need to test for yourself. You can probably remove :

  • swc core
  • sharp
  • esbuild
  • caniuse lite
  • uglify-js
  • terser

@AndreyMay
Copy link

AndreyMay commented Jul 2, 2024

nextjs v14.2.4 / open-next 3.0.2 -- still includes dev dependencies in standalone build.

This next config works and excludes unwanted dependencies (note: the setup for monorepo):

    outputFileTracingRoot: path.join(__dirname, '../../'),
    outputFileTracingExcludes: {
      '*': [
        './**/.prisma/client/libquery_engine-darwin*', // prisma mac binary
        './**/@swc/core-linux-x64-gnu*',
        './**/@swc/core-linux-x64-musl*',
        './**/@esbuild*',
        './**/webpack/',
        './**/rollup*',
        './**/terser*',
        './**/sharp*',
      ],
    },

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bundled node_modules bloat
10 participants