Skip to content

Commit

Permalink
refactor: Isolate import invocations
Browse files Browse the repository at this point in the history
  • Loading branch information
medikoo committed Dec 8, 2022
1 parent ac1e0db commit fe62096
Show file tree
Hide file tree
Showing 4 changed files with 6 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/plugins/aws/invoke-local/index.js
Expand Up @@ -836,7 +836,7 @@ class AwsInvokeLocal {
return require(modulePath);
} catch (error) {
if (error.code === 'ERR_REQUIRE_ESM') {
return await import(`file:///${modulePath}.js`);
return await require('../../../utils/import-esm')(`${modulePath}.js`);
} else if (error.code === 'MODULE_NOT_FOUND') {
// Attempt to require handler with `.cjs` extension
pathToHandler = `${pathToHandler}.cjs`;
Expand Down
3 changes: 3 additions & 0 deletions lib/utils/import-esm.js
@@ -0,0 +1,3 @@
'use strict';

module.exports = async (modPath) => import(`file:///${modPath}`);
2 changes: 1 addition & 1 deletion lib/utils/require-with-import-fallback.js
Expand Up @@ -6,7 +6,7 @@ module.exports = async (modPath) => {
} catch (error) {
// Fallback to import() if the runtime supports native ESM
if (error.code === 'ERR_REQUIRE_ESM') {
return (await import(`file:///${modPath}`)).default;
return (await require('./import-esm')(modPath)).default;
}
throw error;
}
Expand Down
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -132,8 +132,7 @@
},
{
"files": [
"lib/utils/require-with-import-fallback.js",
"lib/plugins/aws/invoke-local/index.js"
"lib/utils/import-esm.js"
],
"parserOptions": {
"ecmaVersion": 2020
Expand Down

0 comments on commit fe62096

Please sign in to comment.