Skip to content
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

ES-Module support #331

Closed
wants to merge 15 commits into from
Closed

ES-Module support #331

wants to merge 15 commits into from

Conversation

ctavan
Copy link
Member

@ctavan ctavan commented Sep 23, 2019

Discussion: #330

@ctavan
Copy link
Member Author

ctavan commented Oct 13, 2019

@43081j @DarylThayil since you have commented on #317: Would you mind testing out my development branch from this PR by installing the following package directly from github:

  "dependencies": {
    "uuid": "github:ctavan/uuid-esm#master"
  }

You can have a look at some examples of how this development version can be used with esmodules in https://github.com/kelektiv/node-uuid/tree/c1307d3fde47d2191bee36d5ea2f42310c63fef8/examples

Would be curious about your feedback!

@ctavan ctavan mentioned this pull request Oct 13, 2019
6 tasks
Copy link
Member

@broofa broofa left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the esm-* directories include CLI files (uuid-bin.* and bin dir)? esm-browser/bin/uuid doesn't really make sense.

Naming nit: Having a "src/lib" directory is a little weird. "src" and "lib" are typically peer directories. Not sure what this should get renamed to but... something. Maybe "src/common"? Or maybe just pull the "lib" files up into "src" at this point? The main reason those were in "lib" was so we could put just the top-tier dependencies (v1/v3/v4/v5.js) at the top level. But if all the JS is getting pushed down into "src", then just having a single dir is probably fine.

Not approving yet, as I think we need to resolve how we want to publish browser-esm .v node-esm .v node-commonjs versions of this library, and I need to actually do my day job right now. Just want to push these comments up, otherwise I'll just sit on this indefinitely. 😛

@@ -0,0 +1,3 @@
<!DOCTYPE html>
<title>UUID esmodule example</title>
<script type="module" src="example.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

'Should probably have some verbiage in the BODY to inform readers where the actual code output is. E.g. "Please open Developer Console to view output"... something like that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add explanation to the files.

How do you feel about adding these examples at all? Helpful or not?


console.log('uuidv1: ', uuidv1());


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Style nit: Avoid multiple newlines

Consider adding no-multiple-empty-lines to eslintrc.json?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would even consider to add prettier to get rid of any formatting discussion. However since this PR is already getting really big I wanted to push these kinds of tooling changes and refactorings to a different PR. Fine for you?

rm -rf "$DIR"
mkdir -p "$DIR"

# Traspile CommonJS versions of files
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Traspile CommonJS versions of files
# Transpile CommonJS versions of files

# Traspile CommonJS versions of files
env TARGET='commonjs' babel src --source-root src --out-dir "$DIR" --copy-files --quiet

# Traspile ESM versions of files for node
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# Traspile ESM versions of files for node
# Transpile ESM versions of files for node

Comment on lines 26 to 31
for FILE in "$DIR"/esm-browser/lib/*-browser.js
do
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for FILE in "$DIR"/esm-browser/lib/*-browser.js; do

Style nit: Prefer one line for declaring for-loops in Bash.

src/lib/v35.js Outdated
generateUUID.URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
// For CommonJS default export support
generateUUID.DNS = DNS;
generateUUID.URL = URL;
Copy link
Member

@broofa broofa Oct 14, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe have the v3/v5.js files explicitly export DNS, URL from './lib/v35.js' rather than bodging it in here? (Would that work?)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried that but it does not seem to work.

I'm using https://www.npmjs.com/package/babel-plugin-add-module-exports to add module.exports to the transpiled CommonJS modules so people can continue to do

const uuidv4 = require('uuid/v4');

instead of having to do ugly workarounds like

const uuidv4 = require('uuid/v4').default;

or

const { default: uuidv4 } = require('uuid/v4');

As soon as there is more than export default … this module.exports approach will no longer work.

Copy link
Member Author

@ctavan ctavan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@broofa thanks for the review, I will push changes as suggested.

Not approving yet, as I think we need to resolve how we want to publish browser-esm .v node-esm .v node-commonjs versions of this library, and I need to actually do my day job right now. Just want to push these comments up, otherwise I'll just sit on this indefinitely. 😛

As this PR has already grown almost too big to review I wanted to propose to create a new next branch from master and I would then post all my PRs against this next branch. That way we can take a bit more time and focus to flesh things out (also potentially publishing some prereleases) without putting master into a broken state.

I would then also make sure to keep PRs smaller so there's a more realistic chance for you to review.

Fine for you?

@ctavan ctavan mentioned this pull request Oct 14, 2019
@ctavan ctavan changed the title WIP ES-Module support ES-Module support Oct 15, 2019
@ctavan
Copy link
Member Author

ctavan commented Oct 15, 2019

@broofa since I pushed so many fixups I decided to squash all my fixups together again.

Quick summary of my changes:

  • moved the ./lib code up one level
  • as per ECMAScript Module / Browser Support #330 went for CommonJS-build for node and esm build for browsers (webpack/rollup/etc. through module: and browser: properties in package.json
  • as discussed in ECMAScript Module / Browser Support #330 decided not to change the module API at this point
  • instead, removed the deprecation for the default import and instead adjusted the examples to use const uuid = require('uuid'); again -> browser builds where module size matters should now make use of esm!
  • made v4 the first quickstart example to work against the above-the-fold-v1-bias

Here's what the contents of the npm tarball would look like: https://github.com/ctavan/uuid-esm (you can also check out the updated README there)

Feature-wise I would love to merge this state into a next branch and make a [email protected] prerelease.

I would then like to make some more tooling improvements (like publishing changelog to the Github releases page), maybe add browser tests (now that we have code specifically targeting the browser) and remove some outdated code like pre-node-4 Buffer code, but all of that in smaller, separate PRs.

@ctavan ctavan requested a review from broofa October 16, 2019 07:48
@broofa
Copy link
Member

broofa commented Oct 16, 2019

This all sounds good. I like the idea of a next branch. Given that this is a top-20 NPM module, letting this simmer for a bit so we get more eyeballs on it is probably wise. It might be worth adding a header to the README on master informing people about this branch so they can weigh in.

I'd like to keep whatever we're doing here in sync with https://github.com/tc39/proposal-uuid, whatever that means. At a minimum, try to have some agreement on how the two projects interrelate would be prudent. E.g. Do we treat this module as the "experimental" sandbox for whatever API(s) might come later there? Dunno.

Note that your work here supersedes much of my es6-modules branch. We should probably just delete that branch to avoid confusion. My only hesitatio there is that the UUID class has some useful code in it for parsing, validation, and working with byte arrays. Part of me wants to say we should incorporate that, but I'm not sure I can make a good enough argument for that.

I'm traveling this week and next, but I'll try to review again ASAP.

@ctavan
Copy link
Member Author

ctavan commented Oct 16, 2019

This all sounds good. I like the idea of a next branch. Given that this is a top-20 NPM module, letting this simmer for a bit so we get more eyeballs on it is probably wise. It might be worth adding a header to the README on master informing people about this branch so they can weigh in.

👍

I would need publish access for the npm module at some point though to publish 4.0.0-alpha prereleases.

Will make sure to update the master README.

I'd like to keep whatever we're doing here in sync with https://github.com/tc39/proposal-uuid, whatever that means. At a minimum, try to have some agreement on how the two projects interrelate would be prudent. E.g. Do we treat this module as the "experimental" sandbox for whatever API(s) might come later there? Dunno.

👍

However as discussed in #330 I will focus on ESModule interoperability and tooling changes for now and I will not introduce any API changes for the time being.

Should we decide to change the API of this module though, I totally agree that we should keep it in sync with https://github.com/tc39/proposal-uuid.

Note that your work here supersedes much of my es6-modules branch. We should probably just delete that branch to avoid confusion. My only hesitatio there is that the UUID class has some useful code in it for parsing, validation, and working with byte arrays. Part of me wants to say we should incorporate that, but I'm not sure I can make a good enough argument for that.

In fact I cherry-picked your commit from the other branch, I see value in leaving the other branch open for a while until we have made decisions around the API changes (which I intentionally left out here).

@ctavan ctavan mentioned this pull request Oct 16, 2019
@ctavan
Copy link
Member Author

ctavan commented Oct 16, 2019

Closing in favor of #337 337

@ctavan ctavan closed this Oct 16, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants