-
-
Notifications
You must be signed in to change notification settings - Fork 903
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: native Node.js ES Modules (wrapper approach)
BREAKING CHANGE: Native ES Modules is still an experimental API in Node.js 14.0.0 and has so far not officially been supported by the `uuid` module. Since Node.js allows importing CommonJS modules it was possible to import the `uuid` module like this: ```js import uuid from 'uuid'; console.log(uuid.v4()); // -> 'cd6c3b08-0adc-4f4b-a6ef-36087a1c9869' ``` This will no longer work with proper ES Module exports in place. You can now import the `uuid` library as described in the documentation: ```js import { v4 as uuidv4 } from 'uuid'; uuidv4(); // ⇨ '9b1deb4d-3b7d-4bad-9bdd-2b0d7b3dcb6d' ``` or ```js import * as uuid from 'uuid'; console.log(uuid.v4()); // -> 'cd6c3b08-0adc-4f4b-a6ef-36087a1c9869' ``` Enabling native ES Modules for Node.js requires some special care for the v1 algorithm which needs internal state. This makes this library susceptible to the dual package hazard described in https://nodejs.org/docs/latest-v14.x/api/esm.html#esm_dual_commonjs_es_module_packages While the "isolated state" solution seems to make more sense it causes trouble with rollup which supports CommonJS files only with an additional plugin, see rollup/rollup#3514. It is worth noting that webpack could deal with the "isolated state" solution since webpack supports CommonJS sources out of the box without further plugins and also doesn't get confused by `.cjs` file extensions that would have to be used in the state isolation approach for compatibility with Node.js. The wrapper approach should however work fine. Here's what code will be used in each case: 1. Node.js `require('uuid')` -> dist/index.js (CommonJS) -> dist/v1.js (CommonJS) 2. Node.js `import { v1 as uuidv1 } from 'uuid'` -> wrapper.mjs (ESM) -> dist/v1.js (CommonJS) 3. rollup/webpack (targeting Node.js environments) -> dist/esm-node/index.js (ESM) -> dist/esm-node/v1.js (ESM) 4. rollup/webpack (targeting Browser environments) -> dist/esm-browser/index.js (ESM) -> dist/esm-browser/v1.js (ESM)
- Loading branch information
Showing
10 changed files
with
105 additions
and
22 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
../wrapper.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
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,6 @@ | ||
# uuid example Node.js ESModules | ||
|
||
``` | ||
npm install | ||
npm test | ||
``` |
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,43 @@ | ||
import { v1 as uuidv1, v4 as uuidv4, v3 as uuidv3, v5 as uuidv5 } from 'uuid'; | ||
import * as uuid from 'uuid'; | ||
|
||
console.log('uuidv1()', uuidv1()); | ||
|
||
console.log('uuidv4()', uuidv4()); | ||
|
||
// ... using predefined DNS namespace (for domain names) | ||
console.log('uuidv3() DNS', uuidv3('hello.example.com', uuidv3.DNS)); | ||
|
||
// ... using predefined URL namespace (for, well, URLs) | ||
console.log('uuidv3() URL', uuidv3('http://example.com/hello', uuidv3.URL)); | ||
|
||
// ... using a custom namespace | ||
// | ||
// Note: Custom namespaces should be a UUID string specific to your application! | ||
// E.g. the one here was generated using this modules `uuid` CLI. | ||
const MY_NAMESPACE = '55238d15-c926-4598-b49d-cf4e913ba13c'; | ||
console.log('uuidv3() MY_NAMESPACE', uuidv3('Hello, World!', MY_NAMESPACE)); | ||
|
||
// ... using predefined DNS namespace (for domain names) | ||
console.log('uuidv5() DNS', uuidv5('hello.example.com', uuidv5.DNS)); | ||
|
||
// ... using predefined URL namespace (for, well, URLs) | ||
console.log('uuidv5() URL', uuidv5('http://example.com/hello', uuidv5.URL)); | ||
|
||
// ... using a custom namespace | ||
// | ||
// Note: Custom namespaces should be a UUID string specific to your application! | ||
// E.g. the one here was generated using this modules `uuid` CLI. | ||
// const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341'; | ||
console.log('uuidv5() MY_NAMESPACE', uuidv5('Hello, World!', MY_NAMESPACE)); | ||
|
||
console.log('Same with default export'); | ||
|
||
console.log('uuid.v1()', uuid.v1()); | ||
console.log('uuid.v4()', uuid.v4()); | ||
console.log('uuid.v3() DNS', uuid.v3('hello.example.com', uuid.v3.DNS)); | ||
console.log('uuid.v3() URL', uuid.v3('http://example.com/hello', uuid.v3.URL)); | ||
console.log('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE)); | ||
console.log('uuid.v5() DNS', uuid.v5('hello.example.com', uuid.v5.DNS)); | ||
console.log('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL)); | ||
console.log('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE)); |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,11 @@ | ||
{ | ||
"name": "uuid-example-node-esmodules", | ||
"version": "0.0.0", | ||
"private": true, | ||
"scripts": { | ||
"test": "node --experimental-modules example.mjs" | ||
}, | ||
"dependencies": { | ||
"uuid": "file:../../.local" | ||
} | ||
} |
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,5 @@ | ||
import uuid from './dist/index.js'; | ||
export const v1 = uuid.v1; | ||
export const v3 = uuid.v3; | ||
export const v4 = uuid.v4; | ||
export const v5 = uuid.v5; |