-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
started the resource to convert imported images to base64 and add the…
…m to the code
- Loading branch information
Showing
8 changed files
with
782 additions
and
42 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,28 @@ | ||
const { removeBlankLines } = require("../parse"); | ||
|
||
/** | ||
* Checa se a assinatura "as stateful" foi encontrado no topo do arquivo. | ||
* @param {*} content | ||
* @returns | ||
*/ | ||
const asStatefulCheck = (content) => { | ||
// NOTE: Checa se tem "as stateful", se tiver, deve ser tratado como um Widget/Stateful | ||
const hasAsStatefulType = content.includes('"as stateful"'); | ||
|
||
if (hasAsStatefulType) { | ||
content = content.replace('"as stateful";', ""); | ||
content = content.replace('"as stateful"', ""); | ||
|
||
return { | ||
updatedContent: removeBlankLines(content), | ||
asStatefulSignalFound: true, | ||
}; | ||
} | ||
|
||
return { | ||
updatedContent: content, | ||
asStatefulSignalFound: false, | ||
}; | ||
}; | ||
|
||
module.exports = asStatefulCheck; |
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,58 @@ | ||
const parser = require("@babel/parser"); | ||
const traverse = require("@babel/traverse").default; | ||
const t = require("@babel/types"); | ||
|
||
/** | ||
* Verifica se o componente é de um tipo específico, se for, retorna true, senão, false. | ||
* | ||
* ex: | ||
* | ||
* ``` | ||
* const code = `const Foo: AsStateful = () => {...}` | ||
* const code2 = `const Foo: Other = () => {...}` | ||
* const signatureType = "AsStateful"; | ||
* | ||
* hasSignatureType(code, signatureType); // true; | ||
* hasSignatureType(code2, signatureType); // false; | ||
* ``` | ||
* | ||
* @param {*} code | ||
* @param {*} signatureType | ||
* @returns | ||
*/ | ||
function hasSignatureType(code, signatureType) { | ||
const ast = parser.parse(code, { | ||
sourceType: "module", | ||
plugins: ["jsx", "typescript"], | ||
}); | ||
|
||
let found = false; | ||
|
||
traverse(ast, { | ||
VariableDeclarator(path) { | ||
if ( | ||
path.node.id.typeAnnotation && | ||
t.isTSTypeReference(path.node.id.typeAnnotation.typeAnnotation) && | ||
path.node.id.typeAnnotation.typeAnnotation.typeName.name === | ||
signatureType | ||
) { | ||
found = true; | ||
path.stop(); | ||
} | ||
}, | ||
FunctionDeclaration(path) { | ||
if ( | ||
path.node.returnType && | ||
t.isTSTypeReference(path.node.returnType.typeAnnotation) && | ||
path.node.returnType.typeAnnotation.typeName.name === signatureType | ||
) { | ||
found = true; | ||
path.stop(); | ||
} | ||
}, | ||
}); | ||
|
||
return found; | ||
} | ||
|
||
module.exports = hasSignatureType; |
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,66 @@ | ||
const parser = require("@babel/parser"); | ||
const traverse = require("@babel/traverse").default; | ||
const nodePath = require("path"); | ||
const sharp = require("sharp"); | ||
|
||
function isImageFile(filePath) { | ||
const imageExtensions = [".png", ".jpg", ".jpeg", ".gif", ".svg"]; | ||
return imageExtensions.includes(nodePath.extname(filePath).toLowerCase()); | ||
} | ||
|
||
async function getBase64(filePath, quality = 80) { | ||
const fileBuffer = await sharp(filePath) | ||
.resize({ width: 800 }) // Adjust the size as needed | ||
.jpeg({ quality }) // Adjust the quality as needed | ||
.toBuffer(); | ||
|
||
return `data:image/${nodePath | ||
.extname(filePath) | ||
.slice(1)};base64,${fileBuffer.toString("base64")}`; | ||
} | ||
|
||
/** | ||
* Processo que captura os imports de imagens e guarda seu conteúdo em base64 para | ||
* ser usado posteriormente no processo do compilador. | ||
* @param {*} code | ||
* @param {*} fileDir | ||
* @param {*} quality | ||
* @returns | ||
*/ | ||
async function processImportsToBase64(code, fileDir, quality) { | ||
const ast = parser.parse(code, { | ||
sourceType: "module", | ||
plugins: ["jsx", "typescript"], | ||
}); | ||
|
||
const imageImports = []; | ||
|
||
const promises = []; | ||
|
||
traverse(ast, { | ||
ImportDeclaration(path) { | ||
const importPath = path.node.source.value; | ||
const importName = path.node.specifiers[0].local.name; | ||
const fullImportPath = nodePath.resolve( | ||
nodePath.dirname(fileDir), | ||
importPath, | ||
); | ||
|
||
if (isImageFile(fullImportPath)) { | ||
promises.push( | ||
getBase64(fullImportPath, quality).then((base64Content) => { | ||
imageImports.push({ | ||
imageKey: importName, | ||
imageBase64Content: base64Content, | ||
}); | ||
}), | ||
); | ||
} | ||
}, | ||
}); | ||
|
||
await Promise.all(promises); | ||
return imageImports; | ||
} | ||
|
||
module.exports = processImportsToBase64; |
Oops, something went wrong.