From 2eb7f0149e3e6a00253fedf13f3e7d88183600ec Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Tue, 11 Jul 2023 11:09:03 -0400 Subject: [PATCH 1/6] bug(build): fixed built ESM files importing CJS files from react-styles --- .../index.ts | 34 +++++++++++++++++++ .../package.json | 12 +++++++ packages/tsconfig.base.json | 3 +- 3 files changed, 48 insertions(+), 1 deletion(-) create mode 100644 packages/transformer-react-styles-esm-imports/index.ts create mode 100644 packages/transformer-react-styles-esm-imports/package.json diff --git a/packages/transformer-react-styles-esm-imports/index.ts b/packages/transformer-react-styles-esm-imports/index.ts new file mode 100644 index 00000000000..f7f3b68edf4 --- /dev/null +++ b/packages/transformer-react-styles-esm-imports/index.ts @@ -0,0 +1,34 @@ +// https://levelup.gitconnected.com/writing-typescript-custom-ast-transformer-part-2-5322c2b1660e +import * as ts from 'typescript'; + +const transformer: ts.TransformerFactory = (context) => (sourceFile) => { + // Only transform for ESM build + // ESM: module = 5, CJS: module = 1 + if (context.getCompilerOptions().module !== 5) { + return sourceFile; + } + + /** @type { import("typescript").Visitor } */ + function visitor(node: ts.Node) { + const { factory } = context; + if (ts.isImportDeclaration(node)) { + const text = node.moduleSpecifier.getFullText(sourceFile); + + if (/@patternfly\/react-styles\/css/.test(text)) { + return factory.updateImportDeclaration( + node, + node.decorators, + node.modifiers, + node.importClause, + factory.createStringLiteral(text.trim().replace(/"|'/g, '').replace(/$/, '.mjs'), true), + undefined + ); + } + } + + return ts.visitEachChild(node, visitor, context); + } + return ts.visitNode(sourceFile, visitor); +}; + +module.exports = transformer; diff --git a/packages/transformer-react-styles-esm-imports/package.json b/packages/transformer-react-styles-esm-imports/package.json new file mode 100644 index 00000000000..d2a3349a2ea --- /dev/null +++ b/packages/transformer-react-styles-esm-imports/package.json @@ -0,0 +1,12 @@ +{ + "name": "transformer-react-styles-esm-imports", + "private": true, + "version": "1.0.0", + "description": "Transform react-styles/css imports to target mjs files when compiling to ESM.", + "main": "index.ts", + "author": "Red Hat", + "license": "MIT", + "peerDependencies": { + "typescript": ">=3.7.2" + } +} diff --git a/packages/tsconfig.base.json b/packages/tsconfig.base.json index 8b1bd94e118..70704706ba6 100644 --- a/packages/tsconfig.base.json +++ b/packages/tsconfig.base.json @@ -19,7 +19,8 @@ "strictNullChecks": false, "isolatedModules": true, "plugins": [ - { "transform": "transformer-cjs-imports" } + { "transform": "transformer-cjs-imports" }, + { "transform": "transformer-react-styles-esm-imports/index.ts", "type": "raw" } ] }, "exclude": [ From 6c80e27054111f7ef617a3a8dfd91d5d1015a34c Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Tue, 11 Jul 2023 11:32:20 -0400 Subject: [PATCH 2/6] edited yarn.lock to invalidate deps cache in GH actions --- yarn.lock | 1 + 1 file changed, 1 insertion(+) diff --git a/yarn.lock b/yarn.lock index 491e9b015ec..bb3be115c0a 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1,5 +1,6 @@ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 +# manual edit to invalidate deps cache "@aashutoshrathi/word-wrap@^1.2.3": From c75f3ff761d4d906591361210dc76f858cab7b0f Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Tue, 11 Jul 2023 13:39:54 -0400 Subject: [PATCH 3/6] excluded file extensions from react-styles path transform in ESM build --- packages/transformer-react-styles-esm-imports/index.ts | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/packages/transformer-react-styles-esm-imports/index.ts b/packages/transformer-react-styles-esm-imports/index.ts index f7f3b68edf4..d79bf5c07e0 100644 --- a/packages/transformer-react-styles-esm-imports/index.ts +++ b/packages/transformer-react-styles-esm-imports/index.ts @@ -14,7 +14,9 @@ const transformer: ts.TransformerFactory = (context) => (sourceFi if (ts.isImportDeclaration(node)) { const text = node.moduleSpecifier.getFullText(sourceFile); - if (/@patternfly\/react-styles\/css/.test(text)) { + // Only transform imports from react-styles/css + // Exclude anything that already has an explicit file extension + if (/@patternfly\/react-styles\/css/.test(text) && !/\.[a-z]{1,5}('|");?$/.test(text)) { return factory.updateImportDeclaration( node, node.decorators, From 6e6e92fec4a070aa5e8652d218ec16c76f0f584d Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Tue, 11 Jul 2023 14:40:19 -0400 Subject: [PATCH 4/6] updated jest config to transform .mjs files --- jest.config.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/jest.config.js b/jest.config.js index 620b84b3b63..10cbcfd3396 100644 --- a/jest.config.js +++ b/jest.config.js @@ -10,7 +10,7 @@ module.exports = { ], roots: ['/packages'], transform: { - '^.+\\.[jt]sx?$': 'babel-jest', + '^.+\\.m?[jt]sx?$': 'babel-jest', '^.+\\.svg$': 'jest-transform-stub' }, setupFilesAfterEnv: ['/packages/testSetup.ts'], From 2da402443d3a05e8726adf5e4e4c6a90f4e74a6c Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Tue, 11 Jul 2023 15:11:25 -0400 Subject: [PATCH 5/6] updated pr action to invalidate deps cache if package files added/edited --- .github/workflows/pr-preview.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/pr-preview.yml b/.github/workflows/pr-preview.yml index 2f400db2655..d943b7a14e5 100644 --- a/.github/workflows/pr-preview.yml +++ b/.github/workflows/pr-preview.yml @@ -26,7 +26,7 @@ jobs: node_modules **/node_modules ~/.cache/Cypress - key: ${{ runner.os }}-yarn-14-${{ secrets.CACHE_VERSION }}-${{ hashFiles('yarn.lock') }} + key: ${{ runner.os }}-yarn-14-${{ secrets.CACHE_VERSION }}-${{ hashFiles('yarn.lock', 'packages/*/package.json') }} - run: yarn install --frozen-lockfile if: steps.yarn-cache.outputs.cache-hit != 'true' - uses: actions/cache@v2 From 7270ef27ec98ebcafd4e85bcb3a75b7f41f2ce14 Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Thu, 12 Oct 2023 11:56:17 -0400 Subject: [PATCH 6/6] chore(deps): Add ts-node to support executing typescript files directly --- package.json | 3 +- yarn.lock | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/package.json b/package.json index 45f13ae3a79..8e765fed703 100644 --- a/package.json +++ b/package.json @@ -64,7 +64,8 @@ "react-dom": "^18", "surge": "^0.23.1", "ts-patch": "^2.1.0", - "typescript": "^4.7.4" + "typescript": "^4.7.4", + "ts-node": "^10.9.1" }, "scripts": { "build": "yarn clean && yarn build:generate && yarn build:esm && yarn build:cjs && yarn build:subpaths && yarn build:single:packages", diff --git a/yarn.lock b/yarn.lock index bb3be115c0a..3c671239dea 100644 --- a/yarn.lock +++ b/yarn.lock @@ -1,6 +1,5 @@ # THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY. # yarn lockfile v1 -# manual edit to invalidate deps cache "@aashutoshrathi/word-wrap@^1.2.3": @@ -2877,6 +2876,13 @@ resolved "https://registry.npmjs.org/@bcoe/v8-coverage/-/v8-coverage-0.2.3.tgz" integrity sha512-0hYQ8SB4Db5zvZB4axdMHGwEaQjkZzFjQiN9LVYvIFB2nSUHW9tYpxWriPrWDASIxiaXax83REcLxuSdnGPZtw== +"@cspotcode/source-map-support@^0.8.0": + version "0.8.1" + resolved "https://registry.yarnpkg.com/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz#00629c35a688e05a88b1cda684fb9d5e73f000a1" + integrity sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw== + dependencies: + "@jridgewell/trace-mapping" "0.3.9" + "@cypress/request@^2.88.6": version "2.88.6" resolved "https://registry.npmjs.org/@cypress/request/-/request-2.88.6.tgz" @@ -3252,7 +3258,7 @@ resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.1.0.tgz#2203b118c157721addfe69d47b70465463066d78" integrity sha512-F2msla3tad+Mfht5cJq7LSXcdudKTWCVYUgw6pLFOOHSTtZlj6SWNYAp+AhuqLmWdBO2X5hPrLcu8cVP8fy28w== -"@jridgewell/resolve-uri@^3.1.0": +"@jridgewell/resolve-uri@^3.0.3", "@jridgewell/resolve-uri@^3.1.0": version "3.1.1" resolved "https://registry.yarnpkg.com/@jridgewell/resolve-uri/-/resolve-uri-3.1.1.tgz#c08679063f279615a3326583ba3a90d1d82cc721" integrity sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA== @@ -3280,6 +3286,14 @@ resolved "https://registry.yarnpkg.com/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz#d7c6e6755c78567a951e04ab52ef0fd26de59f32" integrity sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg== +"@jridgewell/trace-mapping@0.3.9": + version "0.3.9" + resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz#6534fd5933a53ba7cbf3a17615e273a0d1273ff9" + integrity sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ== + dependencies: + "@jridgewell/resolve-uri" "^3.0.3" + "@jridgewell/sourcemap-codec" "^1.4.10" + "@jridgewell/trace-mapping@^0.3.14": version "0.3.17" resolved "https://registry.yarnpkg.com/@jridgewell/trace-mapping/-/trace-mapping-0.3.17.tgz#793041277af9073b0951a7fe0f0d8c4c98c36985" @@ -4113,6 +4127,26 @@ resolved "https://registry.yarnpkg.com/@tootallnate/once/-/once-2.0.0.tgz#f544a148d3ab35801c1f633a7441fd87c2e484bf" integrity sha512-XCuKFP5PS55gnMVu3dty8KPatLqUoy/ZYzDzAGCQ8JNFCkLXzmI7vNHCR+XpbZaMWQK/vQubr7PkYq8g470J/A== +"@tsconfig/node10@^1.0.7": + version "1.0.9" + resolved "https://registry.yarnpkg.com/@tsconfig/node10/-/node10-1.0.9.tgz#df4907fc07a886922637b15e02d4cebc4c0021b2" + integrity sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA== + +"@tsconfig/node12@^1.0.7": + version "1.0.11" + resolved "https://registry.yarnpkg.com/@tsconfig/node12/-/node12-1.0.11.tgz#ee3def1f27d9ed66dac6e46a295cffb0152e058d" + integrity sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag== + +"@tsconfig/node14@^1.0.0": + version "1.0.3" + resolved "https://registry.yarnpkg.com/@tsconfig/node14/-/node14-1.0.3.tgz#e4386316284f00b98435bf40f72f75a09dabf6c1" + integrity sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow== + +"@tsconfig/node16@^1.0.2": + version "1.0.4" + resolved "https://registry.yarnpkg.com/@tsconfig/node16/-/node16-1.0.4.tgz#0b92dcc0cc1c81f6f306a381f28e31b1a56536e9" + integrity sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA== + "@tufjs/canonical-json@1.0.0": version "1.0.0" resolved "https://registry.yarnpkg.com/@tufjs/canonical-json/-/canonical-json-1.0.0.tgz#eade9fd1f537993bc1f0949f3aea276ecc4fab31" @@ -5201,7 +5235,7 @@ acorn-walk@^7.1.1: version "7.2.0" resolved "https://registry.npmjs.org/acorn-walk/-/acorn-walk-7.2.0.tgz" -acorn-walk@^8.0.0: +acorn-walk@^8.0.0, acorn-walk@^8.1.1: version "8.2.0" resolved "https://registry.yarnpkg.com/acorn-walk/-/acorn-walk-8.2.0.tgz#741210f2e2426454508853a2f44d0ab83b7f69c1" integrity sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA== @@ -5497,6 +5531,11 @@ are-we-there-yet@^3.0.0: delegates "^1.0.0" readable-stream "^3.6.0" +arg@^4.1.0: + version "4.1.3" + resolved "https://registry.yarnpkg.com/arg/-/arg-4.1.3.tgz#269fc7ad5b8e42cb63c896d5666017261c144089" + integrity sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA== + argparse@^1.0.7: version "1.0.10" resolved "https://registry.npmjs.org/argparse/-/argparse-1.0.10.tgz" @@ -7688,6 +7727,11 @@ create-hmac@^1.1.0, create-hmac@^1.1.4, create-hmac@^1.1.7: safe-buffer "^5.0.1" sha.js "^2.4.8" +create-require@^1.1.0: + version "1.1.1" + resolved "https://registry.yarnpkg.com/create-require/-/create-require-1.1.1.tgz#c1d7e8f1e5f6cfc9ff65f9cd352d37348756c333" + integrity sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ== + cross-fetch@3.1.5: version "3.1.5" resolved "https://registry.yarnpkg.com/cross-fetch/-/cross-fetch-3.1.5.tgz#e1389f44d9e7ba767907f7af8454787952ab534f" @@ -8326,6 +8370,11 @@ diff-sequences@^29.6.3: resolved "https://registry.yarnpkg.com/diff-sequences/-/diff-sequences-29.6.3.tgz#4deaf894d11407c51efc8418012f9e70b84ea921" integrity sha512-EjePK1srD3P08o2j4f0ExnylqRs5B9tJjcp9t1krH2qRi8CCdsYfwe9JgSLurFBWwq4uOlipzfk5fHNvwFKr8Q== +diff@^4.0.1: + version "4.0.2" + resolved "https://registry.yarnpkg.com/diff/-/diff-4.0.2.tgz#60f3aecb89d5fae520c11aa19efc2bb982aade7d" + integrity sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A== + diffie-hellman@^5.0.0: version "5.0.3" resolved "https://registry.npmjs.org/diffie-hellman/-/diffie-hellman-5.0.3.tgz" @@ -13590,6 +13639,11 @@ make-dir@^2.0.0, make-dir@^2.1.0: pify "^4.0.1" semver "^5.6.0" +make-error@^1.1.1: + version "1.3.6" + resolved "https://registry.yarnpkg.com/make-error/-/make-error-1.3.6.tgz#2eb2e37ea9b67c4891f684a1394799af484cf7a2" + integrity sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw== + make-fetch-happen@^10.0.3: version "10.2.1" resolved "https://registry.yarnpkg.com/make-fetch-happen/-/make-fetch-happen-10.2.1.tgz#f5e3835c5e9817b617f2770870d9492d28678164" @@ -18679,6 +18733,25 @@ ts-loader@^8.3.0: micromatch "^4.0.0" semver "^7.3.4" +ts-node@^10.9.1: + version "10.9.1" + resolved "https://registry.yarnpkg.com/ts-node/-/ts-node-10.9.1.tgz#e73de9102958af9e1f0b168a6ff320e25adcff4b" + integrity sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw== + dependencies: + "@cspotcode/source-map-support" "^0.8.0" + "@tsconfig/node10" "^1.0.7" + "@tsconfig/node12" "^1.0.7" + "@tsconfig/node14" "^1.0.0" + "@tsconfig/node16" "^1.0.2" + acorn "^8.4.1" + acorn-walk "^8.1.1" + arg "^4.1.0" + create-require "^1.1.0" + diff "^4.0.1" + make-error "^1.1.1" + v8-compile-cache-lib "^3.0.1" + yn "3.1.1" + ts-patch@^2.1.0: version "2.1.0" resolved "https://registry.yarnpkg.com/ts-patch/-/ts-patch-2.1.0.tgz#b4ba3e3f029144d7c4c6566916ebd5a453f070f5" @@ -19319,6 +19392,11 @@ uuid@^9.0.0: resolved "https://registry.yarnpkg.com/uuid/-/uuid-9.0.0.tgz#592f550650024a38ceb0c562f2f6aa435761efb5" integrity sha512-MXcSTerfPa4uqyzStbRoTgt5XIe3x5+42+q1sDuy3R5MDk66URdLMOZe5aPX/SQd+kuYAh0FdP/pO28IkQyTeg== +v8-compile-cache-lib@^3.0.1: + version "3.0.1" + resolved "https://registry.yarnpkg.com/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz#6336e8d71965cb3d35a1bbb7868445a7c05264bf" + integrity sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg== + v8-compile-cache@2.3.0: version "2.3.0" resolved "https://registry.yarnpkg.com/v8-compile-cache/-/v8-compile-cache-2.3.0.tgz#2de19618c66dc247dcfb6f99338035d8245a2cee" @@ -20405,6 +20483,11 @@ ylru@^1.2.0: version "1.2.1" resolved "https://registry.npmjs.org/ylru/-/ylru-1.2.1.tgz" +yn@3.1.1: + version "3.1.1" + resolved "https://registry.yarnpkg.com/yn/-/yn-3.1.1.tgz#1e87401a09d767c1d5eab26a6e4c185182d2eb50" + integrity sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q== + yocto-queue@^0.1.0: version "0.1.0" resolved "https://registry.yarnpkg.com/yocto-queue/-/yocto-queue-0.1.0.tgz#0294eb3dee05028d31ee1a5fa2c556a6aaf10a1b"