Skip to content

Commit

Permalink
feat: native ES Module support for Node.js
Browse files Browse the repository at this point in the history
  • Loading branch information
ctavan committed Mar 7, 2020
1 parent ee039ee commit 5e45431
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 1 deletion.
1 change: 1 addition & 0 deletions .local/wrapper.mjs
6 changes: 6 additions & 0 deletions examples/node-esmodules/README.md
@@ -0,0 +1,6 @@
# uuid example Node.js ESModules

```
npm install
npm test
```
43 changes: 43 additions & 0 deletions examples/node-esmodules/example.js
@@ -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));
11 changes: 11 additions & 0 deletions examples/node-esmodules/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions examples/node-esmodules/package.json
@@ -0,0 +1,12 @@
{
"name": "uuid-example-node-esmodules",
"version": "0.0.0",
"private": true,
"type": "module",
"scripts": {
"test": "node --experimental-modules example.js"
},
"dependencies": {
"uuid": "file:../../.local"
}
}
7 changes: 6 additions & 1 deletion package.json
Expand Up @@ -18,6 +18,10 @@
},
"sideEffects": false,
"main": "dist/index.js",
"exports": {
"require": "dist/index.js",
"import": "./wrapper.mjs"
},
"module": "dist/esm-node/index.js",
"browser": {
"./dist/md5.js": "./dist/md5-browser.js",
Expand All @@ -34,7 +38,8 @@
"v1.js",
"v3.js",
"v4.js",
"v5.js"
"v5.js",
"wrapper.mjs"
],
"devDependencies": {
"@babel/cli": "7.8.4",
Expand Down
5 changes: 5 additions & 0 deletions wrapper.mjs
@@ -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;

0 comments on commit 5e45431

Please sign in to comment.