Skip to content

Commit

Permalink
[Typings] Fix typings for mjs (#197)
Browse files Browse the repository at this point in the history
* update version of `micro-memoize` and update type references

* update `es-to-mjs` script to include types

* Release 6.1.6-beta.0
  • Loading branch information
planttheidea authored May 8, 2023
1 parent d4364a6 commit 00b4323
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 deletions.
68 changes: 41 additions & 27 deletions es-to-mjs.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,42 +5,56 @@ const path = require('path');

const pkg = require('./package.json');

const SOURCE = path.join(__dirname, pkg.module);
const SOURCE_MAP = `${SOURCE}.map`;
const DESTINATION = path.join(__dirname, 'mjs', 'index.mjs');
const DESTINATION_MAP = `${DESTINATION}.map`;
const BASE_PATH = __dirname;
const SOURCE_ENTRY = path.join(BASE_PATH, pkg.module);
const SOURCE_MAP = `${SOURCE_ENTRY}.map`;
const SOURCE_TYPES = path.join(BASE_PATH, 'index.d.ts');
const DESTINATION = 'mjs';
const DESTINATION_ENTRY = path.join(BASE_PATH, DESTINATION, 'index.mjs');
const DESTINATION_MAP = `${DESTINATION_ENTRY}.map`;
const DESTINATION_TYPES = path.join(BASE_PATH, DESTINATION, 'index.d.mts');

function getFileName(filename) {
return filename.replace(`${BASE_PATH}/`, '');
}

const getFileName = (filename) => {
const split = filename.split('/');
try {
if (!fs.existsSync(path.join(__dirname, 'mjs'))) {
fs.mkdirSync(path.join(__dirname, 'mjs'));
}

return split[split.length - 1];
};
fs.copyFileSync(SOURCE_ENTRY, DESTINATION_ENTRY);

try {
if (!fs.existsSync(path.join(__dirname, 'mjs'))) {
fs.mkdirSync(path.join(__dirname, 'mjs'));
}
const contents = fs
.readFileSync(DESTINATION_ENTRY, { encoding: 'utf8' })
.replace('fast-equals', 'fast-equals/dist/fast-equals.mjs')
.replace('fast-stringify', 'fast-stringify/mjs/index.mjs')
.replace('micro-memoize', 'micro-memoize/mjs/index.mjs')
.replace(/\/\/# sourceMappingURL=(.*)/, (match, value) => {
return match.replace(value, 'index.mjs.map');
});

fs.copyFileSync(SOURCE, DESTINATION);
fs.writeFileSync(DESTINATION_ENTRY, contents, { encoding: 'utf8' });

const contents = fs
.readFileSync(DESTINATION, { encoding: 'utf8' })
.replace('fast-equals', 'fast-equals/dist/fast-equals.mjs')
.replace('fast-stringify', 'fast-stringify/mjs/index.mjs')
.replace('micro-memoize', 'micro-memoize/mjs/index.mjs')
.replace(/\/\/# sourceMappingURL=(.*)/, (match, value) => {
return match.replace(value, 'index.mjs.map');
});
console.log(
`Copied ${getFileName(SOURCE_ENTRY)} to ${getFileName(
DESTINATION_ENTRY
)}`
);

fs.writeFileSync(DESTINATION, contents, { encoding: 'utf8' });
fs.copyFileSync(SOURCE_MAP, DESTINATION_MAP);

console.log(`Copied ${getFileName(SOURCE)} to ${getFileName(DESTINATION)}`);
console.log(`Copied ${SOURCE_MAP} to ${getFileName(DESTINATION_MAP)}`);

fs.copyFileSync(SOURCE_MAP, DESTINATION_MAP);
fs.copyFileSync(SOURCE_TYPES, DESTINATION_TYPES);

console.log(`Copied ${getFileName(SOURCE_MAP)} to ${getFileName(DESTINATION_MAP)}`);
console.log(
`Copied ${getFileName(SOURCE_TYPES)} to ${getFileName(
DESTINATION_TYPES
)}`
);
} catch (error) {
console.error(error);
console.error(error);

process.exit(1);
process.exit(1);
}
12 changes: 8 additions & 4 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
/* eslint-disable */

import { MicroMemoize } from 'micro-memoize/src/types';
import {
Cache as BaseCache,
Memoized as BaseMemoized,
Options as BaseOptions,
} from 'micro-memoize';

export type AnyFn = (...args: any[]) => any;
export type Moizeable = AnyFn & Record<string, any>;
Expand Down Expand Up @@ -36,9 +40,9 @@ export type Key<Arg extends any = any> = Arg[];
export type Value = any;

export type Cache<MoizeableFn extends Moizeable = Moizeable> =
MicroMemoize.Cache<MoizeableFn>;
BaseCache<MoizeableFn>;
export type MicroMemoizeOptions<MoizeableFn extends Moizeable = Moizeable> =
MicroMemoize.Options<MoizeableFn>;
BaseOptions<MoizeableFn>;

export type Expiration = {
expirationMethod: () => void;
Expand Down Expand Up @@ -103,7 +107,7 @@ export type StatsCache = {
};

export type Memoized<MoizeableFn extends Moizeable = Moizeable> =
MicroMemoize.Memoized<MoizeableFn>;
BaseMemoized<MoizeableFn>;

export type Moized<
MoizeableFn extends Moizeable = Moizeable,
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
},
"dependencies": {
"fast-equals": "^3.0.1",
"micro-memoize": "^4.0.11"
"micro-memoize": "^4.1.2"
},
"description": "Blazing fast memoization based on all parameters passed",
"devDependencies": {
Expand Down Expand Up @@ -127,5 +127,5 @@
},
"sideEffects": false,
"types": "./index.d.ts",
"version": "6.1.5"
"version": "6.1.6-beta.0"
}
8 changes: 4 additions & 4 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5906,10 +5906,10 @@ methods@~1.1.2:
resolved "https://registry.yarnpkg.com/methods/-/methods-1.1.2.tgz#5529a4d67654134edcc5266656835b0f851afcee"
integrity sha512-iclAHeNqNm68zFtnZ0e+1L2yUIdvzNoauKU4WBA3VvH/vPFieF7qfRlwUZU+DA9P9bPXIS90ulxoUoCH23sV2w==

micro-memoize@^4.0.11:
version "4.0.14"
resolved "https://registry.yarnpkg.com/micro-memoize/-/micro-memoize-4.0.14.tgz#d1239ce2e5831125ac518509f5a23b54e7ca3e17"
integrity sha512-2tzWP1w2Hh+r7kCYa4f//jpBEA6dAueiuLco38NxfjF9Py3KCCI7wVOTdCvOhmTC043t+ulclVBdl3v+s+UJIQ==
micro-memoize@^4.1.2:
version "4.1.2"
resolved "https://registry.yarnpkg.com/micro-memoize/-/micro-memoize-4.1.2.tgz#ce719c1ba1e41592f1cd91c64c5f41dcbf135f36"
integrity sha512-+HzcV2H+rbSJzApgkj0NdTakkC+bnyeiUxgT6/m7mjcz1CmM22KYFKp+EVj1sWe4UYcnriJr5uqHQD/gMHLD+g==

micromatch@^4.0.2, micromatch@^4.0.4, micromatch@^4.0.5:
version "4.0.5"
Expand Down

0 comments on commit 00b4323

Please sign in to comment.