-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(plugin): add support for mjs and cjs filters
closes #163
- Loading branch information
Showing
10 changed files
with
82 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = async req => { | ||
try { | ||
return /\{"products":\[/.test(req.response.content.text ?? ''); | ||
} catch { | ||
return false; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default async req => { | ||
try { | ||
return /\{"products":\[/.test(req.response.content.text ?? ''); | ||
} catch { | ||
return false; | ||
} | ||
}; |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,17 +1,31 @@ | ||
import { ErrorUtils } from './ErrorUtils'; | ||
|
||
export class Loader { | ||
public static load<T>(path: string): T | undefined { | ||
const module = this.interopRequireDefault( | ||
// eslint-disable-next-line @typescript-eslint/no-var-requires -- `ts-node` does not handle the dynamic imports like `import(path)` | ||
require(/* webpackIgnore: true */ path) | ||
); | ||
public static async load<T>(path: string): Promise<T | undefined> { | ||
let module: unknown; | ||
|
||
try { | ||
module = require(/* webpackIgnore: true */ path); | ||
} catch (err) { | ||
const stack = ErrorUtils.isError(err) ? err.stack : undefined; | ||
|
||
if ( | ||
!stack?.includes('[ERR_REQUIRE_ESM]') && | ||
!stack?.includes( | ||
'SyntaxError: Cannot use import statement outside a module' | ||
) | ||
) { | ||
throw err; | ||
} | ||
|
||
module = (await import(/* webpackIgnore: true */ path)).default; | ||
} | ||
|
||
return module?.default as T | undefined; | ||
return this.interopRequireDefault<T | undefined>(module); | ||
} | ||
|
||
private static interopRequireDefault(obj: unknown): { | ||
default: unknown; | ||
} { | ||
private static interopRequireDefault<T>(m: unknown): T | undefined { | ||
// @ts-expect-error unknown is not assignable to the module type | ||
return obj?.__esModule ? obj : { default: obj }; | ||
return m && m.__esModule && m.default ? m.default : m; | ||
} | ||
} |