-
-
Notifications
You must be signed in to change notification settings - Fork 306
Use import() on esm files in supported node versions #547
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,13 @@ | ||
'use strict'; | ||
|
||
const { extname: extnamePath } = require('path'); | ||
const getPackageType = require('get-package-type'); | ||
|
||
module.exports = function (file) { | ||
const ext = extnamePath(file); | ||
|
||
if (ext === '.mjs' || (ext === '.js' && getPackageType.sync(file) === 'module')) { | ||
return import(file); | ||
} | ||
require(file); | ||
}; |
This file contains hidden or 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 hidden or 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 hidden or 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 |
---|---|---|
|
@@ -27,8 +27,10 @@ | |
"defined": "^1.0.0", | ||
"dotignore": "^0.1.2", | ||
"for-each": "^0.3.3", | ||
"get-package-type": "^0.1.0", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I’m a bit concerned that this package’s engines declaration will cause false errors for yarn users, and anyone using engines-strict or https://npmjs.com/ls-engines, if it ever adds an engines declaration in the future (cc @coreyfarrell) |
||
"glob": "^7.1.7", | ||
"has": "^1.0.3", | ||
"has-dynamic-import": "^2.0.0", | ||
"inherits": "^2.0.4", | ||
"is-regex": "^1.1.3", | ||
"minimist": "^1.2.5", | ||
|
@@ -41,6 +43,7 @@ | |
"through": "^2.3.8" | ||
}, | ||
"devDependencies": { | ||
"@ljharb/eslint-config": "^17.6.0", | ||
"array.prototype.flatmap": "^1.2.4", | ||
"aud": "^1.1.5", | ||
"concat-stream": "^1.6.2", | ||
|
@@ -57,7 +60,7 @@ | |
"prepublishOnly": "safe-publish-latest", | ||
"prepublish": "!(type not-in-publish) || not-in-publish || npm run prepublishOnly", | ||
"prelint": "eclint check", | ||
"lint": "eslint . bin/*", | ||
"lint": "eslint --ext .js,.cjs,.mjs . bin/*", | ||
"pretest": "npm run lint", | ||
"test": "npm run tests-only", | ||
"posttest": "aud --production", | ||
|
This file contains hidden or 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,198 @@ | ||
'use strict'; | ||
|
||
var tap = require('tap'); | ||
var spawn = require('child_process').spawn; | ||
var concat = require('concat-stream'); | ||
var hasDynamicImport = require('has-dynamic-import'); | ||
var assign = require('object.assign'); | ||
|
||
tap.test('importing mjs files', function (t) { | ||
hasDynamicImport().then(function (hasSupport) { | ||
if (hasSupport) { | ||
var tc = function (rows) { | ||
t.same(rows.toString('utf8'), [ | ||
'TAP version 13', | ||
'# mjs-a', | ||
'ok 1 test ran', | ||
'# mjs-b', | ||
'ok 2 test ran after mjs-a', | ||
'# mjs-c', | ||
'ok 3 test ran after mjs-b', | ||
'# mjs-d', | ||
'ok 4 test ran after mjs-c', | ||
'# mjs-e', | ||
'ok 5 test ran after mjs-d', | ||
'# mjs-f', | ||
'ok 6 test ran after mjs-e', | ||
'# mjs-g', | ||
'ok 7 test ran after mjs-f', | ||
'# mjs-h', | ||
'ok 8 test ran after mjs-g', | ||
'', | ||
'1..8', | ||
'# tests 8', | ||
'# pass 8', | ||
'', | ||
'# ok' | ||
].join('\n') + '\n\n'); | ||
}; | ||
|
||
var ps = tape('import/mjs-*.mjs'); | ||
ps.stdout.pipe(concat(tc)); | ||
ps.stderr.pipe(process.stderr); | ||
ps.on('exit', function (code) { | ||
t.equal(code, 0); | ||
t.end(); | ||
}); | ||
} else { | ||
t.pass('does not support dynamic import'); | ||
t.end(); | ||
} | ||
}); | ||
}); | ||
|
||
tap.test('importing type: "module" files', function (t) { | ||
hasDynamicImport().then(function (hasSupport) { | ||
if (hasSupport) { | ||
var tc = function (rows) { | ||
t.same(rows.toString('utf8'), [ | ||
'TAP version 13', | ||
'# package-type-a', | ||
'ok 1 test ran', | ||
'# package-type-b', | ||
'ok 2 test ran after package-type-a', | ||
'# package-type-c', | ||
'ok 3 test ran after package-type-b', | ||
'', | ||
'1..3', | ||
'# tests 3', | ||
'# pass 3', | ||
'', | ||
'# ok' | ||
].join('\n') + '\n\n'); | ||
}; | ||
|
||
var ps = tape('import/package_type/*.js'); | ||
ps.stdout.pipe(concat(tc)); | ||
ps.stderr.pipe(process.stderr); | ||
ps.on('exit', function (code) { | ||
t.equal(code, 0); | ||
t.end(); | ||
}); | ||
} else { | ||
t.pass('does not support dynamic import'); | ||
t.end(); | ||
} | ||
}); | ||
}); | ||
|
||
tap.test('errors importing test files', function (t) { | ||
hasDynamicImport().then(function (hasSupport) { | ||
var createTest = function (options) { | ||
var message = options.error + ' in `' + options.mode + '` mode`'; | ||
var ps = tape(options.files, { env: { NODE_OPTIONS: '--unhandled-rejections=' + options.mode } }); | ||
ps.stderr.pipe(concat(options.unhandledRejection(message))); | ||
ps.on('exit', function (code, sig) { | ||
t.equal(code, options.exitCode, message + ' has exit code ' + options.exitCode); | ||
}); | ||
}; | ||
|
||
var warning = function (message) { | ||
return function (rows) { | ||
t.match(rows.toString('utf8'), 'UnhandledPromiseRejectionWarning', 'should have unhandled rejection warning: ' + message); | ||
}; | ||
}; | ||
|
||
var noWarning = function (message) { | ||
return function (rows) { | ||
t.notMatch(rows.toString('utf8'), 'UnhandledPromiseRejectionWarning', 'should not have unhandled rejection warning: ' + message); | ||
}; | ||
}; | ||
|
||
if (hasSupport) { | ||
var tests = [{ | ||
files: 'import/syntax-error.mjs import/mjs-a.mjs import/mjs-b.mjs', | ||
error: 'syntax errors in first imported esm file', | ||
mode: 'warn', | ||
exitCode: 0, | ||
unhandledRejection: warning | ||
}, { | ||
files: 'import/throws.mjs import/mjs-a.mjs import/mjs-b.mjs', | ||
error: 'thrown errors in first imported esm file', | ||
mode: 'warn', | ||
exitCode: 0, | ||
unhandledRejection: warning | ||
}, { | ||
files: 'import/mjs-a.mjs import/syntax-error.mjs', | ||
error: 'syntax error in esm file', | ||
mode: 'warn', | ||
exitCode: 1, | ||
unhandledRejection: warning | ||
}, { | ||
files: 'import/syntax-error.mjs', | ||
error: 'syntax error in esm file', | ||
mode: 'strict', | ||
exitCode: 1, | ||
unhandledRejection: noWarning | ||
}, { | ||
files: 'import/throws.mjs', | ||
error: 'thrown error in esm file', | ||
mode: 'strict', | ||
exitCode: 1, | ||
unhandledRejection: noWarning | ||
}, { | ||
files: 'import/syntax-error.cjs', | ||
error: 'syntax error in cjs file', | ||
mode: 'warn', | ||
exitCode: 1, | ||
unhandledRejection: noWarning | ||
}, { | ||
files: 'import/throws.cjs', | ||
error: 'thrown error in cjs file', | ||
mode: 'warn', | ||
exitCode: 1, | ||
unhandledRejection: noWarning | ||
}, { | ||
files: 'import/syntax-error.cjs', | ||
error: 'syntax error in cjs file', | ||
mode: 'strict', | ||
exitCode: 1, | ||
unhandledRejection: noWarning | ||
}, { | ||
files: 'import/throws.cjs', | ||
error: 'thrown error in cjs file', | ||
mode: 'strict', | ||
exitCode: 1, | ||
unhandledRejection: noWarning | ||
}, { | ||
files: 'import/mjs-a.mjs import/syntax-error.cjs', | ||
error: 'syntax error in cjs file in loading promise', | ||
mode: 'warn', | ||
exitCode: 1, | ||
unhandledRejection: warning | ||
}, { | ||
files: 'import/mjs-a.mjs import/syntax-error.cjs', | ||
error: 'syntax error in cjs file in loading promise', | ||
mode: 'strict', | ||
exitCode: 1, | ||
unhandledRejection: noWarning | ||
}]; | ||
|
||
t.plan(tests.length * 2); | ||
|
||
tests.map(createTest); | ||
} else { | ||
t.pass('does not support dynamic import'); | ||
t.end(); | ||
} | ||
}); | ||
}); | ||
|
||
function tape(args, options) { | ||
options = assign({ cwd: __dirname }, options); | ||
|
||
var proc = require('child_process'); | ||
var bin = __dirname + '/../bin/tape'; | ||
|
||
return proc.spawn('node', [bin].concat(args.split(' ')), options); | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.