-
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.
- Loading branch information
1 parent
67e38d7
commit 07d2d8c
Showing
3 changed files
with
119 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,89 @@ | ||
const fs = require('fs'); | ||
const path = require('path'); | ||
const terser = require('terser'); | ||
const glob = require('glob'); | ||
const jsonminify = require('jsonminify'); | ||
|
||
// Diretório de saída da build | ||
const outputDir = 'dist/darkmode-angular'; | ||
|
||
// Função para minificar um arquivo e remover a linha `//# sourceMappingURL=` | ||
async function minifyFile(filePath) { | ||
try { | ||
// Lê o conteúdo do arquivo | ||
const content = fs.readFileSync(filePath, 'utf8'); | ||
|
||
// Minifica o conteúdo do arquivo com remoção de comentários | ||
const result = await terser.minify(content, { | ||
format: { comments: false } // Remove comentários | ||
}); | ||
|
||
if (result.error) { | ||
console.error(`Error minifying ${filePath}:`, result.error); | ||
return; | ||
} | ||
|
||
// Remove a linha `//# sourceMappingURL=` | ||
const minifiedContent = result.code.replace(/\/\/# sourceMappingURL=.*\s*/, ''); | ||
|
||
// Grava o conteúdo minificado de volta ao arquivo | ||
fs.writeFileSync(filePath, minifiedContent, 'utf8'); | ||
console.log(`Minified ${filePath}`); | ||
} catch (error) { | ||
console.error(`Error processing ${filePath}:`, error); | ||
} | ||
} | ||
|
||
// Função para minificar o package.json usando jsonminify | ||
function minifyPackageJson() { | ||
try { | ||
const packageJsonPath = path.join(outputDir, 'package.json'); | ||
const content = fs.readFileSync(packageJsonPath, 'utf8'); | ||
|
||
// Minifica o JSON | ||
const minifiedContent = jsonminify(content); | ||
|
||
fs.writeFileSync(packageJsonPath, minifiedContent, 'utf8'); | ||
console.log(`Minified ${packageJsonPath}`); | ||
} catch (error) { | ||
console.error('Error minifying package.json:', error); | ||
} | ||
} | ||
|
||
// Função para processar todos os arquivos | ||
async function processFiles() { | ||
try { | ||
// Encontre todos os arquivos .js e .mjs no diretório de saída | ||
const jsFiles = glob.sync(path.join(outputDir, '**', '*.js')); | ||
const mjsFiles = glob.sync(path.join(outputDir, '**', '*.mjs')); | ||
|
||
// Minifique todos os arquivos encontrados | ||
for (const file of [...jsFiles, ...mjsFiles]) { | ||
await minifyFile(file); | ||
} | ||
|
||
// Remova os arquivos source map | ||
const sourceMaps = glob.sync(path.join(outputDir, '**', '*.map')); | ||
for (const mapFile of sourceMaps) { | ||
fs.unlinkSync(mapFile); | ||
console.log(`Removed source map ${mapFile}`); | ||
} | ||
|
||
// Minifique o package.json | ||
minifyPackageJson(); | ||
|
||
// Remova o .npmignore | ||
const npmIgnorePath = path.join(outputDir, '.npmignore'); | ||
if (fs.existsSync(npmIgnorePath)) { | ||
fs.unlinkSync(npmIgnorePath); | ||
console.log(`Removed .npmignore`); | ||
} | ||
|
||
console.log('Post-build processing completed.'); | ||
} catch (error) { | ||
console.error('Error during post-build processing:', error); | ||
} | ||
} | ||
|
||
// Execute o script | ||
processFiles(); |