Skip to content

Commit 59e6a49

Browse files
authored
fix: clean up esm builds for node and browser (#383)
- add example for usage with node and webpack - add specific babel config for node esm build - fix path in native browser esmodule example
1 parent 4344a22 commit 59e6a49

File tree

11 files changed

+4193
-6
lines changed

11 files changed

+4193
-6
lines changed

.babelrc.js

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,11 @@ module.exports = {
66
plugins: ['babel-plugin-add-module-exports'],
77
presets: [['@babel/preset-env', { targets: { node: '8' }, modules: 'commonjs' }]],
88
},
9-
esm: {
9+
esmBrowser: {
1010
presets: [['@babel/preset-env', { modules: false }]],
1111
},
12+
esmNode: {
13+
presets: [['@babel/preset-env', { targets: { node: '8' }, modules: false }]],
14+
},
1215
},
1316
};

examples/browser-esmodules/example.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@ import {
33
v4 as uuidv4,
44
v3 as uuidv3,
55
v5 as uuidv5,
6-
} from './node_modules/uuid/esm-browser/index.js';
7-
import * as uuid from './node_modules/uuid/esm-browser/index.js';
6+
} from './node_modules/uuid/dist/esm-browser/index.js';
7+
import * as uuid from './node_modules/uuid/dist/esm-browser/index.js';
88

99
console.log('uuidv1()', uuidv1());
1010

examples/node-webpack/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
dist/

examples/node-webpack/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# uuid example Node.js with Webpack
2+
3+
```
4+
npm install
5+
npm test
6+
```
7+
8+
This will run webpack and execute the resulting bundles in `./dist`.
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
import { v1 as uuidv1, v4 as uuidv4, v3 as uuidv3, v5 as uuidv5 } from 'uuid';
2+
import * as uuid from 'uuid';
3+
4+
console.log('uuidv1()', uuidv1());
5+
console.log('uuidv4()', uuidv4());
6+
7+
// ... using predefined DNS namespace (for domain names)
8+
console.log('uuidv3() DNS', uuidv3('hello.example.com', uuidv3.DNS));
9+
10+
// ... using predefined URL namespace (for, well, URLs)
11+
console.log('uuidv3() URL', uuidv3('http://example.com/hello', uuidv3.URL));
12+
13+
// ... using a custom namespace
14+
//
15+
// Note: Custom namespaces should be a UUID string specific to your application!
16+
// E.g. the one here was generated using this modules `uuid` CLI.
17+
const MY_NAMESPACE = '55238d15-c926-4598-b49d-cf4e913ba13c';
18+
console.log('uuidv3() MY_NAMESPACE', uuidv3('Hello, World!', MY_NAMESPACE));
19+
20+
// ... using predefined DNS namespace (for domain names)
21+
console.log('uuidv5() DNS', uuidv5('hello.example.com', uuidv5.DNS));
22+
23+
// ... using predefined URL namespace (for, well, URLs)
24+
console.log('uuidv5() URL', uuidv5('http://example.com/hello', uuidv5.URL));
25+
26+
// ... using a custom namespace
27+
//
28+
// Note: Custom namespaces should be a UUID string specific to your application!
29+
// E.g. the one here was generated using this modules `uuid` CLI.
30+
// const MY_NAMESPACE = '1b671a64-40d5-491e-99b0-da01ff1f3341';
31+
console.log('uuidv5() MY_NAMESPACE', uuidv5('Hello, World!', MY_NAMESPACE));
32+
33+
console.log('Same with default export');
34+
35+
console.log('uuid.v1()', uuid.v1());
36+
console.log('uuid.v4()', uuid.v4());
37+
console.log('uuid.v3() DNS', uuid.v3('hello.example.com', uuid.v3.DNS));
38+
console.log('uuid.v3() URL', uuid.v3('http://example.com/hello', uuid.v3.URL));
39+
console.log('uuid.v3() MY_NAMESPACE', uuid.v3('Hello, World!', MY_NAMESPACE));
40+
console.log('uuid.v5() DNS', uuid.v5('hello.example.com', uuid.v5.DNS));
41+
console.log('uuid.v5() URL', uuid.v5('http://example.com/hello', uuid.v5.URL));
42+
console.log('uuid.v5() MY_NAMESPACE', uuid.v5('Hello, World!', MY_NAMESPACE));
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { v1 as uuidv1 } from 'uuid';
2+
3+
console.log('uuidv1()', uuidv1());
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { v4 as uuidv4 } from 'uuid';
2+
3+
console.log('uuidv4()', uuidv4());

0 commit comments

Comments
 (0)