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
feat(config-loader): support a rule written by ESM #187
Conversation
import { validateConfig, validateRawConfig } from "@secretlint/config-validator"; | ||
|
||
export function moduleInterop<T>(moduleExports: T): T { | ||
if (moduleExports && (moduleExports as any).__esModule) { | ||
return (moduleExports as any).default?.default!; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
tip: just something that helped me with async imports: const _importDynamic = new Function('modulePath', 'return import(modulePath)')
await _importDynamic('node-fetch') // v3 is esm only |
|
||
// FIXME: https://github.com/microsoft/TypeScript/issues/43329 | ||
// module: node12 will be replace it | ||
const _importDynamic = new Function("modulePath", "return import(modulePath)"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dynamic Import hack in CommonJS + TypeScript.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This workaround should be removed in the future
- CSP Issue
- Performance Issue
- Type Issue
Yes. We have used |
@@ -123,7 +120,7 @@ export const loadPackagesFromRawConfig = ( | |||
// TODO: any to be remove | |||
const ruleModule: any = replacedDefinition | |||
? replacedDefinition.rule | |||
: moduleInterop(require(moduleResolver.resolveRulePackageName(configDescriptorRule.id))); | |||
: moduleInterop(await _importDynamic(moduleResolver.resolveRulePackageName(configDescriptorRule.id))); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It cause a bug #205
Currently, secretlint can not load a rule that is written by ESM.
This PR aims to allow to load secretlint rule as ESM modules.
Blocker
TypeScript does not support
import()
in CommonJSimport()
when module isCommonJS
· Issue #43329 · microsoft/TypeScript__esModule
interop is broken.We need to pick
.default.default
. It is ugly.export cosnt creator
instead ofexport default
#190