-
Notifications
You must be signed in to change notification settings - Fork 352
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
bug(build): fixed built ESM files importing CJS files from react-styl…
…es (#9347) * bug(build): fixed built ESM files importing CJS files from react-styles * edited yarn.lock to invalidate deps cache in GH actions * excluded file extensions from react-styles path transform in ESM build * updated jest config to transform .mjs files * updated pr action to invalidate deps cache if package files added/edited * chore(deps): Add ts-node to support executing typescript files directly
- Loading branch information
1 parent
a772c6d
commit 817e9bd
Showing
7 changed files
with
140 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
// https://levelup.gitconnected.com/writing-typescript-custom-ast-transformer-part-2-5322c2b1660e | ||
import * as ts from 'typescript'; | ||
|
||
const transformer: ts.TransformerFactory<ts.SourceFile> = (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); | ||
|
||
// 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, | ||
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; |
12 changes: 12 additions & 0 deletions
12
packages/transformer-react-styles-esm-imports/package.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters