forked from patternfly/patternfly-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(duplicate-cjs): add transformer-cjs-imports (patternfly#4910)
* fix(duplicate-cjs): add transformer-cjs-imports * fix lint
- Loading branch information
Showing
6 changed files
with
70 additions
and
4 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
// https://levelup.gitconnected.com/writing-typescript-custom-ast-transformer-part-2-5322c2b1660e | ||
const ts = require('typescript'); | ||
|
||
/** | ||
* We import `@patternfly/react-[tokens,icons]/dist/js` to avoid parsing massive modules | ||
* as well as for cross-module imports with types to `@patternfly/react-core`. | ||
* HOWEVER we would like for the ESM output to reference `@patternfly/react-[]/dist/esm` | ||
* for better tree-shaking and smaller bundlers. | ||
* A large offender of this is Tooltip's Popover helper. | ||
* | ||
* @param context TS context | ||
*/ | ||
function transformerCJSImports(context) { | ||
// Only transform for ESM build | ||
if (context.getCompilerOptions().target !== 2) { | ||
return node => node; | ||
} | ||
/** | ||
* If a node is an import, change its moduleSpecifier | ||
* Otherwise iterate over all its childern. | ||
* | ||
* @param node TS Node | ||
*/ | ||
function visit(node) { | ||
if (ts.isImportDeclaration(node) && /@patternfly\/.*\/dist\/js/.test(node.moduleSpecifier.text)) { | ||
const newNode = ts.getMutableClone(node); | ||
const newPath = node.moduleSpecifier.text.replace(/dist\/js/, 'dist/esm'); | ||
newNode.moduleSpecifier = ts.createStringLiteral(newPath); | ||
return newNode; | ||
} | ||
return ts.visitEachChild(node, child => visit(child), context); | ||
} | ||
return node => ts.visitNode(node, visit); | ||
} | ||
|
||
module.exports = () => ({ | ||
before: transformerCJSImports | ||
}); |
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,11 @@ | ||
{ | ||
"name": "transformer-cjs-imports", | ||
"version": "4.0.0", | ||
"description": "Transform CJS imports to ESM in typescript depending on module target.", | ||
"main": "index.js", | ||
"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