Skip to content

Commit

Permalink
fix: clean up esm builds for node and browser (#383)
Browse files Browse the repository at this point in the history
- add example for usage with node and webpack
- add specific babel config for node esm build
- fix path in native browser esmodule example
  • Loading branch information
ctavan committed Feb 25, 2020
1 parent 4344a22 commit 59e6a49
Show file tree
Hide file tree
Showing 11 changed files with 4,193 additions and 6 deletions.
5 changes: 4 additions & 1 deletion .babelrc.js
Expand Up @@ -6,8 +6,11 @@ module.exports = {
plugins: ['babel-plugin-add-module-exports'],
presets: [['@babel/preset-env', { targets: { node: '8' }, modules: 'commonjs' }]],
},
esm: {
esmBrowser: {
presets: [['@babel/preset-env', { modules: false }]],
},
esmNode: {
presets: [['@babel/preset-env', { targets: { node: '8' }, modules: false }]],
},
},
};
4 changes: 2 additions & 2 deletions examples/browser-esmodules/example.js
Expand Up @@ -3,8 +3,8 @@ import {
v4 as uuidv4,
v3 as uuidv3,
v5 as uuidv5,
} from './node_modules/uuid/esm-browser/index.js';
import * as uuid from './node_modules/uuid/esm-browser/index.js';
} from './node_modules/uuid/dist/esm-browser/index.js';
import * as uuid from './node_modules/uuid/dist/esm-browser/index.js';

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

Expand Down
1 change: 1 addition & 0 deletions examples/node-webpack/.gitignore
@@ -0,0 +1 @@
dist/
8 changes: 8 additions & 0 deletions examples/node-webpack/README.md
@@ -0,0 +1,8 @@
# uuid example Node.js with Webpack

```
npm install
npm test
```

This will run webpack and execute the resulting bundles in `./dist`.
42 changes: 42 additions & 0 deletions examples/node-webpack/example-all.js
@@ -0,0 +1,42 @@
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));
3 changes: 3 additions & 0 deletions examples/node-webpack/example-v1.js
@@ -0,0 +1,3 @@
import { v1 as uuidv1 } from 'uuid';

console.log('uuidv1()', uuidv1());
3 changes: 3 additions & 0 deletions examples/node-webpack/example-v4.js
@@ -0,0 +1,3 @@
import { v4 as uuidv4 } from 'uuid';

console.log('uuidv4()', uuidv4());

0 comments on commit 59e6a49

Please sign in to comment.