-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(dev): use ts-morph to parse template configs (#387)
Previous to this change, we used esbuild to compile the templates into modules in order to pull off the config const. This led to various issues for people. It works fine with Vite since Vite does a lot of CJS/polyfill magic, but we weren't handling that with esbuild. This was overkill anyways as we only need to grab the config off of the templates, so compilation is irrelevant at this step. --------- Co-authored-by: github-actions <41898282+github-actions[bot]@users.noreply.github.com>
- Loading branch information
1 parent
a2ad104
commit bc51b80
Showing
13 changed files
with
328 additions
and
68 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 |
---|---|---|
|
@@ -1283,6 +1283,36 @@ Repository: https://github.com/yargs/yargs-parser.git | |
|
||
----------- | ||
|
||
The following npm package may be included in this product: | ||
|
||
- [email protected] | ||
|
||
This package contains the following license and notice below: | ||
|
||
The MIT License (MIT) | ||
|
||
Copyright (c) 2017-2023 David Sherret | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in all | ||
copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | ||
SOFTWARE. | ||
|
||
----------- | ||
|
||
The following npm package may be included in this product: | ||
|
||
- [email protected] | ||
|
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
113 changes: 113 additions & 0 deletions
113
packages/pages/src/common/src/parsers/sourceFileParser.ts
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,113 @@ | ||
import { | ||
Project, | ||
SourceFile, | ||
SyntaxKind, | ||
FunctionDeclaration, | ||
ObjectLiteralExpression, | ||
ArrowFunction, | ||
} from "ts-morph"; | ||
import vm from "node:vm"; | ||
import typescript from "typescript"; | ||
import { processEnvVariables } from "../../../util/processEnvVariables.js"; | ||
|
||
export function createTsMorphProject() { | ||
return new Project({ | ||
compilerOptions: { | ||
jsx: typescript.JsxEmit.ReactJSX, | ||
}, | ||
}); | ||
} | ||
|
||
export default class SourceFileParser { | ||
private sourceFile: SourceFile; | ||
|
||
constructor( | ||
private filepath: string, | ||
project: Project | ||
) { | ||
if (!project.getSourceFile(filepath)) { | ||
project.addSourceFileAtPath(filepath); | ||
} | ||
this.sourceFile = project.getSourceFileOrThrow(filepath); | ||
} | ||
|
||
getExportedObjectExpression( | ||
variableName: string | ||
): ObjectLiteralExpression | undefined { | ||
const variableStatement = this.sourceFile | ||
.getVariableStatements() | ||
.find((variableStatement) => { | ||
return ( | ||
variableStatement.isExported() && | ||
variableStatement | ||
.getFirstDescendantByKindOrThrow(SyntaxKind.VariableDeclaration) | ||
.getName() === variableName | ||
); | ||
}); | ||
if (!variableStatement) { | ||
return; | ||
} | ||
const objectLiteralExp = variableStatement.getFirstDescendantByKind( | ||
SyntaxKind.ObjectLiteralExpression | ||
); | ||
if (!objectLiteralExp) { | ||
throw new Error( | ||
`Could not find ObjectLiteralExpression within \`${variableStatement.getFullText()}\`.` | ||
); | ||
} | ||
return objectLiteralExp; | ||
} | ||
|
||
getExportFunctionExpression( | ||
functionName?: string | ||
): ObjectLiteralExpression[] | ObjectLiteralExpression | undefined { | ||
if (functionName) { | ||
const functionDeclaration = this.sourceFile.getFunction( | ||
(f) => f.isExported() && f.getName() === functionName | ||
); | ||
const objectLiteralExp = functionDeclaration?.getFirstDescendantByKind( | ||
SyntaxKind.ObjectLiteralExpression | ||
); | ||
return objectLiteralExp; | ||
} | ||
const functionDeclarations = this.sourceFile.getFunctions(); | ||
const objectLiteralExpressions: ObjectLiteralExpression[] = []; | ||
functionDeclarations.forEach((fd) => { | ||
if (fd.isExported()) { | ||
objectLiteralExpressions.push( | ||
fd.getFirstAncestorByKindOrThrow(SyntaxKind.ObjectLiteralExpression) | ||
); | ||
} | ||
}); | ||
return objectLiteralExpressions; | ||
} | ||
|
||
getFunctions() { | ||
return this.sourceFile.getFunctions(); | ||
} | ||
|
||
getFunctionNode( | ||
funcName: string | ||
): FunctionDeclaration | ArrowFunction | undefined { | ||
return ( | ||
this.sourceFile.getFunction(funcName) ?? | ||
this.sourceFile | ||
.getVariableDeclaration(funcName) | ||
?.getFirstChildByKind(SyntaxKind.ArrowFunction) | ||
); | ||
} | ||
|
||
/** | ||
* This function takes in an {@link ObjectLiteralExpression} and returns it's data. | ||
* | ||
* It converts a js object string and converts it into an object using vm.runInNewContext, | ||
* which can be thought of as a safe version of `eval`. Note that we cannot use JSON.parse here, | ||
* because we are working with a js object not a JSON. | ||
*/ | ||
getCompiledObjectLiteral<T>(objectLiteralExp: ObjectLiteralExpression): T { | ||
return vm.runInNewContext( | ||
"(" + objectLiteralExp.getText() + ")", | ||
processEnvVariables("YEXT_PUBLIC") | ||
); | ||
} | ||
} |
25 changes: 25 additions & 0 deletions
25
packages/pages/src/common/src/parsers/templateConfigParser.ts
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,25 @@ | ||
import { TemplateConfig } from "../template/types.js"; | ||
import SourceFileParser from "./sourceFileParser.js"; | ||
|
||
export const TEMPLATE_CONFIG_VARIABLE_NAME = "config"; | ||
|
||
/** | ||
* TemplateConfigParser is a class for parsing the template config. | ||
*/ | ||
export default class TemplateConfigParser { | ||
constructor(private sourceFileParser: SourceFileParser) {} | ||
|
||
/** Returns the template config exported from the page if one is present. */ | ||
getTemplateConfig(): TemplateConfig | undefined { | ||
const configObjectLiteralExp = | ||
this.sourceFileParser.getExportedObjectExpression( | ||
TEMPLATE_CONFIG_VARIABLE_NAME | ||
); | ||
return ( | ||
configObjectLiteralExp && | ||
this.sourceFileParser.getCompiledObjectLiteral<TemplateConfig>( | ||
configObjectLiteralExp | ||
) | ||
); | ||
} | ||
} |
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
Oops, something went wrong.