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

When using esbuild plugin, import-is-undefined warning raised #471

Open
Karibash opened this issue Feb 1, 2024 · 3 comments
Open

When using esbuild plugin, import-is-undefined warning raised #471

Karibash opened this issue Feb 1, 2024 · 3 comments

Comments

@Karibash
Copy link

Karibash commented Feb 1, 2024

Environment

"@sentry/serverless": "7.99.0",
"@sentry/esbuild-plugin": "2.10.3",
"esbuild": "0.19.12",

Steps to Reproduce

If the file that is the entry point does not have a default export, an import-is-undefined warning will be raised at build time.

Cause of the issue

When injecting the stubs, the original module defaults are exported, which is the cause of this issue.

import * as OriginalModule from ${JSON.stringify(originalPath)};
export default OriginalModule.default;

Proposed amendment

I think it needs to be determined if the module has a default export.
There are two possible ways to do that.

Use dynamic import

It is possible to determine if a module has a default export by defining a function like the following.
However, there is a problem with this: it may affect the build if the module has side effects.

const moduleHasDefaultExport = async (path: string): Promise<boolean> => {
  const module = await import(path);
  return !!module.default;
};

Use AST parser

By using the AST parser as shown below, it is possible to determine if a module has a default export.
However, it seems excessive to introduce an AST parser just to solve this issue.

// I have no experience with the AST parser, so I am not sure if this code will work.
const moduleHasDefaultExport = async (path: string): Promise<boolean> => {
  const traverse = (node: Node): boolean => {
    if (node.type === 'ExportDefaultDeclaration') {
      return true;
    }

    if ('body' in node && Array.isArray(node.body)) {
      for (const childNode of node.body) {
        const result = traverse(childNode);
        if (result) {
          return true;
        }
      }
    }

    return false;
  };

  const script = await swc.parseFile(path);
  return traverse(script);
};
@getsantry getsantry bot moved this to Waiting for: Product Owner in GitHub Issues with 👀 2 Feb 1, 2024
@lforst
Copy link
Member

lforst commented Feb 1, 2024

Thanks for the investigation. You are right that this needs to be solved with a bit more elaborate solution - I hacked this together. Honestly adding a file parser isntt too crazy imo. It's a build tool so whatever.

@Cldfire
Copy link

Cldfire commented Mar 5, 2024

I'm also hitting this.

@bml1g12
Copy link

bml1g12 commented May 27, 2024

I'm also hitting this, creating a lot of spam

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

No branches or pull requests

4 participants