diff --git a/.gitignore b/.gitignore index b0fb09f..73a7d5e 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ node_modules dist/* +types/* diff --git a/package-lock.json b/package-lock.json index b1cea18..111cca3 100644 --- a/package-lock.json +++ b/package-lock.json @@ -104,6 +104,16 @@ } } }, + "@rollup/plugin-typescript": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/@rollup/plugin-typescript/-/plugin-typescript-6.1.0.tgz", + "integrity": "sha512-hJxaiE6WyNOsK+fZpbFh9CUijZYqPQuAOWO5khaGTUkM8DYNNyA2TDlgamecE+qLOG1G1+CwbWMAx3rbqpp6xQ==", + "dev": true, + "requires": { + "@rollup/pluginutils": "^3.1.0", + "resolve": "^1.17.0" + } + }, "@rollup/pluginutils": { "version": "3.1.0", "resolved": "https://registry.npmjs.org/@rollup/pluginutils/-/pluginutils-3.1.0.tgz", @@ -931,6 +941,12 @@ "yn": "3.1.1" } }, + "tslib": { + "version": "2.0.3", + "resolved": "https://registry.npmjs.org/tslib/-/tslib-2.0.3.tgz", + "integrity": "sha512-uZtkfKblCEQtZKBF6EBXVZeQNl82yqtDQdv+eck8u7tdPxjLu2/lp5/uPW+um2tpuxINHWy3GhiccY7QgEaVHQ==", + "dev": true + }, "type-check": { "version": "0.3.2", "resolved": "https://registry.npmjs.org/type-check/-/type-check-0.3.2.tgz", @@ -941,9 +957,9 @@ } }, "typescript": { - "version": "2.2.2", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-2.2.2.tgz", - "integrity": "sha1-YGAiUIR5tV/6NotY/uljoD39eww=", + "version": "4.1.1-rc", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.1.1-rc.tgz", + "integrity": "sha512-tgNcFrLIjlaMWEc7bKC0bxLNIt8BIAauY/HLUOQDyTP75HGskETtXOt46x4EKAHRKhWVLMc7yM02puTHa/yhCA==", "dev": true }, "v8-argv": { diff --git a/package.json b/package.json index c32badc..9d5fe2f 100644 --- a/package.json +++ b/package.json @@ -26,15 +26,17 @@ "readme.md" ], "scripts": { - "build": "rollup -c", + "build": "rollup -c && npm run typedef", "size": "npm run build && terser --compress --mangle -- ./dist/umd.js | gzip -c | wc -c", "test": "npm run build && tape test/*.js && jsmd readme.md && npm run test:typescript", - "test:typescript": "tsc --noEmit test/typescript.ts && ts-node test/typescript.ts" + "test:typescript": "tsc --noEmit test/typescript.ts && ts-node test/typescript.ts", + "typedef": "tsc -p tsconfig.typedef.json" }, "dependencies": {}, "devDependencies": { "@rollup/plugin-commonjs": "^16.0.0", "@rollup/plugin-node-resolve": "^10.0.0", + "@rollup/plugin-typescript": "^6.1.0", "@types/node": "^8.10.54", "is-mergeable-object": "1.1.0", "is-plain-obj": "^3.0.0", @@ -43,7 +45,8 @@ "tape": "^5.0.1", "terser": "^5.3.8", "ts-node": "^9.0.0", - "typescript": "=2.2.2" + "tslib": "^2.0.3", + "typescript": "^4.1.1-rc" }, "engines": { "node": ">=0.10.0" diff --git a/rollup.config.js b/rollup.config.js index cd5527c..ec2c616 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -2,13 +2,15 @@ import resolve from '@rollup/plugin-node-resolve' import commonjs from '@rollup/plugin-commonjs' +import typescript from '@rollup/plugin-typescript'; import pkg from './package.json' export default { - input: `src/index.js`, + input: `src/index.ts`, plugins: [ - commonjs(), resolve(), + commonjs(), + typescript() ], output: [ { diff --git a/src/index.js b/src/index.ts similarity index 94% rename from src/index.js rename to src/index.ts index 761c002..1db49d1 100644 --- a/src/index.js +++ b/src/index.ts @@ -1,4 +1,5 @@ -var isPlainObj = require('is-plain-obj') +import isPlainObj from 'is-plain-obj'; + function defaultIsMergeable(value) { return Array.isArray(value) || isPlainObj(value) } @@ -75,7 +76,7 @@ function mergeObject(target, source, options) { return destination } -function deepmerge(target, source, options) { +export default function deepmerge(target, source, options) { options = Object.assign({ arrayMerge: defaultArrayMerge, isMergeable: defaultIsMergeable @@ -96,7 +97,7 @@ function deepmerge(target, source, options) { } } -deepmerge.all = function deepmergeAll(array, options) { +export function deepmergeAll(array, options) { if (!Array.isArray(array)) { throw new Error('first argument should be an array') } @@ -105,5 +106,3 @@ deepmerge.all = function deepmergeAll(array, options) { return deepmerge(prev, next, options) }, {}) } - -module.exports = deepmerge diff --git a/tsconfig.json b/tsconfig.json new file mode 100644 index 0000000..8cf05cd --- /dev/null +++ b/tsconfig.json @@ -0,0 +1,17 @@ +{ + "compilerOptions": { + "target": "ES2020", + "module": "ES2020", + "lib": [ + "ES2020" + ], + "moduleResolution": "node", + "esModuleInterop": true, + "strict": true, + "skipLibCheck": true, + "forceConsistentCasingInFileNames": true + }, + "include": [ + "src" + ] +} diff --git a/tsconfig.typedef.json b/tsconfig.typedef.json new file mode 100644 index 0000000..fe082f3 --- /dev/null +++ b/tsconfig.typedef.json @@ -0,0 +1,8 @@ +{ + "extends": "./tsconfig.json", + "compilerOptions": { + "declaration": true, + "declarationDir": "types/", + "emitDeclarationOnly": true + } +} diff --git a/types/index.d.ts b/types/index.d.ts deleted file mode 100644 index 20f645a..0000000 --- a/types/index.d.ts +++ /dev/null @@ -1,16 +0,0 @@ -declare function deepmerge(x: Partial, y: Partial, options?: deepmerge.Options): T; -declare function deepmerge(x: Partial, y: Partial, options?: deepmerge.Options): T1 & T2; - -declare namespace deepmerge { - export interface Options { - arrayMerge?(target: any[], source: any[], options?: Options): any[]; - clone?: boolean; - customMerge?: (key: string, options?: Options) => ((x: any, y: any) => any) | undefined; - isMergeable?(value: object): boolean; - } - - export function all (objects: object[], options?: Options): object; - export function all (objects: Partial[], options?: Options): T; -} - -export = deepmerge;