diff --git a/.local/wrapper.mjs b/.local/wrapper.mjs deleted file mode 120000 index 4d9f862a..00000000 --- a/.local/wrapper.mjs +++ /dev/null @@ -1 +0,0 @@ -../wrapper.mjs \ No newline at end of file diff --git a/bundlewatch.config.json b/bundlewatch.config.json index 4a3fb794..87704bf8 100644 --- a/bundlewatch.config.json +++ b/bundlewatch.config.json @@ -1,6 +1,6 @@ { "files": [ - { "path": "./examples/browser-rollup/dist/v1-size.js", "maxSize": "0.8 kB" }, + { "path": "./examples/browser-rollup/dist/v1-size.js", "maxSize": "0.9 kB" }, { "path": "./examples/browser-rollup/dist/v3-size.js", "maxSize": "1.8 kB" }, { "path": "./examples/browser-rollup/dist/v4-size.js", "maxSize": "0.5 kB" }, { "path": "./examples/browser-rollup/dist/v5-size.js", "maxSize": "1.2 kB" }, diff --git a/examples/browser-rollup/package-lock.json b/examples/browser-rollup/package-lock.json index fd7c8147..2dfa3610 100644 --- a/examples/browser-rollup/package-lock.json +++ b/examples/browser-rollup/package-lock.json @@ -30,6 +30,29 @@ "js-tokens": "^4.0.0" } }, + "@rollup/plugin-commonjs": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-11.1.0.tgz", + "integrity": "sha512-Ycr12N3ZPN96Fw2STurD21jMqzKwL9QuFhms3SD7KKRK7oaXUsBU9Zt0jL/rOPHiPYisI21/rXGO3jr9BnLHUA==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^3.0.8", + "commondir": "^1.0.1", + "estree-walker": "^1.0.1", + "glob": "^7.1.2", + "is-reference": "^1.1.2", + "magic-string": "^0.25.2", + "resolve": "^1.11.0" + }, + "dependencies": { + "estree-walker": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-1.0.1.tgz", + "integrity": "sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==", + "dev": true + } + } + }, "@rollup/plugin-node-resolve": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/@rollup/plugin-node-resolve/-/plugin-node-resolve-7.1.3.tgz", @@ -92,6 +115,22 @@ "color-convert": "^1.9.0" } }, + "balanced-match": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/balanced-match/-/balanced-match-1.0.0.tgz", + "integrity": "sha1-ibTRmasr7kneFk6gK4nORi1xt2c=", + "dev": true + }, + "brace-expansion": { + "version": "1.1.11", + "resolved": "https://registry.npmjs.org/brace-expansion/-/brace-expansion-1.1.11.tgz", + "integrity": "sha512-iCuPHDFgrHX7H2vEI/5xpz07zSHB00TpugqhmYtVmMO6518mCuRMoOYFldEBl0g187ufozdaHgWKcYFb61qGiA==", + "dev": true, + "requires": { + "balanced-match": "^1.0.0", + "concat-map": "0.0.1" + } + }, "braces": { "version": "3.0.2", "resolved": "https://registry.npmjs.org/braces/-/braces-3.0.2.tgz", @@ -145,6 +184,18 @@ "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", "dev": true }, + "commondir": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", + "integrity": "sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=", + "dev": true + }, + "concat-map": { + "version": "0.0.1", + "resolved": "https://registry.npmjs.org/concat-map/-/concat-map-0.0.1.tgz", + "integrity": "sha1-2Klr13/Wjfd5OnMDajug1UBdR3s=", + "dev": true + }, "escape-string-regexp": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/escape-string-regexp/-/escape-string-regexp-1.0.5.tgz", @@ -166,6 +217,12 @@ "to-regex-range": "^5.0.1" } }, + "fs.realpath": { + "version": "1.0.0", + "resolved": "https://registry.npmjs.org/fs.realpath/-/fs.realpath-1.0.0.tgz", + "integrity": "sha1-FQStJSMVjKpA20onh8sBQRmU6k8=", + "dev": true + }, "fsevents": { "version": "2.1.2", "resolved": "https://registry.npmjs.org/fsevents/-/fsevents-2.1.2.tgz", @@ -173,12 +230,42 @@ "dev": true, "optional": true }, + "glob": { + "version": "7.1.6", + "resolved": "https://registry.npmjs.org/glob/-/glob-7.1.6.tgz", + "integrity": "sha512-LwaxwyZ72Lk7vZINtNNrywX0ZuLyStrdDtabefZKAY5ZGJhVtgdznluResxNmPitE0SAO+O26sWTHeKSI2wMBA==", + "dev": true, + "requires": { + "fs.realpath": "^1.0.0", + "inflight": "^1.0.4", + "inherits": "2", + "minimatch": "^3.0.4", + "once": "^1.3.0", + "path-is-absolute": "^1.0.0" + } + }, "has-flag": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/has-flag/-/has-flag-3.0.0.tgz", "integrity": "sha1-tdRU3CGZriJWmfNGfloH87lVuv0=", "dev": true }, + "inflight": { + "version": "1.0.6", + "resolved": "https://registry.npmjs.org/inflight/-/inflight-1.0.6.tgz", + "integrity": "sha1-Sb1jMdfQLQwJvJEKEHW6gWW1bfk=", + "dev": true, + "requires": { + "once": "^1.3.0", + "wrappy": "1" + } + }, + "inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true + }, "is-module": { "version": "1.0.0", "resolved": "https://registry.npmjs.org/is-module/-/is-module-1.0.0.tgz", @@ -191,6 +278,15 @@ "integrity": "sha512-41Cifkg6e8TylSpdtTpeLVMqvSBEVzTttHvERD741+pnZ8ANv0004MRL43QKPDlK9cGvNp6NZWZUBlbGXYxxng==", "dev": true }, + "is-reference": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-1.1.4.tgz", + "integrity": "sha512-uJA/CDPO3Tao3GTrxYn6AwkM4nUPJiGGYu5+cB8qbC7WGFlrKZbiRo7SFKxUAEpFUfiHofWCXBUNhvYJMh+6zw==", + "dev": true, + "requires": { + "@types/estree": "0.0.39" + } + }, "jest-worker": { "version": "24.9.0", "resolved": "https://registry.npmjs.org/jest-worker/-/jest-worker-24.9.0.tgz", @@ -218,6 +314,15 @@ "integrity": "sha512-RdJUflcE3cUzKiMqQgsCu06FPu9UdIJO0beYbPhHN4k6apgJtifcoCtT9bcxOpYBtpD2kCM6Sbzg4CausW/PKQ==", "dev": true }, + "magic-string": { + "version": "0.25.7", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.7.tgz", + "integrity": "sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==", + "dev": true, + "requires": { + "sourcemap-codec": "^1.4.4" + } + }, "merge-stream": { "version": "2.0.0", "resolved": "https://registry.npmjs.org/merge-stream/-/merge-stream-2.0.0.tgz", @@ -234,6 +339,30 @@ "picomatch": "^2.0.5" } }, + "minimatch": { + "version": "3.0.4", + "resolved": "https://registry.npmjs.org/minimatch/-/minimatch-3.0.4.tgz", + "integrity": "sha512-yJHVQEhyqPLUTgt9B83PXu6W3rx4MvvHvSUvToogpwoGDOUQ+yDrR0HRot+yOCdCO7u4hX3pWft6kWBBcqh0UA==", + "dev": true, + "requires": { + "brace-expansion": "^1.1.7" + } + }, + "once": { + "version": "1.4.0", + "resolved": "https://registry.npmjs.org/once/-/once-1.4.0.tgz", + "integrity": "sha1-WDsap3WWHUsROsF9nFC6753Xa9E=", + "dev": true, + "requires": { + "wrappy": "1" + } + }, + "path-is-absolute": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/path-is-absolute/-/path-is-absolute-1.0.1.tgz", + "integrity": "sha1-F0uSaHNVNP+8es5r9TpanhtcX18=", + "dev": true + }, "path-parse": { "version": "1.0.6", "resolved": "https://registry.npmjs.org/path-parse/-/path-parse-1.0.6.tgz", @@ -308,6 +437,12 @@ "source-map": "^0.6.0" } }, + "sourcemap-codec": { + "version": "1.4.8", + "resolved": "https://registry.npmjs.org/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz", + "integrity": "sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==", + "dev": true + }, "supports-color": { "version": "5.5.0", "resolved": "https://registry.npmjs.org/supports-color/-/supports-color-5.5.0.tgz", @@ -339,6 +474,12 @@ }, "uuid": { "version": "file:../../.local" + }, + "wrappy": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/wrappy/-/wrappy-1.0.2.tgz", + "integrity": "sha1-tSQ9jz7BqjXxNkYFvA0QNuMKtp8=", + "dev": true } } } diff --git a/examples/browser-rollup/package.json b/examples/browser-rollup/package.json index bbf31496..6db664fb 100644 --- a/examples/browser-rollup/package.json +++ b/examples/browser-rollup/package.json @@ -10,6 +10,7 @@ "uuid": "file:../../.local" }, "devDependencies": { + "@rollup/plugin-commonjs": "^11.1.0", "@rollup/plugin-node-resolve": "^7.1.3", "rollup": "^2.6.1", "rollup-plugin-terser": "^5.3.0" diff --git a/examples/browser-rollup/rollup.config.js b/examples/browser-rollup/rollup.config.js index e5fe3f9f..3e39bb27 100644 --- a/examples/browser-rollup/rollup.config.js +++ b/examples/browser-rollup/rollup.config.js @@ -1,7 +1,14 @@ +const commonjs = require('@rollup/plugin-commonjs'); const resolve = require('@rollup/plugin-node-resolve'); const { terser } = require('rollup-plugin-terser'); -const plugins = [resolve({ browser: true }), terser()]; +const plugins = [ + resolve({ browser: true }), + commonjs({ + extensions: ['.cjs'], + }), + terser(), +]; module.exports = [ { input: './example-all.js', diff --git a/package-lock.json b/package-lock.json index 95db6ecf..6c044e9f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -2013,6 +2013,29 @@ "rimraf": "^2.5.2" } }, + "@rollup/plugin-commonjs": { + "version": "11.1.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-commonjs/-/plugin-commonjs-11.1.0.tgz", + "integrity": "sha512-Ycr12N3ZPN96Fw2STurD21jMqzKwL9QuFhms3SD7KKRK7oaXUsBU9Zt0jL/rOPHiPYisI21/rXGO3jr9BnLHUA==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^3.0.8", + "commondir": "^1.0.1", + "estree-walker": "^1.0.1", + "glob": "^7.1.2", + "is-reference": "^1.1.2", + "magic-string": "^0.25.2", + "resolve": "^1.11.0" + }, + "dependencies": { + "estree-walker": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/estree-walker/-/estree-walker-1.0.1.tgz", + "integrity": "sha512-1fMXF3YP4pZZVozF8j/ZLfvnR8NSIljt56UhbZ5PeeDmmGHpgpdwQt7ITlGvYaQukCvuBRMLEiKiYC+oeIg4cg==", + "dev": true + } + } + }, "@rollup/plugin-node-resolve": { "version": "7.1.3", "resolved": "https://registry.npmjs.org/@rollup/plugin-node-resolve/-/plugin-node-resolve-7.1.3.tgz", @@ -4399,6 +4422,12 @@ "integrity": "sha512-NOKm8xhkzAjzFx8B2v5OAHT+u5pRQc2UCa2Vq9jYL/31o2wi9mxBA7LIFs3sV5VSC49z6pEhfbMULvShKj26WA==", "dev": true }, + "commondir": { + "version": "1.0.1", + "resolved": "https://registry.npmjs.org/commondir/-/commondir-1.0.1.tgz", + "integrity": "sha1-3dgA2gxmEnOTzKWVDqloo6rxJTs=", + "dev": true + }, "compare-func": { "version": "1.3.2", "resolved": "https://registry.npmjs.org/compare-func/-/compare-func-1.3.2.tgz", @@ -8066,6 +8095,15 @@ "integrity": "sha1-eaKp7OfwlugPNtKy87wWwf9L8/o=", "dev": true }, + "is-reference": { + "version": "1.1.4", + "resolved": "https://registry.npmjs.org/is-reference/-/is-reference-1.1.4.tgz", + "integrity": "sha512-uJA/CDPO3Tao3GTrxYn6AwkM4nUPJiGGYu5+cB8qbC7WGFlrKZbiRo7SFKxUAEpFUfiHofWCXBUNhvYJMh+6zw==", + "dev": true, + "requires": { + "@types/estree": "0.0.39" + } + }, "is-regex": { "version": "1.0.4", "resolved": "https://registry.npmjs.org/is-regex/-/is-regex-1.0.4.tgz", @@ -10785,6 +10823,15 @@ "yallist": "^2.1.2" } }, + "magic-string": { + "version": "0.25.7", + "resolved": "https://registry.npmjs.org/magic-string/-/magic-string-0.25.7.tgz", + "integrity": "sha512-4CrMT5DOHTDk4HYDlzmwu4FVCcIYI8gauveasrdCu2IKIFOJ3f0v/8MDGJCDL9oD2ppz/Av1b0Nj345H9M+XIA==", + "dev": true, + "requires": { + "sourcemap-codec": "^1.4.4" + } + }, "make-dir": { "version": "2.1.0", "resolved": "https://registry.npmjs.org/make-dir/-/make-dir-2.1.0.tgz", @@ -12769,6 +12816,12 @@ "integrity": "sha1-PpNdfd1zYxuXZZlW1VEo6HtQhKM=", "dev": true }, + "sourcemap-codec": { + "version": "1.4.8", + "resolved": "https://registry.npmjs.org/sourcemap-codec/-/sourcemap-codec-1.4.8.tgz", + "integrity": "sha512-9NykojV5Uih4lgo5So5dtw+f0JgJX30KCNI8gwhz2J9A15wD0Ml6tjHKwf6fTSa6fAdVBdZeNOs9eJ71qCk8vA==", + "dev": true + }, "spdx-correct": { "version": "3.0.0", "resolved": "https://registry.npmjs.org/spdx-correct/-/spdx-correct-3.0.0.tgz", diff --git a/package.json b/package.json index 192a77bd..85bbb55b 100644 --- a/package.json +++ b/package.json @@ -20,14 +20,14 @@ "main": "./dist/index.js", "exports": { "require": "./dist/index.js", - "import": "./wrapper.mjs" + "import": "./dist/esm-node/index.mjs" }, - "module": "./dist/esm-node/index.js", + "module": "./dist/esm-node/index.mjs", "browser": { "./dist/md5.js": "./dist/md5-browser.js", "./dist/rng.js": "./dist/rng-browser.js", "./dist/sha1.js": "./dist/sha1-browser.js", - "./dist/esm-node/index.js": "./dist/esm-browser/index.js" + "./dist/esm-node/index.mjs": "./dist/esm-browser/index.js" }, "files": [ "CHANGELOG.md", @@ -39,8 +39,7 @@ "v1.js", "v3.js", "v4.js", - "v5.js", - "wrapper.mjs" + "v5.js" ], "devDependencies": { "@babel/cli": "7.8.4", @@ -48,6 +47,7 @@ "@babel/preset-env": "7.9.5", "@commitlint/cli": "8.3.5", "@commitlint/config-conventional": "8.3.4", + "@rollup/plugin-commonjs": "11.1.0", "@rollup/plugin-node-resolve": "7.1.3", "@wdio/browserstack-service": "6.0.12", "@wdio/cli": "6.0.15", diff --git a/rollup.config.js b/rollup.config.js index 34544dfd..59f8457b 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -1,4 +1,5 @@ import nodeResolve from '@rollup/plugin-node-resolve'; +import commonjs from '@rollup/plugin-commonjs'; import { terser } from 'rollup-plugin-terser'; function chunk(input, name) { @@ -10,7 +11,13 @@ function chunk(input, name) { name, compact: true, }, - plugins: [nodeResolve({ browser: true }), terser()], + plugins: [ + nodeResolve({ browser: true }), + commonjs({ + extensions: ['.cjs'], + }), + terser(), + ], }; } diff --git a/scripts/build.sh b/scripts/build.sh index 0efdbfea..fbf73972 100755 --- a/scripts/build.sh +++ b/scripts/build.sh @@ -35,6 +35,16 @@ done echo "Removing browser-specific files from esm-node" rm -f "$DIR"/esm-node/*-browser.js +for FILE in "$DIR"/esm-node/*.js +do + echo "Renaming esm-node file to .mjs: $FILE" + mv "$FILE" "${FILE%.js}.mjs" +done +# Fix imports +sed -i'' -e "s/\.js'/.mjs'/" "$DIR"/esm-node/*.mjs +sed -i'' -e "s/\.\/v1state.cjs'/..\/v1state.cjs'/" "$DIR"/esm-node/v1.mjs +rm -f "$DIR"/esm-node/v1state.cjs + # UMD Build mkdir "$DIR/umd" rollup -c diff --git a/src/v1.js b/src/v1.js index 594616e7..92cf4b2f 100644 --- a/src/v1.js +++ b/src/v1.js @@ -1,26 +1,20 @@ import rng from './rng.js'; import bytesToUuid from './bytesToUuid.js'; +import state from './v1state.cjs'; // **`v1()` - Generate time-based UUID** // // Inspired by https://github.com/LiosK/UUID.js // and http://docs.python.org/library/uuid.html -var _nodeId; -var _clockseq; - -// Previous uuid creation time -var _lastMSecs = 0; -var _lastNSecs = 0; - // See https://github.com/uuidjs/uuid for API details function v1(options, buf, offset) { var i = (buf && offset) || 0; var b = buf || []; options = options || {}; - var node = options.node || _nodeId; - var clockseq = options.clockseq !== undefined ? options.clockseq : _clockseq; + var node = options.node || state._nodeId; + var clockseq = options.clockseq !== undefined ? options.clockseq : state._clockseq; // node and clockseq need to be initialized to random values if they're not // specified. We do this lazily to minimize issues related to insufficient @@ -29,7 +23,7 @@ function v1(options, buf, offset) { var seedBytes = options.random || (options.rng || rng)(); if (node == null) { // Per 4.5, create and 48-bit node id, (47 random bits + multicast bit = 1) - node = _nodeId = [ + node = state._nodeId = [ seedBytes[0] | 0x01, seedBytes[1], seedBytes[2], @@ -40,7 +34,7 @@ function v1(options, buf, offset) { } if (clockseq == null) { // Per 4.2.2, randomize (14 bit) clockseq - clockseq = _clockseq = ((seedBytes[6] << 8) | seedBytes[7]) & 0x3fff; + clockseq = state._clockseq = ((seedBytes[6] << 8) | seedBytes[7]) & 0x3fff; } } @@ -52,10 +46,10 @@ function v1(options, buf, offset) { // Per 4.2.1.2, use count of uuid's generated during the current clock // cycle to simulate higher resolution clock - var nsecs = options.nsecs !== undefined ? options.nsecs : _lastNSecs + 1; + var nsecs = options.nsecs !== undefined ? options.nsecs : state._lastNSecs + 1; // Time since last uuid creation (in msecs) - var dt = msecs - _lastMSecs + (nsecs - _lastNSecs) / 10000; + var dt = msecs - state._lastMSecs + (nsecs - state._lastNSecs) / 10000; // Per 4.2.1.2, Bump clockseq on clock regression if (dt < 0 && options.clockseq === undefined) { @@ -64,7 +58,7 @@ function v1(options, buf, offset) { // Reset nsecs if clock regresses (new clockseq) or we've moved onto a new // time interval - if ((dt < 0 || msecs > _lastMSecs) && options.nsecs === undefined) { + if ((dt < 0 || msecs > state._lastMSecs) && options.nsecs === undefined) { nsecs = 0; } @@ -73,9 +67,9 @@ function v1(options, buf, offset) { throw new Error("uuid.v1(): Can't create more than 10M uuids/sec"); } - _lastMSecs = msecs; - _lastNSecs = nsecs; - _clockseq = clockseq; + state._lastMSecs = msecs; + state._lastNSecs = nsecs; + state._clockseq = clockseq; // Per 4.1.4 - Convert from unix epoch to Gregorian epoch msecs += 12219292800000; diff --git a/src/v1state.cjs b/src/v1state.cjs new file mode 100644 index 00000000..1b762b63 --- /dev/null +++ b/src/v1state.cjs @@ -0,0 +1,7 @@ +module.exports = { + _nodeId: undefined, + _clockseq: undefined, + // Previous uuid creation time + _lastMSecs: 0, + _lastNSecs: 0, +}; diff --git a/wrapper.mjs b/wrapper.mjs deleted file mode 100644 index 87e7f2e7..00000000 --- a/wrapper.mjs +++ /dev/null @@ -1,5 +0,0 @@ -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;