2020-12-14 01:09:43 +02:00
|
|
|
// When run via npm, we can leverage the injected environment variables to infer the import type
|
|
|
|
const isTypeModule = process.env.npm_package_type === 'module'
|
|
|
|
|
2020-08-15 16:54:43 +02:00
|
|
|
/**
|
|
|
|
* imports 'mjs', else requires.
|
|
|
|
* NOTE: require me late!
|
|
|
|
* @param {string} filepath
|
|
|
|
* @todo WARN on version 10 and '--experimental-modules' and '--esm'
|
|
|
|
*/
|
|
|
|
module.exports = function importFile(filepath) {
|
2020-12-14 01:09:43 +02:00
|
|
|
return isTypeModule || filepath.endsWith('.mjs')
|
2020-08-15 16:54:43 +02:00
|
|
|
? import(require('url').pathToFileURL(filepath))
|
|
|
|
: require(filepath);
|
|
|
|
};
|