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

fix: cjs resolution when falling back to parentLoad #106

Closed
wants to merge 4 commits into from

Conversation

timfish
Copy link
Contributor

@timfish timfish commented Jun 15, 2024

When falling back to parentLoad on parsing errors (#104), we don't consider CJS.

For CJS, the parentLoad can return { format: 'commonjs', source: undefined }. In this case we should be passing this full result through as the result of load(). Once we start returning a full result from getSource, it becomes redundant and we can have a single load function.

@timfish
Copy link
Contributor Author

timfish commented Jun 17, 2024

While testing this I also found that the logged error was not as helpful as it could be. processModule may parse multiple files in the process of wrapping a module so I've updated it to log specifically which file the parsing failed for.

Error: 'import-in-the-middle' failed to wrap 'file:///Users/tim/Documents/Repositories/import-in-the-middle/test/fixtures/json-attributes.mjs'
    at load (/Users/tim/Documents/Repositories/import-in-the-middle/hook.js:306:21)
    at async nextLoad (node:internal/modules/esm/hooks:750:22)
    at async Hooks.load (node:internal/modules/esm/hooks:383:20)
    at async MessagePort.handleMessage (node:internal/modules/esm/worker:199:18) {
  cause: Error: Failed to parse 'file:///Users/tim/Documents/Repositories/import-in-the-middle/test/fixtures/json-attributes.mjs'
      at getExports (/Users/tim/Documents/Repositories/import-in-the-middle/lib/get-exports.js:115:17)
      at async processModule (/Users/tim/Documents/Repositories/import-in-the-middle/hook.js:131:23)
      at async load (/Users/tim/Documents/Repositories/import-in-the-middle/hook.js:269:25)
      at async nextLoad (node:internal/modules/esm/hooks:750:22)
      at async Hooks.load (node:internal/modules/esm/hooks:383:20)
      at async MessagePort.handleMessage (node:internal/modules/esm/worker:199:18) {
    cause: SyntaxError: Unexpected token (1:40)
        at pp$4.raise (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:3573:15)
        at pp$9.unexpected (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:772:10)
        at pp$9.semicolon (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:749:68)
        at Parser.parseImport (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn-import-attributes/lib/index.js:242:12)
        at pp$8.parseStatement (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:948:51)
        at pp$8.parseTopLevel (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:829:23)
        at Parser.parse (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:601:17)
        at Function.parse (/Users/tim/Documents/Repositories/import-in-the-middle/node_modules/acorn/dist/acorn.js:651:37)
        at getEsmExports (/Users/tim/Documents/Repositories/import-in-the-middle/lib/get-esm-exports.js:37:23)
        at getExports (/Users/tim/Documents/Repositories/import-in-the-middle/lib/get-exports.js:97:14) {
      pos: 40,
      loc: [Position],
      raisedAt: 44
    }
  }
}

@timfish timfish marked this pull request as ready for review June 17, 2024 09:24
Copy link
Contributor

@jsumners-nr jsumners-nr left a comment

Choose a reason for hiding this comment

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

Is there some test that will show the problem being solved here?

@timfish
Copy link
Contributor Author

timfish commented Jun 17, 2024

Is there some test that will show the problem being solved here?

The current reproduction that I have requires @prisma/client which has "engines": {"node": ">=16.13"} so it's not compatible with all the versions tested.

I'll see if I can reduce it down!

Qard
Qard previously approved these changes Jun 17, 2024
@timfish
Copy link
Contributor Author

timfish commented Jun 18, 2024

#108 fixes the issue with @prisma/client and I'm unable to find another reproduction that makes these changes necessary. The broken resolution meant that CJS code was trying to load ESM code.

Maybe this PR will be required to support --experimental-require-module?

@timfish timfish marked this pull request as draft June 18, 2024 14:12
@timfish
Copy link
Contributor Author

timfish commented Jun 25, 2024

Closing in favour of #115, #114 and #109

@timfish timfish closed this Jun 25, 2024
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.

3 participants