Skip to content

Commit 0926778

Browse files
authored
Add cjs-specific output and types (#122)
* add cjs-specific output and types * update README * Release 4.2.0-beta.0 * fix scripts * Release 4.2.0-beta.1 * update CHANGELOG
1 parent 5023ec9 commit 0926778

6 files changed

Lines changed: 75 additions & 7 deletions

File tree

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
.nyc_output
33
.yarn
44
coverage
5+
cjs
56
dist
67
es
78
node_modules

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# micro-memoize CHANGELOG
22

3+
## 4.2.0
4+
5+
- [#122](https://github.com/planttheidea/micro-memoize/pull/122) - Add CJS import for CommonJS requires
6+
37
## 4.1.3
48

59
- [#121](https://github.com/planttheidea/micro-memoize/pull/121) - Avoid reference to broken source maps (fix for [#79](https://github.com/planttheidea/micro-memoize/issues/79))

README.md

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -47,18 +47,26 @@ ESM in browsers:
4747
import memoize from 'micro-memoize';
4848
```
4949

50-
ESM in NodeJS:
50+
CommonJS:
51+
52+
```ts
53+
const memoize = require('micro-memoize');
54+
```
55+
56+
MJS import:
5157

5258
```ts
5359
import memoize from 'micro-memoize/mjs';
5460
```
5561

56-
CommonJS:
62+
CJS require:
5763

5864
```ts
59-
const memoize = require('micro-memoize');
65+
const memoize = require('micro-memoize/cjs');
6066
```
6167

68+
Note: This is mainly used when `moduleResolution` is set to `Node16` / `NodeNext` in TS projects.
69+
6270
## Usage
6371

6472
```ts

package.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -83,11 +83,13 @@
8383
},
8484
"scripts": {
8585
"benchmark": "npm run dist && NODE_ENV=production node ./benchmarks/index.cjs",
86-
"build": "NODE_ENV=production rollup -c config/rollup.config.js --bundleConfigAsCjs",
87-
"build:mjs": "node scripts/es-to-mjs.js",
86+
"build": "npm run build:bundle && npm run build:mjs && npm run build:cjs",
87+
"build:bundle": "NODE_ENV=production rollup -c config/rollup.config.js --bundleConfigAsCjs",
88+
"build:cjs": "node scripts/create-cjs-import.js",
89+
"build:mjs": "node scripts/create-mjs-import.js",
8890
"clean": "rm -rf dist && rm -rf mjs",
8991
"dev": "NODE_ENV=development webpack-dev-server --config=webpack/webpack.config.js",
90-
"dist": "npm run clean && npm run build && npm run build:mjs",
92+
"dist": "npm run clean && npm run build",
9193
"lint": "ESLINT_USE_FLAT_CONFIG=false eslint src/*.ts --max-warnings 0",
9294
"lint:fix": "ESLINT_USE_FLAT_CONFIG=false npm run lint -- --fix",
9395
"prepublish": "if in-publish; then npm run prepublish:compile; fi",
@@ -102,5 +104,5 @@
102104
},
103105
"sideEffects": false,
104106
"types": "./index.d.ts",
105-
"version": "4.1.3"
107+
"version": "4.2.0-beta.1"
106108
}

scripts/create-cjs-import.js

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
const fs = require('fs');
2+
const path = require('path');
3+
4+
const pkg = require('../package.json');
5+
6+
const BASE_PATH = path.resolve(__dirname, '..');
7+
const SOURCE_ENTRY = path.join(BASE_PATH, pkg.main);
8+
const SOURCE_TYPES = path.join(BASE_PATH, 'index.d.ts');
9+
const DESTINATION = 'cjs';
10+
const DESTINATION_ENTRY = path.join(BASE_PATH, DESTINATION, 'index.cjs');
11+
const DESTINATION_TYPES = path.join(BASE_PATH, DESTINATION, 'index.d.cts');
12+
13+
function getFilename(filename) {
14+
return filename.replace(`${BASE_PATH}/`, '');
15+
}
16+
17+
try {
18+
if (!fs.existsSync(path.join(BASE_PATH, DESTINATION))) {
19+
fs.mkdirSync(path.join(BASE_PATH, DESTINATION));
20+
}
21+
22+
fs.copyFileSync(SOURCE_ENTRY, DESTINATION_ENTRY);
23+
24+
const contents = fs
25+
.readFileSync(DESTINATION_ENTRY, { encoding: 'utf8' })
26+
.replace(/\/\/# sourceMappingURL=(.*)/, (match, value) =>
27+
match.replace(value, 'index.cjs.map'),
28+
);
29+
30+
fs.writeFileSync(DESTINATION_ENTRY, contents, { encoding: 'utf8' });
31+
32+
console.log(
33+
`Copied ${getFilename(SOURCE_ENTRY)} to ${getFilename(DESTINATION_ENTRY)}`,
34+
);
35+
36+
const types = fs
37+
.readFileSync(SOURCE_TYPES, { encoding: 'utf8' })
38+
.replace(
39+
/export default function memoize/,
40+
() => 'declare function memoize',
41+
)
42+
.concat('\n\nexport = memoize;');
43+
44+
fs.writeFileSync(DESTINATION_TYPES, types, { encoding: 'utf8' });
45+
46+
console.log(
47+
`Copied ${getFilename(SOURCE_TYPES)} to ${getFilename(DESTINATION_TYPES)}`,
48+
);
49+
} catch (error) {
50+
console.error(error);
51+
52+
process.exit(1);
53+
}

0 commit comments

Comments
 (0)