From e9250443a70c7de26ce90d734a54ceaf00c9e07d Mon Sep 17 00:00:00 2001 From: MTRNord Date: Fri, 8 Nov 2024 10:58:38 +0100 Subject: [PATCH 1/4] Use cjs fileending in build output Signed-off-by: MTRNord --- packages/api/package.json | 6 +++--- packages/mui/package.json | 4 ++-- packages/react/package.json | 6 +++--- packages/testing/package.json | 6 +++--- rollup.config.mjs | 2 +- 5 files changed, 12 insertions(+), 12 deletions(-) diff --git a/packages/api/package.json b/packages/api/package.json index 7c7b4c73..0a88c36b 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -44,11 +44,11 @@ }, "publishConfig": { "module": "./build/esm/index.js", - "main": "./build/cjs/index.js", + "main": "./build/cjs/index.cjs", "types": "./build/index.d.ts", "exports": { "import": "./build/esm/index.js", - "require": "./build/cjs/index.js", + "require": "./build/cjs/index.cjs", "types": "./build/index.d.ts" } }, @@ -60,4 +60,4 @@ "widget", "matrix-widget-api" ] -} +} \ No newline at end of file diff --git a/packages/mui/package.json b/packages/mui/package.json index 6346339d..7f15a7cc 100644 --- a/packages/mui/package.json +++ b/packages/mui/package.json @@ -65,11 +65,11 @@ }, "publishConfig": { "module": "./build/esm/index.js", - "main": "./build/cjs/index.js", + "main": "./build/cjs/index.cjs", "types": "./build/index.d.ts", "exports": { "import": "./build/esm/index.js", - "require": "./build/cjs/index.js", + "require": "./build/cjs/index.cjs", "types": "./build/index.d.ts" } }, diff --git a/packages/react/package.json b/packages/react/package.json index 96df0def..0914aaf9 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -50,11 +50,11 @@ }, "publishConfig": { "module": "./build/esm/index.js", - "main": "./build/cjs/index.js", + "main": "./build/cjs/index.cjs", "types": "./build/index.d.ts", "exports": { "import": "./build/esm/index.js", - "require": "./build/cjs/index.js", + "require": "./build/cjs/index.cjs", "types": "./build/index.d.ts" } }, @@ -66,4 +66,4 @@ "widget", "matrix-widget-api" ] -} +} \ No newline at end of file diff --git a/packages/testing/package.json b/packages/testing/package.json index 610f8989..eaf2054b 100644 --- a/packages/testing/package.json +++ b/packages/testing/package.json @@ -42,11 +42,11 @@ }, "publishConfig": { "module": "./build/esm/index.js", - "main": "./build/cjs/index.js", + "main": "./build/cjs/index.cjs", "types": "./build/index.d.ts", "exports": { "import": "./build/esm/index.js", - "require": "./build/cjs/index.js", + "require": "./build/cjs/index.cjs", "types": "./build/index.d.ts" } }, @@ -58,4 +58,4 @@ "widget", "matrix-widget-api" ] -} +} \ No newline at end of file diff --git a/rollup.config.mjs b/rollup.config.mjs index 510a7ca2..b1909322 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -21,7 +21,7 @@ const config = { plugins: [commonjs()], output: [ { - file: 'build/cjs/index.js', + file: 'build/cjs/index.cjs', format: 'cjs', interop: 'auto', }, From 14aa88b0a440a3351379942f6072c1f518db0608 Mon Sep 17 00:00:00 2001 From: MTRNord Date: Fri, 8 Nov 2024 11:13:03 +0100 Subject: [PATCH 2/4] Use correct export syntax Signed-off-by: MTRNord --- package.json | 1 + packages/api/package.json | 17 ++++++++---- packages/mui/package.json | 19 +++++++++----- packages/react/package.json | 17 ++++++++---- packages/testing/package.json | 17 ++++++++---- rollup.config.mjs | 3 +++ scripts/prepack.js | 49 +++++++++++++++++++++++++++++++++++ yarn.lock | 15 +++++++++++ 8 files changed, 117 insertions(+), 21 deletions(-) diff --git a/package.json b/package.json index 1857a840..41a82ba0 100644 --- a/package.json +++ b/package.json @@ -35,6 +35,7 @@ "@rollup/plugin-commonjs": "^28.0.0", "cross-env": "^7.0.3", "depcheck": "^1.4.7", + "dree": "^5.1.5", "eslint": "^9.7.0", "eslint-config-prettier": "^9.1.0", "eslint-plugin-notice": "^1.0.0", diff --git a/packages/api/package.json b/packages/api/package.json index 0a88c36b..1d6985ca 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -5,7 +5,6 @@ "author": "Nordeck IT + Consulting GmbH", "license": "Apache-2.0", "source": "./src/index.ts", - "types": "./src/index.ts", "exports": { "import": "./src/index.ts", "require": "./src/index.ts" @@ -45,11 +44,19 @@ "publishConfig": { "module": "./build/esm/index.js", "main": "./build/cjs/index.cjs", - "types": "./build/index.d.ts", "exports": { - "import": "./build/esm/index.js", - "require": "./build/cjs/index.cjs", - "types": "./build/index.d.ts" + ".": { + "module": "./build/esm/index.js", + "import": { + "types": "./build/esm/index.d.ts", + "default": "./build/esm/index.js" + }, + "require": { + "types": "./build/cjs/index.d.cts", + "default": "./build/cjs/index.cjs" + } + }, + "./package.json": "./package.json" } }, "files": [ diff --git a/packages/mui/package.json b/packages/mui/package.json index 7f15a7cc..346796c7 100644 --- a/packages/mui/package.json +++ b/packages/mui/package.json @@ -5,7 +5,6 @@ "author": "Nordeck IT + Consulting GmbH", "license": "Apache-2.0", "source": "./src/index.ts", - "types": "./src/index.ts", "exports": { "import": "./src/index.ts", "require": "./src/index.ts" @@ -66,11 +65,19 @@ "publishConfig": { "module": "./build/esm/index.js", "main": "./build/cjs/index.cjs", - "types": "./build/index.d.ts", "exports": { - "import": "./build/esm/index.js", - "require": "./build/cjs/index.cjs", - "types": "./build/index.d.ts" + ".": { + "module": "./build/esm/index.js", + "import": { + "types": "./build/esm/index.d.ts", + "default": "./build/esm/index.js" + }, + "require": { + "types": "./build/cjs/index.d.cts", + "default": "./build/cjs/index.cjs" + } + }, + "./package.json": "./package.json" } }, "files": [ @@ -81,4 +88,4 @@ "widget", "matrix-widget-api" ] -} +} \ No newline at end of file diff --git a/packages/react/package.json b/packages/react/package.json index 0914aaf9..06320bde 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -5,7 +5,6 @@ "author": "Nordeck IT + Consulting GmbH", "license": "Apache-2.0", "source": "./src/index.ts", - "types": "./src/index.ts", "exports": { "import": "./src/index.ts", "require": "./src/index.ts" @@ -51,11 +50,19 @@ "publishConfig": { "module": "./build/esm/index.js", "main": "./build/cjs/index.cjs", - "types": "./build/index.d.ts", "exports": { - "import": "./build/esm/index.js", - "require": "./build/cjs/index.cjs", - "types": "./build/index.d.ts" + ".": { + "module": "./build/esm/index.js", + "import": { + "types": "./build/esm/index.d.ts", + "default": "./build/esm/index.js" + }, + "require": { + "types": "./build/cjs/index.d.cts", + "default": "./build/cjs/index.cjs" + } + }, + "./package.json": "./package.json" } }, "files": [ diff --git a/packages/testing/package.json b/packages/testing/package.json index eaf2054b..5ed63735 100644 --- a/packages/testing/package.json +++ b/packages/testing/package.json @@ -5,7 +5,6 @@ "author": "Nordeck IT + Consulting GmbH", "license": "Apache-2.0", "source": "./src/index.ts", - "types": "./src/index.ts", "exports": { "import": "./src/index.ts", "require": "./src/index.ts" @@ -43,11 +42,19 @@ "publishConfig": { "module": "./build/esm/index.js", "main": "./build/cjs/index.cjs", - "types": "./build/index.d.ts", "exports": { - "import": "./build/esm/index.js", - "require": "./build/cjs/index.cjs", - "types": "./build/index.d.ts" + ".": { + "module": "./build/esm/index.js", + "import": { + "types": "./build/esm/index.d.ts", + "default": "./build/esm/index.js" + }, + "require": { + "types": "./build/cjs/index.d.cts", + "default": "./build/cjs/index.cjs" + } + }, + "./package.json": "./package.json" } }, "files": [ diff --git a/rollup.config.mjs b/rollup.config.mjs index b1909322..0592a41a 100644 --- a/rollup.config.mjs +++ b/rollup.config.mjs @@ -16,6 +16,9 @@ import commonjs from '@rollup/plugin-commonjs'; +/** + * @type {import('rollup').RollupOptions} + */ const config = { input: 'lib/src/index.js', plugins: [commonjs()], diff --git a/scripts/prepack.js b/scripts/prepack.js index eac9fb92..220ece84 100644 --- a/scripts/prepack.js +++ b/scripts/prepack.js @@ -11,6 +11,55 @@ const PACKAGE_PATH = 'package.json'; const PACKAGE_PATH_BACKUP = 'package.json-prepack'; import * as fs from 'fs'; +import * as path from 'path'; +import * as dree from 'dree'; + +// Ensure the typescript types are in the build folder as cjs and esm variants +const walkOptions = { + depth: 100 +} +const tree = dree.scan('./lib/src', walkOptions); + +function treeWalk(child) { + if (!child.path.endsWith('.d.ts')) { + return; + } + // Copy the types for cjs under the build/cjs folder + const cjsPath = child.path.replace('lib/src', 'build/cjs'); + + // If it is a directory, create the directory + if (child.type === 'directory') { + fs.mkdirSync(cjsPath, { recursive: true }); + } else { + // Ensure target directory exists + fs.mkdirSync(path.dirname(cjsPath), { recursive: true }); + fs.copyFileSync(child.path, cjsPath.replace('.d.ts', '.d.cts')); + } + + // Copy the types for esm under the build/esm folder + const esmPath = child.path.replace('lib/src', 'build/esm'); + + // If it is a directory, create the directory + if (child.type === 'directory') { + fs.mkdirSync(esmPath, { recursive: true }); + } else { + // Ensure target directory exists + fs.mkdirSync(path.dirname(esmPath), { recursive: true }); + fs.copyFileSync(child.path, esmPath); + } +} + +// Copy the types to the build folder while preserving the folder structure. +// It also copies each .d.ts to a file with .d.cts extension to be used in the cjs build. +// Be aware that children is recursive. A child can have children. +function treeWalker(node) { + treeWalk(node); + if (node.children) { + node.children.forEach(treeWalker); + } +} + +treeWalker(tree); const packageContent = JSON.parse(fs.readFileSync(PACKAGE_PATH)); diff --git a/yarn.lock b/yarn.lock index a1e63a5b..8c806d2a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -2720,6 +2720,14 @@ domutils@^3.0.1, domutils@^3.1.0: domelementtype "^2.3.0" domhandler "^5.0.3" +dree@^5.1.5: + version "5.1.5" + resolved "https://registry.yarnpkg.com/dree/-/dree-5.1.5.tgz#7341107e5c5c694cad4e3f903ab2beec29864386" + integrity sha512-4VZ5m1EgV+467S2LdTUxCE1L98ikzGFW7PTE8FlPSk7jvkbqeKQN1vCfwQ6w1vYVgAZMSooyvoae5F/LQWozhw== + dependencies: + minimatch "^10.0.1" + yargs "^17.7.2" + eastasianwidth@^0.2.0: version "0.2.0" resolved "https://registry.yarnpkg.com/eastasianwidth/-/eastasianwidth-0.2.0.tgz#696ce2ec0aa0e6ea93a397ffcf24aa7840c827cb" @@ -4504,6 +4512,13 @@ min-indent@^1.0.0: resolved "https://registry.yarnpkg.com/min-indent/-/min-indent-1.0.1.tgz#a63f681673b30571fbe8bc25686ae746eefa9869" integrity sha512-I9jwMn07Sy/IwOj3zVkVik2JTvgpaykDZEigL6Rx6N9LbMywwUSMtxET+7lVoDLLd3O3IXwJwvuuns8UB/HeAg== +minimatch@^10.0.1: + version "10.0.1" + resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-10.0.1.tgz#ce0521856b453c86e25f2c4c0d03e6ff7ddc440b" + integrity sha512-ethXTt3SGGR+95gudmqJ1eNhRO7eGEGIgYA9vnPatK4/etz2MEVDno5GMCibdMTuBMyElzIlgxMna3K94XDIDQ== + dependencies: + brace-expansion "^2.0.1" + minimatch@^3.0.2, minimatch@^3.0.3, minimatch@^3.0.4, minimatch@^3.1.2: version "3.1.2" resolved "https://registry.yarnpkg.com/minimatch/-/minimatch-3.1.2.tgz#19cd194bfd3e428f049a70817c038d89ab4be35b" From 2d3333fe923eab8085111d5e29e9eb3d70ca784e Mon Sep 17 00:00:00 2001 From: MTRNord Date: Fri, 8 Nov 2024 12:18:12 +0100 Subject: [PATCH 3/4] Fix formatting Signed-off-by: MTRNord --- packages/api/package.json | 2 +- packages/mui/package.json | 2 +- packages/react/package.json | 2 +- packages/testing/package.json | 2 +- scripts/prepack.js | 6 +++--- 5 files changed, 7 insertions(+), 7 deletions(-) diff --git a/packages/api/package.json b/packages/api/package.json index 1d6985ca..88ca40eb 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -67,4 +67,4 @@ "widget", "matrix-widget-api" ] -} \ No newline at end of file +} diff --git a/packages/mui/package.json b/packages/mui/package.json index 346796c7..ea56bf1a 100644 --- a/packages/mui/package.json +++ b/packages/mui/package.json @@ -88,4 +88,4 @@ "widget", "matrix-widget-api" ] -} \ No newline at end of file +} diff --git a/packages/react/package.json b/packages/react/package.json index 06320bde..68294bc6 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -73,4 +73,4 @@ "widget", "matrix-widget-api" ] -} \ No newline at end of file +} diff --git a/packages/testing/package.json b/packages/testing/package.json index 5ed63735..dd6e9e16 100644 --- a/packages/testing/package.json +++ b/packages/testing/package.json @@ -65,4 +65,4 @@ "widget", "matrix-widget-api" ] -} \ No newline at end of file +} diff --git a/scripts/prepack.js b/scripts/prepack.js index 220ece84..67765818 100644 --- a/scripts/prepack.js +++ b/scripts/prepack.js @@ -10,14 +10,14 @@ const PACKAGE_PATH = 'package.json'; const PACKAGE_PATH_BACKUP = 'package.json-prepack'; +import * as dree from 'dree'; import * as fs from 'fs'; import * as path from 'path'; -import * as dree from 'dree'; // Ensure the typescript types are in the build folder as cjs and esm variants const walkOptions = { - depth: 100 -} + depth: 100, +}; const tree = dree.scan('./lib/src', walkOptions); function treeWalk(child) { From 377d8d20eabb0a3b27b98a6cf1604e6d334be6e3 Mon Sep 17 00:00:00 2001 From: MTRNord Date: Fri, 8 Nov 2024 12:23:17 +0100 Subject: [PATCH 4/4] Ensure that the types in devmode actually are set so tsc is happy in the monorepo Signed-off-by: MTRNord --- packages/api/package.json | 2 ++ packages/mui/package.json | 2 ++ packages/react/package.json | 2 ++ packages/testing/package.json | 2 ++ 4 files changed, 8 insertions(+) diff --git a/packages/api/package.json b/packages/api/package.json index 88ca40eb..b5b868a7 100644 --- a/packages/api/package.json +++ b/packages/api/package.json @@ -5,6 +5,7 @@ "author": "Nordeck IT + Consulting GmbH", "license": "Apache-2.0", "source": "./src/index.ts", + "types": "./src/index.ts", "exports": { "import": "./src/index.ts", "require": "./src/index.ts" @@ -44,6 +45,7 @@ "publishConfig": { "module": "./build/esm/index.js", "main": "./build/cjs/index.cjs", + "types": "./build/esm/index.d.ts", "exports": { ".": { "module": "./build/esm/index.js", diff --git a/packages/mui/package.json b/packages/mui/package.json index ea56bf1a..3a47245a 100644 --- a/packages/mui/package.json +++ b/packages/mui/package.json @@ -5,6 +5,7 @@ "author": "Nordeck IT + Consulting GmbH", "license": "Apache-2.0", "source": "./src/index.ts", + "types": "./src/index.ts", "exports": { "import": "./src/index.ts", "require": "./src/index.ts" @@ -65,6 +66,7 @@ "publishConfig": { "module": "./build/esm/index.js", "main": "./build/cjs/index.cjs", + "types": "./build/esm/index.d.ts", "exports": { ".": { "module": "./build/esm/index.js", diff --git a/packages/react/package.json b/packages/react/package.json index 68294bc6..1c678580 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -5,6 +5,7 @@ "author": "Nordeck IT + Consulting GmbH", "license": "Apache-2.0", "source": "./src/index.ts", + "types": "./src/index.ts", "exports": { "import": "./src/index.ts", "require": "./src/index.ts" @@ -50,6 +51,7 @@ "publishConfig": { "module": "./build/esm/index.js", "main": "./build/cjs/index.cjs", + "types": "./build/esm/index.d.ts", "exports": { ".": { "module": "./build/esm/index.js", diff --git a/packages/testing/package.json b/packages/testing/package.json index dd6e9e16..d59d0871 100644 --- a/packages/testing/package.json +++ b/packages/testing/package.json @@ -5,6 +5,7 @@ "author": "Nordeck IT + Consulting GmbH", "license": "Apache-2.0", "source": "./src/index.ts", + "types": "./src/index.ts", "exports": { "import": "./src/index.ts", "require": "./src/index.ts" @@ -42,6 +43,7 @@ "publishConfig": { "module": "./build/esm/index.js", "main": "./build/cjs/index.cjs", + "types": "./build/esm/index.d.ts", "exports": { ".": { "module": "./build/esm/index.js",