-
Notifications
You must be signed in to change notification settings - Fork 96
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(tools): ts transform to add static version to elements
- Loading branch information
1 parent
77dff66
commit 2333aec
Showing
5 changed files
with
128 additions
and
39 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
--- | ||
"@patternfly/pfe-tools": minor | ||
--- | ||
**TypeScript**: Add static version transformer. This adds a runtime-only | ||
static `version` field to custom element classes. | ||
|
||
```js | ||
import '@patternfly/elements/pf-button/pf-button.js'; | ||
const PFE_VERSION = | ||
await customElements.whenDefined('pf-button') | ||
.then(PfButton => PfButton.version); | ||
``` |
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 |
---|---|---|
|
@@ -40,7 +40,8 @@ | |
"./test/render-to-string.js": "./test/render-to-string.js", | ||
"./test/stub-logger.js": "./test/stub-logger.js", | ||
"./test/utils.js": "./test/utils.js", | ||
"./typescript/transformers/css-imports.cjs": "./typescript/transformers/css-imports.cjs" | ||
"./typescript/transformers/css-imports.cjs": "./typescript/transformers/css-imports.cjs", | ||
"./typescript/transformers/static-version.cjs": "./typescript/transformers/static-version.cjs" | ||
}, | ||
"contributors": [ | ||
"Kyle Buchanan <[email protected]> (https://github.com/kylebuch8)", | ||
|
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
84 changes: 84 additions & 0 deletions
84
tools/pfe-tools/typescript/transformers/static-version.cjs
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,84 @@ | ||
const ts = require('typescript'); | ||
const fs = require('node:fs'); | ||
const path = require('node:path'); | ||
|
||
/** | ||
* @param {ts.ModifierLike} mod | ||
* @returns {mod is ts.ExportKeyword} | ||
*/ | ||
const isExportKeyword = mod => | ||
mod.kind === ts.SyntaxKind.ExportKeyword; | ||
|
||
/** | ||
* @param {ts.ModifierLike} mod | ||
* @returns {mod is ts.Decorator} | ||
*/ | ||
const isCustomElementDecorator = mod => | ||
ts.isDecorator(mod) | ||
&& ts.isCallExpression(mod.expression) | ||
&& ts.isIdentifier(mod.expression.expression) | ||
&& mod.expression.expression.escapedText === 'customElement'; | ||
|
||
/** | ||
* @param {ts.Node} node | ||
* @returns {node is ts.ClassDeclaration} | ||
*/ | ||
const isExportCustomElementClass = node => | ||
ts.isClassDeclaration(node) | ||
&& !!node.modifiers?.some(isExportKeyword) | ||
&& !!node.modifiers?.some(isCustomElementDecorator); | ||
|
||
/** @param {string} dir */ | ||
function findPackageDir(dir) { | ||
if (fs.existsSync(path.join(dir, 'package.json'))) { | ||
return dir; | ||
} | ||
const parentDir = path.resolve(dir, '..'); | ||
if (dir === parentDir) { | ||
return null; | ||
} | ||
return findPackageDir(parentDir); | ||
} | ||
|
||
/** @param {string} filePath */ | ||
function getNearestPackageJson(filePath) { | ||
const parentDir = path.dirname(filePath); | ||
const packageDir = findPackageDir(parentDir); | ||
if (packageDir) { | ||
const filePath = path.normalize(`${packageDir}/package.json`); | ||
return require(filePath); | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
/** @returns {ts.TransformerFactory<ts.SourceFile>} */ | ||
module.exports = () => ctx => { | ||
return sourceFile => ts.visitEachChild( | ||
sourceFile, | ||
function addVersionVisitor(node) { | ||
if (isExportCustomElementClass(node)) { | ||
const { fileName } = node.getSourceFile(); | ||
const packageJson = getNearestPackageJson(fileName); | ||
if (packageJson?.version) { | ||
return ctx.factory.createClassDeclaration( | ||
node.modifiers, | ||
node.name, | ||
node.typeParameters, | ||
node.heritageClauses, | ||
node.members.concat(ctx.factory.createPropertyDeclaration( | ||
[ctx.factory.createModifier(ts.SyntaxKind.StaticKeyword)], | ||
'version', | ||
undefined, | ||
undefined, | ||
ctx.factory.createStringLiteral(packageJson.version) | ||
)) | ||
); | ||
} | ||
} | ||
return node; | ||
}, | ||
ctx | ||
); | ||
}; | ||
|
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