Skip to content

Commit

Permalink
fix: use absolute specifier for dynamic import
Browse files Browse the repository at this point in the history
  • Loading branch information
3cp committed Jun 9, 2022
1 parent 0b97628 commit 26ea6fe
Showing 1 changed file with 10 additions and 4 deletions.
14 changes: 10 additions & 4 deletions lib/normalised-import.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
const path = require('path');

// Normalise possible mjs/cjs/js file import.
module.exports = async function(path) {
module.exports = async function(modulePath) {
// Use Absolute specifiers
// https://nodejs.org/dist/latest-v16.x/docs/api/esm.html#import-specifiers
const p = 'file://' + path.resolve(modulePath);

// 1. try path.mjs first
try {
return await import(path + '.mjs');
return await import(p + '.mjs');
} catch (e) {
// ignore
// console.error(e);
Expand All @@ -13,7 +19,7 @@ module.exports = async function(path) {
// Note we use import() to load commonjs code,
// it automatically provides module.exports in
// default export.
return await import(path + '.cjs');
return await import(p + '.cjs');
} catch (e) {
// ignore
// console.error(e);
Expand All @@ -25,7 +31,7 @@ module.exports = async function(path) {
// Note we use import() to load commonjs code,
// it automatically provides module.exports in
// default export.
return await import(path + '.js');
return await import(p + '.js');
} catch (e) {
// ignore
// console.error(e);
Expand Down

0 comments on commit 26ea6fe

Please sign in to comment.