Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix TypeScript default export with ESM (bug #471) #475

Merged
merged 4 commits into from
Mar 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
Loading