Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
- Loading branch information
1 parent
c43fd72
commit 4899346
Showing
8 changed files
with
97 additions
and
14 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
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,14 +1,13 @@ | ||
// When run via npm, we can leverage the injected environment variables to infer the import type | ||
const isTypeModule = process.env.npm_package_type === 'module'; | ||
const isModuleType = require('./is-module-type'); | ||
|
||
/** | ||
* 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) { | ||
return isTypeModule || filepath.endsWith('.mjs') | ||
module.exports = async function importFile(filepath) { | ||
return (await isModuleType(filepath)) | ||
? import(require('url').pathToFileURL(filepath)) | ||
: require(filepath); | ||
}; |
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,14 @@ | ||
const { readFile } = require('./fs'); | ||
|
||
module.exports = async function isModuleType(filepath) { | ||
if (process.env.npm_package_json) { | ||
// npm >= 7.0.0 | ||
const packageJson = JSON.parse( | ||
await readFile(process.env.npm_package_json, 'utf-8') | ||
); | ||
if (packageJson.type === 'module') { | ||
return true; | ||
} | ||
} | ||
return process.env.npm_package_type === 'module' || filepath.endsWith('.mjs'); | ||
}; |
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 |
---|---|---|
@@ -0,0 +1,51 @@ | ||
const path = require('path'); | ||
|
||
const { expect } = require('chai'); | ||
const isModuleType = require('../../../../lib/migrations/util/is-module-type.js'); | ||
require('../../../util/chai-setup'); | ||
|
||
describe('isModuleType', () => { | ||
let originalEnv = {}; | ||
|
||
before(() => { | ||
originalEnv = { ...process.env }; | ||
process.env = {}; | ||
}); | ||
|
||
after(() => { | ||
process.env = { ...originalEnv }; | ||
}); | ||
|
||
beforeEach(() => { | ||
delete process.env.npm_package_type; | ||
delete process.env.npm_package_json; | ||
}); | ||
|
||
it('should return true if the file is a .mjs file', async () => { | ||
expect(await isModuleType('test.mjs')).to.be.true; | ||
}); | ||
|
||
it('should return true if type=module with npm < 7.0.0', async () => { | ||
process.env.npm_package_type = 'module'; | ||
expect(await isModuleType('test.js')).to.be.true; | ||
}); | ||
|
||
it('should return false if type=commonjs with npm < 7.0.0', async () => { | ||
process.env.npm_package_type = 'commonjs'; | ||
expect(await isModuleType('test.js')).to.be.false; | ||
}); | ||
|
||
it('should return true if type=module with npm >= 7.0.0', async () => { | ||
process.env.npm_package_json = path.normalize( | ||
__dirname + '/test/package-module.json' | ||
); | ||
expect(await isModuleType('test.js')).to.be.true; | ||
}); | ||
|
||
it('should return false if type=commonjs with npm >= 7.0.0', async () => { | ||
process.env.npm_package_json = path.normalize( | ||
__dirname + '/test/package-commonjs.json' | ||
); | ||
expect(await isModuleType('test.js')).to.be.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,3 @@ | ||
{ | ||
"type": "commonjs" | ||
} |
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,3 @@ | ||
{ | ||
"type": "module" | ||
} |