Replies: 2 comments 3 replies
-
|
I'm confused - why not just take the actual project folder, run |
Beta Was this translation helpful? Give feedback.
-
|
You are on the right general track with a loader, but I would change two details. First, don't base the retry on the loader file's Second, That explains the error. Node is now trying to resolve For ESM I would split the two cases instead of trying to reproduce import { access } from 'node:fs/promises'
import path from 'node:path'
import { fileURLToPath, pathToFileURL } from 'node:url'
const commonDir = path.resolve(process.cwd(), 'test/Common')
async function exists(file) {
try {
await access(file)
return true
} catch {
return false
}
}
export async function resolve(specifier, context, nextResolve) {
try {
return await nextResolve(specifier, context)
} catch (error) {
if (error?.code !== 'ERR_MODULE_NOT_FOUND') throw error
}
// Only fallback your internal Common imports, not every package import.
// Adjust this test to match the names you actually import from Common.
if (!specifier.startsWith('.') && !specifier.startsWith('/') && !specifier.startsWith('node:')) {
const commonIndex = path.join(commonDir, specifier, 'index.js')
if (await exists(commonIndex)) {
return {
url: pathToFileURL(commonIndex).href,
shortCircuit: true,
}
}
const commonFile = path.join(commonDir, `${specifier}.js`)
if (await exists(commonFile)) {
return {
url: pathToFileURL(commonFile).href,
shortCircuit: true,
}
}
}
throw new Error(`Cannot resolve ${specifier} from ${context.parentURL}`)
}The important part is that this fallback only maps the Common modules you own. For external packages like If you really want to retry package resolution from another location, use a fake file inside that location, not the directory itself, for example: await nextResolve(specifier, {
...context,
parentURL: pathToFileURL(path.join(lambdaTestDir, '__resolve__.mjs')).href,
})But I would keep that as a last resort. The cleaner long-term shape is still to make |
Beta Was this translation helpful? Give feedback.
Uh oh!
There was an error while loading. Please reload this page.
-
I have mono repo with a structure very similar to this:
The CI system creates the zip files that are deployed on the AWS Lambda system and does so by taking the content of lambda folder and placing next to it the content of the Common folder renamed as node_modules.
This has worked since the beginning and when unit tests were added later on, it was decided to have as little changes to the existing code as possible.
Each .js file inside the test folder uses module-alias to trick the loader into looking at the Common folder as a valid source for the files required by the lambda code. This allows debugging the unit tests while being able to edit the common files "in place", greatly speeding up the development process.
Here is the code added at the top of each .js file in the test subfolders:
The second path alias is here because only the
package.jsonfiles in the test folders are defining thedevDependenciesto@awsmodules because they already are available in the target environment, and because the entry point is via tests only. This avoids cluttering the developer's machines with lots of duplicate folders.This works fairly well in CommonJS format but as we must modernize the code base, we are trying to move to ES6 syntax and "module" resolution with as little changes as possible.
One thing that is clear is that adding path aliases like before is no longer possible because imports are resolved long before any of our code is run. This has to be done via the
--importflag on the command line so that I can register a module hook which is what I tried with the following code, inspired byesm-module-alias:This is not exactly the same as the situation before because the
loader.mjsfile is loaded once at the start of the process called by the test runner (mocha) which means__dirnameis the one from that file, not the one for each test file.But even then, the retry appears to work as I see the console log, but sadly it still throws an error, but this time with a hint message:
It's like it has "seen" the folder but did not find the expected files for
ESmodules, which I find weird as they appear to be in thedist-esfolder.Am I on the right track here? Would you have any suggestions for this situation?
I looked at subpath imports (with the # prefix) but this does not work because it cannot look into parent folders for resolution.
Thanks for your time.
Beta Was this translation helpful? Give feedback.
All reactions