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
Export Sinon as an ES module #1809
Conversation
See sinonjs#1805 Not functional bundle. Gets this error: $ node -r esm -e 'import sinon from ./bundle-esm' file:///home/carlerik/dev/sinon/bundle-esm.js:1 import typeDetect from 'type-detect'; SyntaxError: Missing export 'default' in ES module: file:///home/carlerik/dev/sinon/node_modules/diff/lib/index.js at Object.<anonymous> (file:///home/carlerik/dev/sinon/bundle-esm.js:1)
rollup.config.js
Outdated
input: "./lib/sinon.js", | ||
output: { | ||
file: "pkg/bundle-esm.js", | ||
format: "esm" |
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.
Just taking a quick look before getting a little time to test, but should the format be "es"? I don't see "esm" as an alias for format at https://rollupjs.org/guide/en
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.
Worked accidentally, I guess 😄 Still produced an EcmaScript module with export default sinon
. Fixed in later commit. Thanks for noticing.
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.
What I suspect perhaps will cause trouble is the use of the os
module ...
(!) Unresolved dependencies
https://github.com/rollup/rollup/wiki/Troubleshooting#treating-module-as-external-dependency
os (imported by node_modules/supports-color/index.js, commonjs-external:os)
Not totally sure how to automate the last step of verifying in the browser, but I have made a manual test that shows we're not quite there yet ... I added a <script type="module">
import sinon from '/sinon-esm.js';
console.log('sinon is here', typeof sinon);
</script> I then fired up a webserver on port 8000, and pointed chrome to
I guess this is due to the if (!hasNextTick && !hasSetImmediate) {
module.exports = nextTick;
} |
The modules are currently not possible to statically convert to esm. through some minimally invasive changes I think we should be able to make it work, though by hiding the runtime detection of what is exported behind a getter. Not doing this today, and for what I know, some of that logic might be in external dependencies (would have to manually inspect - no time today). If people could delve into this, it would be great. |
This is an exciting PR to follow! |
For some reason - and I have no idea why - the bug with the unresolved export seems to have vanished. I have tried the bundle in both Chrome and Firefox. My only "problem" is that I haven't been able to automatically create a verification step, as I can't find a way of making Puppeteer verify that a test script has been run. But manual inspection seems to indicate this is working. |
That is what I am seeing too:
|
That my non-working script didn't work was already established 😆 Everything seems to work when testing with a browser, though, so in a way it's complete, but I am not comfortable merging this until we have an automatic verification step. I resorted to trying StackOverflow ... Let's see how that pans out. |
@fatso83 Can you share the test file you're using? It's not in this PR ... or am I overlooking something? |
@mantoni I didn't commit the test file, as it wasn't working, but it's the gist I linked to. It should pretty much just be
|
@fatso83 Ah, ok. Maybe you want to try |
@mantoni That wouldn't work, AFAIK (and I already tried). ES Modules are async and non-blocking. If you evaluate immediately after loading the page the script hasn't had time to load the Sinon module. Also, I started out with an |
Yeah, I evaluate the script synchronously and then monitor the |
@mantoni Thanks for the tip on I have now implemented a check that does a simple test of the stubbing functionality. It works and builds as expected, meaning this should be ready for review 😃 |
These tests are failing in older node |
Already included in the 'lint' script. No need to run twice.
💥 ✔️ Travis builds passing |
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.
👍
Purpose
Implements #1805 by using Rollup to export the common js bundle. Enables Sinon to be used in
<script type="module>
blocks without polluting the global namespace.How to verify - mandatory
npm link
cd /tmp; mkdir foo; cd foo; npm install esm; npm link sinon;
node -r esm -e 'import sinon from "sinon"; console.log("sinon.stub()", sinon.stub().returns(42)())'
sinon.stub() 42
Checklist for author
npm run lint
passesOne might wonder if there is a need for having both browserify and rollup, but IMHO it adds little maintenance weight. We could get rid of one of them, but perhaps not in this PR.