Skip to content

Commit

Permalink
Fix TypeScript default export with ESM (bug #471) (#475)
Browse files Browse the repository at this point in the history
* Use mts extension

* Also fix import/export in typedef

* Remove clog

* Fix formatting
  • Loading branch information
eXon authored Mar 31, 2024
1 parent 80f64dd commit d9bc144
Show file tree
Hide file tree
Showing 7 changed files with 53 additions and 44 deletions.
11 changes: 6 additions & 5 deletions packages/react-cookie/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
"version": "7.1.1",
"description": "Universal cookies for React",
"main": "cjs/index.js",
"types": "cjs/index.d.ts",
"types": "cjs/index.d.mts",
"module": "esm/index.mjs",
"exports": {
".": {
"import": {
"types": "./esm/index.d.ts",
"types": "./esm/index.d.mts",
"default": "./esm/index.mjs"
},
"require": {
"types": "./cjs/index.d.ts",
"types": "./cjs/index.d.mts",
"default": "./cjs/index.js"
},
"default": "./cjs/index.js"
Expand All @@ -23,7 +23,7 @@
"esm",
"cjs",
"umd",
"index.d.ts",
"index.d.mts",
"LICENSE"
],
"repository": {
Expand All @@ -42,7 +42,8 @@
"license": "MIT",
"scripts": {
"prebuild": "rimraf esm && rimraf cjs && rimraf umd",
"build": "rollup -c"
"build": "rollup -c",
"postbuild": "node ../../tools/fix-typescript-typedef.mjs ./esm"
},
"dependencies": {
"@types/hoist-non-react-statics": "^3.3.5",
Expand Down
25 changes: 0 additions & 25 deletions packages/react-cookie/rollup.esm.mjs

This file was deleted.

9 changes: 5 additions & 4 deletions packages/universal-cookie-express/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
"version": "7.1.1",
"description": "Hook cookies get/set on Express for server-rendering",
"main": "cjs/index.js",
"types": "cjs/index.d.ts",
"types": "cjs/index.d.mts",
"module": "esm/index.mjs",
"exports": {
".": {
"import": {
"types": "./esm/index.d.ts",
"types": "./esm/index.d.mts",
"default": "./esm/index.mjs"
},
"require": {
"types": "./cjs/index.d.ts",
"types": "./cjs/index.d.mts",
"default": "./cjs/index.js"
},
"default": "./cjs/index.js"
Expand Down Expand Up @@ -40,7 +40,8 @@
"license": "MIT",
"scripts": {
"prebuild": "rimraf esm && rimraf cjs",
"build": "rollup -c"
"build": "rollup -c",
"postbuild": "node ../../tools/fix-typescript-typedef.mjs ./esm"
},
"dependencies": {
"universal-cookie": "^7.0.0"
Expand Down
9 changes: 5 additions & 4 deletions packages/universal-cookie-koa/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,16 @@
"version": "7.1.1",
"description": "Hook cookies get/set on Koa for server-rendering",
"main": "cjs/index.js",
"types": "cjs/index.d.ts",
"types": "cjs/index.d.mts",
"module": "esm/index.mjs",
"exports": {
".": {
"import": {
"types": "./esm/index.d.ts",
"types": "./esm/index.d.mts",
"default": "./esm/index.mjs"
},
"require": {
"types": "./cjs/index.d.ts",
"types": "./cjs/index.d.mts",
"default": "./cjs/index.js"
},
"default": "./cjs/index.js"
Expand Down Expand Up @@ -40,7 +40,8 @@
"license": "MIT",
"scripts": {
"prebuild": "rimraf esm && rimraf cjs",
"build": "rollup -c"
"build": "rollup -c",
"postbuild": "node ../../tools/fix-typescript-typedef.mjs ./esm"
},
"dependencies": {
"universal-cookie": "^7.0.0"
Expand Down
1 change: 0 additions & 1 deletion packages/universal-cookie/index.d.ts

This file was deleted.

11 changes: 6 additions & 5 deletions packages/universal-cookie/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,16 @@
"name": "universal-cookie",
"version": "7.1.1",
"description": "Universal cookies for JavaScript",
"types": "cjs/index.d.ts",
"types": "cjs/index.d.mts",
"module": "esm/index.mjs",
"exports": {
".": {
"import": {
"types": "./esm/index.d.ts",
"types": "./esm/index.d.mts",
"default": "./esm/index.mjs"
},
"require": {
"types": "./cjs/index.d.ts",
"types": "./cjs/index.d.mts",
"default": "./cjs/index.js"
},
"default": "./cjs/index.js"
Expand All @@ -22,7 +22,7 @@
"esm",
"cjs",
"umd",
"index.d.ts",
"index.d.mts",
"LICENSE"
],
"repository": {
Expand All @@ -40,7 +40,8 @@
"license": "MIT",
"scripts": {
"prebuild": "rimraf esm && rimraf cjs && rimraf umd",
"build": "rollup -c"
"build": "rollup -c",
"postbuild": "node ../../tools/fix-typescript-typedef.mjs ./esm"
},
"dependencies": {
"@types/cookie": "^0.6.0",
Expand Down
31 changes: 31 additions & 0 deletions tools/fix-typescript-typedef.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// When using ESM, the default export won't work unless we use .d.mts instead of .d.ts extension
import fs from 'fs/promises';
import path from 'path';

if (process.argv.length < 3) {
throw new Error('Missing folder');
}

const folder = process.argv[2];
console.log(folder);
const extLength = '.m.ts'.length;

const files = await fs.readdir(folder);
const promises = files
.filter((file) => /\.d\.ts$/.test(file))
.map(async (file) => {
const fullFile = path.join(folder, file);
const newFullFile = `${fullFile.substring(0, fullFile.length - extLength)}.d.mts`;

await fs.rename(fullFile, newFullFile);

const content = (await fs.readFile(newFullFile)).toString();
const newContent = content.replaceAll(
/ from \'([^']+)\'/g,
` from '$1.d.mts'`,
);

await fs.writeFile(newFullFile, newContent);
});

await Promise.all(promises);

0 comments on commit d9bc144

Please sign in to comment.