-
-
Notifications
You must be signed in to change notification settings - Fork 15
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
ESM support #113
ESM support #113
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It's great work, thanks for submitting! I wouldn't know how to assess the impact on the users although supporting more idioms is definitely welcome!
Since The only way to interop between cjs and esm is to use esm to |
@@ -2,6 +2,9 @@ | |||
"root": true, | |||
"extends": ["./node_modules/sanctuary-style/eslint-es3.json"], | |||
"env": {"node": true}, | |||
"globals": { | |||
"Promise": "readonly" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm using native Promises in the cjs version to align its output type with the mjs version. If you don't align these types, tooling tends to get confused, not to mention people.
// unlines :: Array String -> String | ||
exports.unlines = function(lines) { | ||
return lines.reduce (function(s, line) { return s + line + '\n'; }, ''); | ||
}; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize that common.js
might be a badly chosen name, seeing as it already means something within the very domain we're working with. I intended it to have "common functionality", which is where it gets the name.
{ | ||
"files": ["*.mjs"], | ||
"env": {"es6": true}, | ||
"parser": "babel-eslint" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need a special parser for our files, because import()
isn't an officially supported part of the language yet. The work around here is that we "pretend" to have written our source for Babel, while in reality we've written it for node --experimental-modules
.
test/index.js
Outdated
try { | ||
stdout = execSync (command, {encoding: 'utf8', stdio: 'pipe'}); | ||
stdout = execSync ( | ||
command + (noWarnings ? ' --nodejs --no-warnings' : ''), |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm passing --no-warnings
to the Node executable via the --nodejs
flag. This is to suppress the warning that Node gives when using --experimental-modules
, which causes stderr assertions to fail.
status: 1, | ||
stdout: '', | ||
stderr: unlines ([ | ||
'error: No files for doctesting provided' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Because the errors are now generated by the doctest
function, giving no files would mean seeing no errors. This error ensures that you'll still see what went wrong when no files are provided.
@davidchambers @danse I've marked the PR as ready for review. Since opening this PR, I've had to change the approach completely, but things appear to be working great now! I've already discussed some of the changes with David in Gitter. I'll paste some here:
This approach means that:
|
Fantastic work, Aldwin! |
2e8f940
to
e7a389f
Compare
README.md
Outdated
@@ -48,14 +48,13 @@ The exit code is 0 if all tests pass, 1 otherwise. | |||
|
|||
### AMD and CommonJS modules |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should generalize this heading.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
⚡
(I stopped doing amendments once I saw you push a commit.) |
@danse @davidchambers I'm very happy with this in its current form. Let me know if I can squash! :D |
Looking good! Please squash. :) |
This commit adds a new choice to the 'module' option: 'esm'. When running doctest with Node version 9 or up available, setting 'module' to 'esm' allows for the documentation comments to be embedded in ECMAScript modules, and use 'import' to load dependencies. The approach included in this commit has the following consequences: 1. The CLI transparently switches between ESM and non-ESM enabled based on the Node version running it. This means the CLI is fully backwards compatible. 2. The programmatic version also transparently switches between ESM and non-ESM depending on whether it's loaded via import or via require. 3. The programmatic version has a breaking change, in that its primary function returns a Promise now. Co-Authored-By: David Chambers <dc@davidchambers.me>
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🌳
thanks @Avaq for your contribution and great work you two, the project keeps developing and the code keeps getting cleaner :) |
Motivation
To close #99. I started this a long time ago, but never finished it. Today I spent a few minutes adding the finishing touches, although completely untested and written by hand.
Changes
Adds a new choice to the
module
option:esm
. When running doctest with Node version 9 or up available, settingmodule
toesm
allows for the documentation comments to be embedded in ECMAScript modules, and the usage ofimport
to load dependencies.The approach included in this commit has the following benefits and consequences:
import
or viarequire
.TODO