Skip to content

Commit a3f078f

Browse files
broofactavan
authored andcommittedOct 25, 2019
feat: hybrid CommonJS & ECMAScript modules build
BREAKING CHANGE: Convert code base to ECMAScript Modules (ESM) and release CommonJS build for node and ESM build for browser bundlers.
1 parent 60eb84a commit a3f078f

29 files changed

+4556
-1544
lines changed
 

‎.babelrc.js

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
module.exports = {
2+
presets: [],
3+
plugins: [],
4+
env: {
5+
commonjs: {
6+
plugins: [
7+
'babel-plugin-add-module-exports',
8+
],
9+
presets: [
10+
['@babel/preset-env', {targets: {node: '8'}, modules: 'commonjs'}],
11+
],
12+
},
13+
esm: {
14+
presets: [
15+
['@babel/preset-env', {modules: false}],
16+
],
17+
},
18+
},
19+
};

‎.eslintignore

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
!.babelrc.js

‎.eslintrc.json

+2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
"env": {
44
"browser": true,
55
"commonjs": true,
6+
"es6": true,
67
"mocha": true,
78
"node": true
89
},
910
"extends": ["eslint:recommended"],
1011
"globals": {
1112
"msCrypto": true
1213
},
14+
"parser": "babel-eslint",
1315
"rules": {
1416
"array-bracket-spacing": ["warn", "never"],
1517
"arrow-body-style": ["warn", "as-needed"],

‎.gitignore

+2
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,5 @@
22
.idea/**
33
.DS_Store
44
node_modules
5+
tmp
6+
dist/

‎README_js.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
```javascript --hide
22
runmd.onRequire = path => {
33
if (path == 'rng') return fun
4-
return path.replace(/^uuid/, './');
4+
return path.replace(/^uuid/, './dist/');
55
}
66

77
// Shim Date and crypto so generated ids are consistent across doc revisions

‎index.js

-8
This file was deleted.

‎lib/rng.js

-8
This file was deleted.

‎package-lock.json

+4,209-1,241
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎package.json

+12-7
Original file line numberDiff line numberDiff line change
@@ -16,27 +16,32 @@
1616
"bin": {
1717
"uuid": "./bin/uuid"
1818
},
19+
"sideEffects": false,
20+
"main": "index.js",
21+
"module": "esm-browser/index.js",
1922
"devDependencies": {
23+
"@babel/cli": "~7.6.0",
24+
"@babel/core": "~7.6.0",
25+
"@babel/preset-env": "~7.6.3",
2026
"@commitlint/cli": "~8.2.0",
2127
"@commitlint/config-conventional": "~8.2.0",
28+
"babel-eslint": "~10.0.3",
29+
"babel-plugin-add-module-exports": "~1.0.2",
2230
"eslint": "~6.4.0",
31+
"esm": "~3.2.25",
2332
"husky": "~3.0.5",
2433
"mocha": "6.2.0",
2534
"runmd": "1.2.1",
2635
"standard-version": "7.0.0"
2736
},
2837
"scripts": {
29-
"lint": "eslint .",
30-
"test": "npm run lint && mocha test/test.js",
38+
"lint": "eslint src/ test/ *.js",
39+
"test": "npm run lint && mocha --require esm --reporter spec --check-leaks test/test.js",
3140
"md": "runmd --watch --output=README.md README_js.md",
41+
"package": "./scripts/package.sh",
3242
"release": "standard-version",
3343
"prepare": "runmd --output=README.md README_js.md"
3444
},
35-
"browser": {
36-
"./lib/rng.js": "./lib/rng-browser.js",
37-
"./lib/sha1.js": "./lib/sha1-browser.js",
38-
"./lib/md5.js": "./lib/md5-browser.js"
39-
},
4045
"repository": {
4146
"type": "git",
4247
"url": "https://github.com/kelektiv/node-uuid.git"

‎scripts/package.sh

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
#!/bin/bash -eu
2+
3+
# Mostly taken from:
4+
# https://github.com/date-fns/date-fns/blob/master/scripts/build/package.sh
5+
# and
6+
# https://medium.com/@iamstan/tips-for-writing-es-modules-in-node-js-96ec688615a4
7+
8+
# cd to the root dir
9+
ROOT="$(pwd)/$(dirname "$0")/.."
10+
cd "$ROOT" || exit 1
11+
12+
PATH="$(npm bin):$PATH"
13+
# XXX: $PACKAGE_OUTPUT_PATH must be an absolute path!
14+
DIR=${PACKAGE_OUTPUT_PATH:-"$ROOT/dist"}
15+
16+
# Clean up output dir
17+
rm -rf "$DIR"
18+
mkdir -p "$DIR"
19+
20+
# Transpile CommonJS versions of files
21+
babel --env-name commonjs src --source-root src --out-dir "$DIR" --copy-files --quiet
22+
23+
# Transpile ESM versions of files for the browser
24+
babel --env-name esm src --source-root src --out-dir "$DIR/esm-browser" --copy-files --quiet
25+
26+
# No need to have the CLI files in the esm build
27+
rm -rf "$DIR/esm-browser/bin"
28+
rm -rf "$DIR/esm-browser/uuid-bin.js"
29+
30+
for FILE in "$DIR"/esm-browser/*-browser.js
31+
do
32+
echo "Replacing node-specific file for esm-browser: $FILE"
33+
mv "$FILE" "${FILE/-browser.js/.js}"
34+
done
35+
36+
echo "Removing browser-specific files from esm-node"
37+
rm -f "$DIR"/*-browser.js
38+
39+
# Copy basic files
40+
for PATTERN in package.json \
41+
CHANGELOG.md \
42+
LICENSE.md \
43+
README.md
44+
do
45+
cp -r "$PATTERN" "$DIR"
46+
done

‎src/bin/uuid

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/usr/bin/env node
2+
require('../uuid-bin');

‎lib/bytesToUuid.js ‎src/bytesToUuid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,4 @@ function bytesToUuid(buf, offset) {
2323
]).join('');
2424
}
2525

26-
module.exports = bytesToUuid;
26+
export default bytesToUuid;

‎src/index.js

+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
export {default as v1} from './v1.js';
2+
export {default as v3} from './v3.js';
3+
export {default as v4} from './v4.js';
4+
export {default as v5} from './v5.js';

‎lib/md5-browser.js ‎src/md5-browser.js

+1-4
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,6 @@
1818
* Distributed under the BSD License
1919
* See http://pajhome.org.uk/crypt/md5 for more info.
2020
*/
21-
22-
'use strict';
23-
2421
function md5(bytes) {
2522
if (typeof(bytes) == 'string') {
2623
var msg = unescape(encodeURIComponent(bytes)); // UTF8 escape
@@ -213,4 +210,4 @@ function md5ii(a, b, c, d, x, s, t) {
213210
return md5cmn(c ^ (b | (~d)), a, b, x, s, t);
214211
}
215212

216-
module.exports = md5;
213+
export default md5;

‎lib/md5.js ‎src/md5.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
var crypto = require('crypto');
1+
import crypto from 'crypto';
42

53
function md5(bytes) {
64
if (typeof Buffer.from === 'function') {
@@ -22,4 +20,4 @@ function md5(bytes) {
2220
return crypto.createHash('md5').update(bytes).digest();
2321
}
2422

25-
module.exports = md5;
23+
export default md5;

‎lib/rng-browser.js ‎src/rng-browser.js

+6-2
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,13 @@
88
var getRandomValues = (typeof(crypto) != 'undefined' && crypto.getRandomValues && crypto.getRandomValues.bind(crypto)) ||
99
(typeof(msCrypto) != 'undefined' && typeof window.msCrypto.getRandomValues == 'function' && msCrypto.getRandomValues.bind(msCrypto));
1010

11+
let rng;
12+
1113
if (getRandomValues) {
1214
// WHATWG crypto RNG - http://wiki.whatwg.org/wiki/Crypto
1315
var rnds8 = new Uint8Array(16); // eslint-disable-line no-undef
1416

15-
module.exports = function whatwgRNG() {
17+
rng = function whatwgRNG() {
1618
getRandomValues(rnds8);
1719
return rnds8;
1820
};
@@ -23,7 +25,7 @@ if (getRandomValues) {
2325
// quality.
2426
var rnds = new Array(16);
2527

26-
module.exports = function mathRNG() {
28+
rng = function mathRNG() {
2729
for (var i = 0, r; i < 16; i++) {
2830
if ((i & 0x03) === 0) r = Math.random() * 0x100000000;
2931
rnds[i] = r >>> ((i & 0x03) << 3) & 0xff;
@@ -32,3 +34,5 @@ if (getRandomValues) {
3234
return rnds;
3335
};
3436
}
37+
38+
export default rng;

‎src/rng.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import crypto from 'crypto';
2+
3+
export default function nodeRNG() {
4+
return crypto.randomBytes(16);
5+
}

‎lib/sha1-browser.js ‎src/sha1-browser.js

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
// Adapted from Chris Veness' SHA1 code at
22
// http://www.movable-type.co.uk/scripts/sha1.html
3-
'use strict';
4-
53
function f(s, x, y, z) {
64
switch (s) {
75
case 0: return (x & y) ^ (~x & z);
@@ -86,4 +84,4 @@ function sha1(bytes) {
8684
];
8785
}
8886

89-
module.exports = sha1;
87+
export default sha1;

‎lib/sha1.js ‎src/sha1.js

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
'use strict';
2-
3-
var crypto = require('crypto');
1+
import crypto from 'crypto';
42

53
function sha1(bytes) {
64
if (typeof Buffer.from === 'function') {
@@ -22,4 +20,4 @@ function sha1(bytes) {
2220
return crypto.createHash('sha1').update(bytes).digest();
2321
}
2422

25-
module.exports = sha1;
23+
export default sha1;

‎bin/uuid ‎src/uuid-bin.js

+14-16
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
1-
#!/usr/bin/env node
2-
var assert = require('assert');
1+
import assert from 'assert';
2+
3+
import v1 from './v1.js';
4+
import v3 from './v3.js';
5+
import v4 from './v4.js';
6+
import v5 from './v5.js';
37

48
function usage() {
59
console.log('Usage:');
@@ -22,41 +26,35 @@ var version = args.shift() || 'v4';
2226

2327
switch (version) {
2428
case 'v1':
25-
var uuidV1 = require('../v1');
26-
console.log(uuidV1());
29+
console.log(v1());
2730
break;
2831

2932
case 'v3':
30-
var uuidV3 = require('../v3');
31-
3233
var name = args.shift();
3334
var namespace = args.shift();
3435
assert(name != null, 'v3 name not specified');
3536
assert(namespace != null, 'v3 namespace not specified');
3637

37-
if (namespace == 'URL') namespace = uuidV3.URL;
38-
if (namespace == 'DNS') namespace = uuidV3.DNS;
38+
if (namespace === 'URL') namespace = v3.URL;
39+
if (namespace === 'DNS') namespace = v3.DNS;
3940

40-
console.log(uuidV3(name, namespace));
41+
console.log(v3(name, namespace));
4142
break;
4243

4344
case 'v4':
44-
var uuidV4 = require('../v4');
45-
console.log(uuidV4());
45+
console.log(v4());
4646
break;
4747

4848
case 'v5':
49-
var uuidV5 = require('../v5');
50-
5149
var name = args.shift();
5250
var namespace = args.shift();
5351
assert(name != null, 'v5 name not specified');
5452
assert(namespace != null, 'v5 namespace not specified');
5553

56-
if (namespace == 'URL') namespace = uuidV5.URL;
57-
if (namespace == 'DNS') namespace = uuidV5.DNS;
54+
if (namespace === 'URL') namespace = v5.URL;
55+
if (namespace === 'DNS') namespace = v5.DNS;
5856

59-
console.log(uuidV5(name, namespace));
57+
console.log(v5(name, namespace));
6058
break;
6159

6260
default:

‎v1.js ‎src/v1.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var rng = require('./lib/rng');
2-
var bytesToUuid = require('./lib/bytesToUuid');
1+
import rng from './rng.js';
2+
import bytesToUuid from './bytesToUuid.js';
33

44
// **`v1()` - Generate time-based UUID**
55
//
@@ -106,4 +106,4 @@ function v1(options, buf, offset) {
106106
return buf ? buf : bytesToUuid(b);
107107
}
108108

109-
module.exports = v1;
109+
export default v1;

‎src/v3.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import v35 from './v35.js';
2+
import md5 from './md5.js';
3+
4+
const v3 = v35('v3', 0x30, md5);
5+
export default v3;

‎lib/v35.js ‎src/v35.js

+9-6
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
var bytesToUuid = require('./bytesToUuid');
1+
import bytesToUuid from './bytesToUuid.js';
22

33
function uuidToBytes(uuid) {
44
// Note: We assume we're being passed a valid uuid string
@@ -19,7 +19,10 @@ function stringToBytes(str) {
1919
return bytes;
2020
}
2121

22-
module.exports = function(name, version, hashfunc) {
22+
export const DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
23+
export const URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
24+
25+
export default function(name, version, hashfunc) {
2326
var generateUUID = function(value, namespace, buf, offset) {
2427
var off = buf && offset || 0;
2528

@@ -49,9 +52,9 @@ module.exports = function(name, version, hashfunc) {
4952
} catch (err) {
5053
}
5154

52-
// Pre-defined namespaces, per Appendix C
53-
generateUUID.DNS = '6ba7b810-9dad-11d1-80b4-00c04fd430c8';
54-
generateUUID.URL = '6ba7b811-9dad-11d1-80b4-00c04fd430c8';
55+
// For CommonJS default export support
56+
generateUUID.DNS = DNS;
57+
generateUUID.URL = URL;
5558

5659
return generateUUID;
57-
};
60+
}

‎v4.js ‎src/v4.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
var rng = require('./lib/rng');
2-
var bytesToUuid = require('./lib/bytesToUuid');
1+
import rng from './rng.js';
2+
import bytesToUuid from './bytesToUuid.js';
33

44
function v4(options, buf, offset) {
55
var i = buf && offset || 0;
@@ -26,4 +26,4 @@ function v4(options, buf, offset) {
2626
return buf || bytesToUuid(rnds);
2727
}
2828

29-
module.exports = v4;
29+
export default v4;

‎src/v5.js

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import v35 from './v35.js';
2+
import sha1 from './sha1.js';
3+
4+
const v5 = v35('v5', 0x50, sha1);
5+
export default v5;

‎test/mocha.opts

-3
This file was deleted.

‎test/test.js

+201-223
Large diffs are not rendered by default.

‎v3.js

-4
This file was deleted.

‎v5.js

-3
This file was deleted.

0 commit comments

Comments
 (0)
Please sign in to comment.