vercel / vercel/nft

Dynamically required modules based on path.resolve reference src in build

Open
#306 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

question
Dominant language
JavaScript
Stars
1.7k
Forks
185
PR merge metrics
No merged PRs in 30d

Description

Tested against 11.1.4
I have a monorepo with a server api where some of the files are dynamically sync loaded through require based on variables.

const fileArray = parseDirs(path.resolve('../server/src/modules'));
const modelArray = fileArray.filter(
	file => file.endsWith('graphql'),
);
modelArray.map(file => {
   const pathArray = file.split('modules/');
   const model = require('../modules/' + pathArray[1]);
   [...]
});
[..]
const model = require('../modules/' + pathArray[1]);

This works fine in development.
When I build to a server version and removed src directories things start to fail.

Ultimately I see references in the build that reference the original path.resolve('../server/src/modules'). In other words, this is not being processed correctly by webpack. It tries to load from src directories instead of the processed chunks.

Is there a way to resolve this? Please don't say: upgrade to next 12. I've had tons of issues with that, not least of which the obligation to upgrade to async import.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reproducing the dynamic require example against the reported 11.1.4 behavior, focusing on the path.resolve('../server/src/modules') and generated build references. Trace how the build handles those dynamic module paths; done means the built server resolves processed chunks rather than the removed src directories.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, webpack
Domain
build-system, tooling
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.