diff --git a/.eslintrc.js b/.eslintrc.js index 6eb18ee..975ba91 100644 --- a/.eslintrc.js +++ b/.eslintrc.js @@ -1,5 +1,16 @@ -/* @flow */ - module.exports = { - extends: "./node_modules/@krakenjs/eslint-config-grumbler", + extends: + "./node_modules/@krakenjs/eslint-config-grumbler/eslintrc-typescript", + + rules: { + // off for initial ts conversion + // Implicit any in catch clause + "@typescript-eslint/no-implicit-any-catch": "off", + // var will evaluate to '[object Object]' when stringified + "@typescript-eslint/no-base-to-string": "off", + // Prefer using an optional chain expression instead, as it's more concise and easier to read + "@typescript-eslint/prefer-optional-chain": "off", + // Generic Object Injection Sink + "security/detect-object-injection": "off", + }, }; diff --git a/.flowconfig b/.flowconfig deleted file mode 100644 index a87ef3f..0000000 --- a/.flowconfig +++ /dev/null @@ -1,15 +0,0 @@ -[ignore] -.*/node_modules/babel-plugin-flow-runtime -.*/node_modules/flow-runtime -.*/node_modules/cross-domain-utils/dist -.*/node_modules/cross-domain-utils/test -.*/node_modules/npm -.*/node_modules/resolve -.*/dist/module -[include] -[libs] -flow-typed -node_modules/@krakenjs/grumbler-scripts/declarations.js -[options] -module.name_mapper='^src\(.*\)$' -> '/src/\1' -experimental.const_params=false diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 80619fc..527d944 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -27,9 +27,6 @@ jobs: - name: 👕 Lint commit messages uses: wagoid/commitlint-github-action@v4 - - name: ▶️ Run flow-typed script - run: npm run flow-typed - - name: ▶️ Run build script run: npm run build diff --git a/babel.config.js b/babel.config.js new file mode 100644 index 0000000..dd684c7 --- /dev/null +++ b/babel.config.js @@ -0,0 +1,4 @@ +module.exports = { + extends: "@krakenjs/babel-config-grumbler/babelrc-browser", + presets: ["@krakenjs/babel-config-grumbler/flow-ts-babel-preset"], +}; diff --git a/karma.conf.js b/karma.conf.js deleted file mode 100644 index 032667a..0000000 --- a/karma.conf.js +++ /dev/null @@ -1,15 +0,0 @@ -/* @flow */ -/* eslint import/no-nodejs-modules: off, import/no-default-export: off */ - -import { getKarmaConfig } from "@krakenjs/karma-config-grumbler"; - -import { WEBPACK_CONFIG_TEST } from "./webpack.config"; - -export default function configKarma(karma: Object) { - const karmaConfig = getKarmaConfig(karma, { - basePath: __dirname, - webpack: WEBPACK_CONFIG_TEST, - }); - - karma.set(karmaConfig); -} diff --git a/package.json b/package.json index 610b972..db8fcb2 100644 --- a/package.json +++ b/package.json @@ -3,25 +3,28 @@ "version": "2.0.3", "description": "Cross domain safe WeakMap.", "main": "dist/cross-domain-safe-weakmap.js", + "module": "dist/index.js", + "types": "dist/types/index.d.ts", "scripts": { - "setup": "npm install && npm run flow-typed", - "lint": "eslint src/ test/ *.js", - "flow-typed": "rm -rf ./flow-typed && flow-typed install", - "flow": "flow", - "karma": "cross-env NODE_ENV=test babel-node --plugins=transform-es2015-modules-commonjs ./node_modules/.bin/karma start", - "babel": "babel src/ --out-dir dist/module", - "webpack": "babel-node --plugins=transform-es2015-modules-commonjs ./node_modules/.bin/webpack --progress", + "build": "npm run test && npm run babel && npm run webpack && npm run build:types", + "build:flow": "find ./dist -type f -not -path './node_modules/*' -name '*.d.ts' -exec sh -c 'flowgen --add-flow-header $1 -o ${1%.*.*}.js.flow' _ '{}' \\;", + "build:tsc": "tsc src/* --outDir ./dist/esm --declaration --emitDeclarationOnly --strict", + "build:types": "npm run build:tsc && npm run build:flow", + "webpack": "cross-env NODE_ENV=production babel-node --plugins=transform-es2015-modules-commonjs ./node_modules/.bin/webpack --progress", + "babel": "cross-env NODE_ENV=production babel src/ --out-dir ./dist/esm/ --extensions .ts,.tsx", + "tsc": "tsc", "format": "prettier --write --ignore-unknown .", "format:check": "prettier --check .", - "test": "npm run format:check && npm run lint && npm run flow-typed && npm run flow && npm run karma", - "build": "npm run test && npm run babel && npm run webpack", + "test": "npm run format:check && npm run lint && npm run tsc --no-emit && npm run vitest", + "lint": "eslint --ext ts,tsx,js,jsx src/ test/", + "clean": "rimraf dist coverage", "prerelease": "npm run clean && npm run build && git add dist && git commit -m 'ci: check in dist folder' || echo 'Nothing to distribute'", "release": "standard-version", "postrelease": "git push && git push --follow-tags && npm publish", - "clean": "rimraf dist coverage", - "reinstall": "rimraf flow-typed && rimraf node_modules && npm install && flow-typed install", "debug": "cross-env NODE_ENV=debug", - "prepare": "husky install" + "prepare": "husky install", + "vitest": "vitest run --dom --coverage", + "vitest:watch": "vitest watch --dom --coverage --ui" }, "repository": { "type": "git", @@ -72,26 +75,32 @@ ], "license": "Apache-2.0", "files": [ - "dist/", - "src/", - "postinstall.js" + "dist/" ], "readmeFilename": "README.md", "devDependencies": { - "@commitlint/cli": "^16.2.1", - "@commitlint/config-conventional": "^16.2.1", - "@krakenjs/grumbler-scripts": "^8.0.5", + "@commitlint/cli": "^17.3.0", + "@commitlint/config-conventional": "^17.3.0", + "@krakenjs/babel-config-grumbler": "^8.0.7", + "@krakenjs/eslint-config-grumbler": "^8.0.7", + "@krakenjs/typescript-config-grumbler": "^8.0.7", + "@krakenjs/webpack-config-grumbler": "8.1.0-alpha.1", + "@vitest/coverage-c8": "^0.25.3", + "@vitest/ui": "^0.25.3", "cross-env": "^7.0.3", - "flow-bin": "0.155.0", - "flow-typed": "^3.8.0", - "husky": "^7.0.0", - "lint-staged": "^12.4.0", - "mocha": "^4", - "prettier": "^2.6.2", - "standard-version": "^9.3.2" + "flowgen": "^1.20.1", + "happy-dom": "^7.7.0", + "husky": "^8.0.2", + "lint-staged": "^13.0.3", + "prettier": "^2.7.1", + "standard-version": "^9.5.0", + "ts-node": "^10.9.1", + "typescript": "4.9.3", + "vite": "^3.2.4", + "vitest": "^0.25.3" }, "dependencies": { - "@krakenjs/cross-domain-utils": "^3.0.2" + "@krakenjs/cross-domain-utils": "^4.0.0-alpha.2" }, "lint-staged": { "*": "prettier --write --ignore-unknown" diff --git a/src/index.js b/src/index.ts similarity index 82% rename from src/index.js rename to src/index.ts index f21cb1b..790b85c 100644 --- a/src/index.js +++ b/src/index.ts @@ -1,3 +1 @@ -/* @flow */ - export { CrossDomainSafeWeakMap as WeakMap } from "./weakmap"; diff --git a/src/native.js b/src/native.ts similarity index 97% rename from src/native.js rename to src/native.ts index 404d263..5593299 100644 --- a/src/native.js +++ b/src/native.ts @@ -1,5 +1,3 @@ -/* @flow */ - export function hasNativeWeakMap(): boolean { if (typeof WeakMap === "undefined") { return false; @@ -13,9 +11,7 @@ export function hasNativeWeakMap(): boolean { const testWeakMap = new WeakMap(); const testKey = {}; const testValue = "__testvalue__"; - Object.freeze(testKey); - testWeakMap.set(testKey, testValue); if (testWeakMap.get(testKey) === testValue) { diff --git a/src/util.js b/src/util.js deleted file mode 100644 index 371d6a4..0000000 --- a/src/util.js +++ /dev/null @@ -1,20 +0,0 @@ -/* @flow */ - -export function safeIndexOf(collection: $ReadOnlyArray, item: T): number { - for (let i = 0; i < collection.length; i++) { - try { - if (collection[i] === item) { - return i; - } - } catch (err) { - // pass - } - } - - return -1; -} - -// eslint-disable-next-line no-unused-vars -export function noop(...args: $ReadOnlyArray) { - // pass -} diff --git a/src/util.ts b/src/util.ts new file mode 100644 index 0000000..1189891 --- /dev/null +++ b/src/util.ts @@ -0,0 +1,18 @@ +export function safeIndexOf(collection: readonly T[], item: T): number { + for (let i = 0; i < collection.length; i++) { + try { + if (collection[i] === item) { + return i; + } + } catch (err) { + // pass + } + } + + return -1; +} + +// eslint-disable-next-line @typescript-eslint/no-unused-vars +export function noop(...args: readonly unknown[]): void { + // pass +} diff --git a/src/weakmap.js b/src/weakmap.ts similarity index 79% rename from src/weakmap.js rename to src/weakmap.ts index 86c3606..d34e141 100644 --- a/src/weakmap.js +++ b/src/weakmap.ts @@ -1,17 +1,16 @@ -/* @flow */ - -import { isWindow, isWindowClosed } from "@krakenjs/cross-domain-utils/src"; +import { + isWindow, + isWindowClosed, +} from "@krakenjs/cross-domain-utils/dist/esm"; import { hasNativeWeakMap } from "./native"; import { noop, safeIndexOf } from "./util"; -export class CrossDomainSafeWeakMap { +export class CrossDomainSafeWeakMap, V> { name: string; - weakmap: ?WeakMap; - // eslint-disable-next-line flowtype/no-mutable-array - keys: Array; - // eslint-disable-next-line flowtype/no-mutable-array - values: Array; + weakmap: WeakMap | undefined; + keys: K[]; + values: V[]; constructor() { // eslint-disable-next-line no-bitwise @@ -19,7 +18,7 @@ export class CrossDomainSafeWeakMap { if (hasNativeWeakMap()) { try { - this.weakmap = new WeakMap(); + this.weakmap = new WeakMap(); } catch (err) { // pass } @@ -29,13 +28,14 @@ export class CrossDomainSafeWeakMap { this.values = []; } - _cleanupClosedWindows() { + _cleanupClosedWindows(): void { const weakmap = this.weakmap; const keys = this.keys; for (let i = 0; i < keys.length; i++) { const value = keys[i]; + // @ts-expect-error `isWindowClosed` takes window which V is not if (isWindow(value) && isWindowClosed(value)) { if (weakmap) { try { @@ -47,7 +47,6 @@ export class CrossDomainSafeWeakMap { keys.splice(i, 1); this.values.splice(i, 1); - i -= 1; } } @@ -68,7 +67,7 @@ export class CrossDomainSafeWeakMap { return true; } - set(key: K, value: V) { + set(key: K, value: V): void { if (!key) { throw new Error(`WeakMap expected key`); } @@ -88,7 +87,9 @@ export class CrossDomainSafeWeakMap { const name = this.name; const entry = key[name]; + // @ts-expect-error entry is implicitly an any so cant index if (entry && entry[0] === key) { + // @ts-expect-error entry is implicitly an any so cant index entry[1] = value; } else { Object.defineProperty(key, name, { @@ -117,7 +118,7 @@ export class CrossDomainSafeWeakMap { } } - get(key: K): V | void { + get(key: K): V | undefined { if (!key) { throw new Error(`WeakMap expected key`); } @@ -138,8 +139,10 @@ export class CrossDomainSafeWeakMap { try { const entry = key[this.name]; + // @ts-expect-error entry is implicitly an any so cant index if (entry && entry[0] === key) { - return entry[1]; + // @ts-expect-error entry is implicitly an any so cant index + return entry[1] as V; } return; @@ -160,7 +163,7 @@ export class CrossDomainSafeWeakMap { return this.values[index]; } - delete(key: K) { + delete(key: K): void { if (!key) { throw new Error(`WeakMap expected key`); } @@ -179,7 +182,9 @@ export class CrossDomainSafeWeakMap { try { const entry = key[this.name]; + // @ts-expect-error entry is implicitly an any so cant index if (entry && entry[0] === key) { + // @ts-expect-error entry is implicitly an any so cant index entry[0] = entry[1] = undefined; } } catch (err) { @@ -219,6 +224,7 @@ export class CrossDomainSafeWeakMap { try { const entry = key[this.name]; + // @ts-expect-error entry is implicitly an any so cant index if (entry && entry[0] === key) { return true; } @@ -237,7 +243,7 @@ export class CrossDomainSafeWeakMap { getOrSet(key: K, getter: () => V): V { if (this.has(key)) { - // $FlowFixMe + // @ts-expect-error v | undefined is not assignable to v. need a guard check return this.get(key); } diff --git a/test/index.js b/test/index.js deleted file mode 100644 index f5d7518..0000000 --- a/test/index.js +++ /dev/null @@ -1,5 +0,0 @@ -/* @flow */ - -import "./util"; - -import "./tests"; diff --git a/test/tests/index.js b/test/tests/index.js deleted file mode 100644 index cf20ac7..0000000 --- a/test/tests/index.js +++ /dev/null @@ -1,3 +0,0 @@ -/* @flow */ - -import "./weakmap"; diff --git a/test/util.js b/test/util.js deleted file mode 100644 index fbee482..0000000 --- a/test/util.js +++ /dev/null @@ -1,11 +0,0 @@ -/* @flow */ - -window.console.karma = (...args) => { - const karma = - window.karma || - (window.top && window.top.karma) || - (window.opener && window.opener.karma); - karma.log("debug", args); - // eslint-disable-next-line no-console - console.log(...args); -}; diff --git a/test/tests/weakmap.js b/test/weakmap.test.ts similarity index 75% rename from test/tests/weakmap.js rename to test/weakmap.test.ts index f566b8f..390629b 100644 --- a/test/tests/weakmap.js +++ b/test/weakmap.test.ts @@ -1,10 +1,9 @@ -/* @flow */ -/* eslint max-lines: 0 */ +import { describe, it } from "vitest"; -import { WeakMap } from "../../src"; +import { WeakMap } from "../src"; -function getWindow(): Object { - const win = {}; +function getWindow(): Record { + const win: Record = {}; win.self = win; win.closed = false; win.parent = win; @@ -14,23 +13,24 @@ function getWindow(): Object { describe("weakmap standard cases", () => { it("should set and get a key", () => { - const map = new WeakMap(); + const map = new WeakMap, string>(); const obj = {}; const val = "foo"; - map.set(obj, val); - const result = map.get(obj); + if (!result) { + throw new Error(`Result is undefined`); + } + if (result !== val) { - throw new Error(`Expected ${result || ""} to be '${val}'`); + throw new Error(`Expected ${result} to be '${val}'`); } }); it("should get a non-existant key", () => { const map = new WeakMap(); const obj = {}; - const result = map.get(obj); if (result !== undefined) { @@ -39,39 +39,38 @@ describe("weakmap standard cases", () => { }); it("should set over an existing key, and get a key", () => { - const map = new WeakMap(); + const map = new WeakMap, string>(); const obj = {}; const val1 = "foo"; const val2 = "bar"; - map.set(obj, val1); map.set(obj, val2); - const result = map.get(obj); + if (!result) { + throw new Error(`Result is undefined`); + } + if (result !== val2) { - throw new Error(`Expected ${result || ""} to be '${val2}'`); + throw new Error(`Expected ${result} to be '${val2}'`); } }); it("should set and check for a key", () => { - const map = new WeakMap(); + const map = new WeakMap, string>(); const obj = {}; const val = "foo"; - map.set(obj, val); - const result = map.has(obj); if (!result) { - throw new Error(`Expected ${result.toString()} to be true`); + throw new Error(`Expected result to be truthy`); } }); it("should check for a non-existant key", () => { const map = new WeakMap(); const obj = {}; - const result = map.has(obj); if (result) { @@ -83,10 +82,8 @@ describe("weakmap standard cases", () => { const map = new WeakMap(); const obj = {}; const val = "foo"; - map.set(obj, val); map.delete(obj); - const result = map.has(obj); if (result) { @@ -99,14 +96,16 @@ describe("weakmap cross-origin cases", () => { const win = getWindow(); it("should set and get a key", () => { - const map = new WeakMap(); + const map = new WeakMap, string>(); const obj = win; const val = "foo"; - map.set(obj, val); - const result = map.get(obj); + if (!result) { + throw new Error(`Result is undefined`); + } + if (result !== val) { throw new Error(`Expected ${result || ""} to be '${val}'`); } @@ -115,7 +114,6 @@ describe("weakmap cross-origin cases", () => { it("should get a non-existant key", () => { const map = new WeakMap(); const obj = win; - const result = map.get(obj); if (result !== undefined) { @@ -124,18 +122,20 @@ describe("weakmap cross-origin cases", () => { }); it("should set over an existing key, and get a key", () => { - const map = new WeakMap(); + const map = new WeakMap, string>(); const obj = win; const val1 = "foo"; const val2 = "bar"; - map.set(obj, val1); map.set(obj, val2); - const result = map.get(obj); + if (!result) { + throw new Error(`Result is undefined`); + } + if (result !== val2) { - throw new Error(`Expected ${result || ""} to be '${val2}'`); + throw new Error(`Expected ${result} to be '${val2}'`); } }); @@ -143,9 +143,7 @@ describe("weakmap cross-origin cases", () => { const map = new WeakMap(); const obj = win; const val = "foo"; - map.set(obj, val); - const result = map.has(obj); if (!result) { @@ -156,7 +154,6 @@ describe("weakmap cross-origin cases", () => { it("should check for a non-existant key", () => { const map = new WeakMap(); const obj = win; - const result = map.has(obj); if (result) { @@ -168,10 +165,8 @@ describe("weakmap cross-origin cases", () => { const map = new WeakMap(); const obj = win; const val = "foo"; - map.set(obj, val); map.delete(obj); - const result = map.has(obj); if (result) { @@ -182,8 +177,6 @@ describe("weakmap cross-origin cases", () => { describe("weakmap cross-origin cases with IE erroring window", () => { const win = getWindow(); - - // $FlowFixMe Object.defineProperty(win, "self", { get() { throw new Error("Rargh can't do that I'm IE ph34r me"); @@ -191,23 +184,23 @@ describe("weakmap cross-origin cases with IE erroring window", () => { }); it("should set and get a key", () => { - const map = new WeakMap(); + const map = new WeakMap, string>(); const obj = win; const val = "foo"; - map.set(obj, val); - const result = map.get(obj); + if (!result) { + throw new Error(`Result is undefined`); + } if (result !== val) { - throw new Error(`Expected ${result || ""} to be '${val}'`); + throw new Error(`Expected ${result} to be '${val}'`); } }); it("should get a non-existant key", () => { const map = new WeakMap(); const obj = win; - const result = map.get(obj); if (result !== undefined) { @@ -216,18 +209,20 @@ describe("weakmap cross-origin cases with IE erroring window", () => { }); it("should set over an existing key, and get a key", () => { - const map = new WeakMap(); + const map = new WeakMap, string>(); const obj = win; const val1 = "foo"; const val2 = "bar"; - map.set(obj, val1); map.set(obj, val2); - const result = map.get(obj); + if (!result) { + throw new Error(`Result is undefined`); + } + if (result !== val2) { - throw new Error(`Expected ${result || ""} to be '${val2}'`); + throw new Error(`Expected ${result} to be '${val2}'`); } }); @@ -235,9 +230,7 @@ describe("weakmap cross-origin cases with IE erroring window", () => { const map = new WeakMap(); const obj = win; const val = "foo"; - map.set(obj, val); - const result = map.has(obj); if (!result) { @@ -248,7 +241,6 @@ describe("weakmap cross-origin cases with IE erroring window", () => { it("should check for a non-existant key", () => { const map = new WeakMap(); const obj = win; - const result = map.has(obj); if (result) { @@ -260,10 +252,8 @@ describe("weakmap cross-origin cases with IE erroring window", () => { const map = new WeakMap(); const obj = win; const val = "foo"; - map.set(obj, val); map.delete(obj); - const result = map.has(obj); if (result) { @@ -275,30 +265,31 @@ describe("weakmap cross-origin cases with IE erroring window", () => { describe("weakmap standard cases with no native WeakMap", () => { it("should set and get a key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; - const map = new WeakMap(); + const map = new WeakMap, string>(); window.WeakMap = weakMap; - const obj = {}; const val = "foo"; - map.set(obj, val); - const result = map.get(obj); + if (!result) { + throw new Error(`Result is undefined`); + } + if (result !== val) { - throw new Error(`Expected ${result || ""} to be '${val}'`); + throw new Error(`Expected ${result} to be '${val}'`); } }); it("should get a non-existant key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; const map = new WeakMap(); window.WeakMap = weakMap; - const obj = {}; - const result = map.get(obj); if (result !== undefined) { @@ -308,35 +299,35 @@ describe("weakmap standard cases with no native WeakMap", () => { it("should set over an existing key, and get a key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; - const map = new WeakMap(); + const map = new WeakMap, string>(); window.WeakMap = weakMap; - const obj = {}; const val1 = "foo"; const val2 = "bar"; - map.set(obj, val1); map.set(obj, val2); - const result = map.get(obj); + if (!result) { + throw new Error(`Result is undefined`); + } + if (result !== val2) { - throw new Error(`Expected ${result || ""} to be '${val2}'`); + throw new Error(`Expected ${result} to be '${val2}'`); } }); it("should set and check for a key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; const map = new WeakMap(); window.WeakMap = weakMap; - const obj = {}; const val = "foo"; - map.set(obj, val); - const result = map.has(obj); if (!result) { @@ -346,12 +337,11 @@ describe("weakmap standard cases with no native WeakMap", () => { it("should check for a non-existant key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; const map = new WeakMap(); window.WeakMap = weakMap; - const obj = {}; - const result = map.has(obj); if (result) { @@ -361,16 +351,14 @@ describe("weakmap standard cases with no native WeakMap", () => { it("should set, delete, and check for a key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; const map = new WeakMap(); window.WeakMap = weakMap; - const obj = {}; const val = "foo"; - map.set(obj, val); map.delete(obj); - const result = map.has(obj); if (result) { @@ -384,30 +372,31 @@ describe("weakmap cross-origin cases with no native WeakMap", () => { it("should set and get a key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; - const map = new WeakMap(); + const map = new WeakMap, string>(); window.WeakMap = weakMap; - const obj = win; const val = "foo"; - map.set(obj, val); - const result = map.get(obj); + if (!result) { + throw new Error(`Result is undefined`); + } + if (result !== val) { - throw new Error(`Expected ${result || ""} to be '${val}'`); + throw new Error(`Expected ${result} to be '${val}'`); } }); it("should get a non-existant key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; const map = new WeakMap(); window.WeakMap = weakMap; - const obj = win; - const result = map.get(obj); if (result !== undefined) { @@ -417,35 +406,35 @@ describe("weakmap cross-origin cases with no native WeakMap", () => { it("should set over an existing key, and get a key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; - const map = new WeakMap(); + const map = new WeakMap, string>(); window.WeakMap = weakMap; - const obj = win; const val1 = "foo"; const val2 = "bar"; - map.set(obj, val1); map.set(obj, val2); - const result = map.get(obj); + if (!result) { + throw new Error(`Result is undefined`); + } + if (result !== val2) { - throw new Error(`Expected ${result || ""} to be '${val2}'`); + throw new Error(`Expected ${result} to be '${val2}'`); } }); it("should set and check for a key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; const map = new WeakMap(); window.WeakMap = weakMap; - const obj = win; const val = "foo"; - map.set(obj, val); - const result = map.has(obj); if (!result) { @@ -455,12 +444,11 @@ describe("weakmap cross-origin cases with no native WeakMap", () => { it("should check for a non-existant key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; const map = new WeakMap(); window.WeakMap = weakMap; - const obj = win; - const result = map.has(obj); if (result) { @@ -470,16 +458,14 @@ describe("weakmap cross-origin cases with no native WeakMap", () => { it("should set, delete, and check for a key", () => { const weakMap = window.WeakMap; + // @ts-expect-error deleting a property we added from window delete window.WeakMap; const map = new WeakMap(); window.WeakMap = weakMap; - const obj = win; const val = "foo"; - map.set(obj, val); map.delete(obj); - const result = map.has(obj); if (result) { diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..5613994 --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,3 @@ +{ + "extends": "@krakenjs/typescript-config-grumbler/tsconfig.json" +} diff --git a/vite.config.js b/vite.config.js new file mode 100644 index 0000000..7950db1 --- /dev/null +++ b/vite.config.js @@ -0,0 +1,31 @@ +/* eslint-disable eslint-comments/disable-enable-pair */ +/* eslint-disable spaced-comment */ +/// + +// Configure Vitest (https://vitest.dev/config/) + +import path from "path"; + +import { defineConfig } from "vite"; + +// eslint-disable-next-line import/no-default-export +export default defineConfig({ + build: { + lib: { + entry: path.resolve(__dirname, "src/index.ts"), + name: "crossDomainSafeWeakMap", + fileName: (format) => `cross-domain-safe-weakmap.${format}.js`, + formats: ["es", "umd"], + }, + sourcemap: true, + rollupOptions: { + ouput: { + preserveModules: true, + }, + }, + }, + test: { + /* for example, use global to avoid globals imports (describe, test, expect): */ + // globals: true, + }, +}); diff --git a/webpack.config.js b/webpack.config.ts similarity index 62% rename from webpack.config.js rename to webpack.config.ts index e17cd63..b8a23f9 100644 --- a/webpack.config.js +++ b/webpack.config.ts @@ -1,13 +1,10 @@ -/* @flow */ -/* eslint import/no-nodejs-modules: off, import/no-default-export: off */ - -import type { WebpackConfig } from "@krakenjs/webpack-config-grumbler/index.flow"; import { getWebpackConfig } from "@krakenjs/webpack-config-grumbler"; export const FILE_NAME = "cross-domain-safe-weakmap"; export const MODULE_NAME = "xdsweakmap"; -export const WEBPACK_CONFIG: WebpackConfig = getWebpackConfig({ +export const WEBPACK_CONFIG = getWebpackConfig({ + entry: "./src/index.ts", filename: `${FILE_NAME}.js`, modulename: MODULE_NAME, vars: { @@ -16,7 +13,8 @@ export const WEBPACK_CONFIG: WebpackConfig = getWebpackConfig({ }, }); -export const WEBPACK_CONFIG_MIN: WebpackConfig = getWebpackConfig({ +export const WEBPACK_CONFIG_MIN = getWebpackConfig({ + entry: "./src/index.ts", filename: `${FILE_NAME}.min.js`, modulename: MODULE_NAME, minify: true, @@ -26,7 +24,8 @@ export const WEBPACK_CONFIG_MIN: WebpackConfig = getWebpackConfig({ }, }); -export const WEBPACK_CONFIG_TEST: WebpackConfig = getWebpackConfig({ +export const WEBPACK_CONFIG_TEST = getWebpackConfig({ + entry: "./src/index.ts", modulename: MODULE_NAME, options: { devtool: "inline-source-map",