sentry-javascript-bundler-plugins
sentry-javascript-bundler-plugins copied to clipboard
When using esbuild plugin, import-is-undefined warning raised
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.
https://github.com/getsentry/sentry-javascript-bundler-plugins/blob/4d123db48d7aee75e79e98b82040423d5a8c0f26/packages/esbuild-plugin/src/index.ts#L100-L101
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);
};
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.
I'm also hitting this.
I'm also hitting this, creating a lot of spam
Also hitting this.