-
Notifications
You must be signed in to change notification settings - Fork 42
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
3759454
commit 02cab7b
Showing
5 changed files
with
95 additions
and
17 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 |
---|---|---|
@@ -1,18 +1,28 @@ | ||
// import { ESLintProgram } from 'vue-eslint-parser/ast/nodes'; | ||
import { FileInfo } from '../astParse'; | ||
import { FileInfo, TransformationResult } from '../astParse'; | ||
|
||
export type ASTTransformation<Params = void> = { | ||
(fileInfo: FileInfo, params: Params): string | null | ||
(fileInfo: FileInfo, params: Params): TransformationResult | null | ||
} | ||
|
||
export enum TransformationType { | ||
// eslint-disable-next-line no-unused-vars | ||
addJsxTransformation = 'addJsxTransformation', | ||
// eslint-disable-next-line no-unused-vars | ||
removeHtmlLangInTemplateTransformation = 'removeHtmlLangInTemplateTransformation', | ||
// eslint-disable-next-line no-unused-vars | ||
indexHtmlTransformation = 'indexHtmlTransformation' | ||
} | ||
|
||
export const transformationMap: { | ||
[name: string]: { | ||
astTransform: ASTTransformation, | ||
needReparse: boolean, | ||
needWriteToOriginFile: boolean, | ||
extensions: string[] | ||
extensions: string[], | ||
transformationType: TransformationType | ||
} | ||
} = { | ||
addJsxTransformation: require('./addJsxTransformation'), | ||
removeHtmlLangInTemplateTransformation: require('./removeHtmlLangInTemplateTransformation') | ||
removeHtmlLangInTemplateTransformation: require('./removeHtmlLangInTemplateTransformation'), | ||
indexHtmlTransformation: require('./indexHtmlTransformation') | ||
} |
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,45 @@ | ||
import type { ASTTransformation } from './index' | ||
import { FileInfo, TransformationResult } from '../astParse' | ||
import { ESLintProgram } from 'vue-eslint-parser/ast'; | ||
import * as parser from 'vue-eslint-parser'; | ||
import { Node } from 'vue-eslint-parser/ast/nodes' | ||
import { TransformationType } from './index'; | ||
|
||
const INDEX_HTML_PATH: string = 'public/index.html' | ||
|
||
export const astTransform:ASTTransformation = (fileInfo: FileInfo) => { | ||
if (!fileInfo.path.replace('\\', '/').endsWith(INDEX_HTML_PATH)) { | ||
return null | ||
} | ||
|
||
const templateStart: string = '<template>' | ||
const templateEnd: string = '</template>' | ||
const htmlContent = `${templateStart}${fileInfo.source}${templateEnd}` | ||
const htmlAST : ESLintProgram = parser.parse(htmlContent, { sourceType: 'module' }) | ||
const root: Node = htmlAST.templateBody | ||
let bodyNode | ||
parser.AST.traverseNodes(root, { | ||
enterNode (node: Node) { | ||
if (node.type === 'VElement' && node.name === 'body') { | ||
bodyNode = node | ||
} | ||
}, | ||
leaveNode () {} | ||
}) | ||
const transformedHtml: string = htmlContent.slice(0, bodyNode.endTag.range[0] - 1) + 'abcd' + htmlContent.slice(bodyNode.endTag.range[0] - 1) | ||
const result: TransformationResult = { | ||
fileInfo: fileInfo, | ||
content: transformedHtml, | ||
type: TransformationType.removeHtmlLangInTemplateTransformation | ||
} | ||
|
||
return result | ||
} | ||
|
||
export const needReparse: boolean = false | ||
|
||
export const needWriteToOriginFile: boolean = false | ||
|
||
export const extensions: string[] = ['.html'] | ||
|
||
export const transformationType: TransformationType = TransformationType.indexHtmlTransformation |
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