Skip to content

Commit

Permalink
feat(monorepo): merge node_modules for monorepo usecases to ensure de…
Browse files Browse the repository at this point in the history
…pendencies are present
  • Loading branch information
sladg committed Dec 5, 2022
1 parent 3fa64c5 commit 90bc42e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,13 +76,23 @@ See:

### Monorepos

**Yarn monorepo**

In case you are using monorepo, there are few more requirements.
Firstly, you need to setup `outputFileTracing` in your `next.config.js` see: https://github.com/vercel/next.js/issues/36386#issuecomment-1137665939.
Firstly, you need to setup `outputFileTracingRoot: path.join(__dirname, '../../')` in your `next.config.js` see: https://github.com/vercel/next.js/issues/36386#issuecomment-1137665939.

Secondly, you need to setup `hoistingLimits: workspace`. We need `node_modules` to actually contain all the dependencies in order for NextJS to pick them up for standalone build.
Secondly, you need to setup `hoistingLimits: workspace` . We need `node_modules` to actually contain all the dependencies in order for NextJS to pick them up for standalone build.

Tested with Turbo@1.5.5 and Yarn@3.2.4

---

**Npm monorepo**

Pure npm@8 monorepo works as well without needing to deal with specific dependencies, however, keep in mind that our packaging merges both root `node_modules` as well as package's `node_modules` together, so if different versions are present, they will be overwritten.

Specify `outputFileTracingRoot: path.join(__dirname, '../../'),` in your `next.config.js` together with `externalDir: true` (in case you are importing dependencies from outside of your package).


### Server handler

Expand Down
25 changes: 19 additions & 6 deletions lib/cli/pack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,20 @@ export const packHandler = async ({ handlerPath, outputFolder, publicFolder, sta
validatePublicFolderStructure(publicFolder)
validateFolderExists(standaloneFolder)

const pathToNextOutput = findPathToNestedFile(staticNames.nextServer, standaloneFolder)

// Dependencies layer configuration
const nodeModulesFolderPath = path.resolve(standaloneFolder, staticNames.nodeFolder)
const depsLambdaFolder = 'nodejs/node_modules'
const dependenciesOutputPath = path.resolve(outputFolder, staticNames.dependenciesZip)
const nestedDependenciesOutputPath = dependenciesOutputPath.includes(pathToNextOutput) ? null : path.resolve(pathToNextOutput, staticNames.nodeFolder)

// Assets bundle configuration
const buildIdPath = path.resolve(commandCwd, './.next/BUILD_ID')
const generatedStaticContentPath = path.resolve(commandCwd, '.next/static')
const generatedStaticRemapping = '_next/static'
const assetsOutputPath = path.resolve(outputFolder, staticNames.assetsZip)

const pathToNextOutput = findPathToNestedFile(staticNames.nextServer, standaloneFolder)

// Code layer configuration
const generatedNextServerPath = path.resolve(pathToNextOutput, staticNames.nextServer)
const packageJsonPath = path.resolve(standaloneFolder, staticNames.packageJson)
Expand All @@ -47,12 +48,24 @@ export const packHandler = async ({ handlerPath, outputFolder, publicFolder, sta
rmSync(outputFolder, { force: true, recursive: true })
mkdirSync(outputFolder)

// @TODO: We need to include nested node_modules in case of mono-repos.
// Zip dependencies from standalone output in a layer-compatible format.
await zipFolder({
// In case monorepo is used, include nested node_modules folder which might include additional dependencies.
await zipMultipleFoldersOrFiles({
outputName: dependenciesOutputPath,
folderPath: nodeModulesFolderPath,
dir: depsLambdaFolder,
inputDefinition: [
{
path: nodeModulesFolderPath,
dir: depsLambdaFolder,
},
...(nestedDependenciesOutputPath
? [
{
path: nestedDependenciesOutputPath,
dir: depsLambdaFolder,
},
]
: []),
],
})

// Zip staticly generated assets and public folder.
Expand Down

0 comments on commit 90bc42e

Please sign in to comment.