Skip to content

Commit

Permalink
add post build scripts
Browse files Browse the repository at this point in the history
  • Loading branch information
lucasferreiralimax committed Oct 18, 2024
1 parent 67e38d7 commit 07d2d8c
Show file tree
Hide file tree
Showing 3 changed files with 119 additions and 1 deletion.
12 changes: 12 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

19 changes: 18 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,29 @@
{
"name": "darkmode-angular",
"version": "0.1.1",
"author": "lucasferreiralimax (https://github.com/lucasferreiralimax)",
"repository": "https://github.com/criar-art/darkmode-angular",
"bugs": {
"url": "https://github.com/criar-art/darkmode-angular/issues"
},
"homepage": "https://darkmode-angular.web.app",
"keywords": [
"angular",
"components",
"darkmode",
"ui"
],
"scripts": {
"ng": "ng",
"start": "ng serve",
"build": "ng build",
"build:module": "ng build darkmode-angular && npm run postbuild",
"version": "cd projects/darkmode-angular && npm version patch",
"publish": "cd dist/darkmode-angular && npm publish",
"watch": "ng build --watch --configuration development",
"test": "ng test",
"test:ci": "ng test --watch=false --browsers=ChromeHeadless"
"test:ci": "ng test --watch=false --browsers=ChromeHeadless",
"postbuild": "node scripts/post-build.js"
},
"private": true,
"dependencies": {
Expand All @@ -29,6 +45,7 @@
"@angular/compiler-cli": "^18.2.2",
"@types/jasmine": "^5.1.4",
"jasmine-core": "^5.2.0",
"jsonminify": "^0.4.2",
"karma": "~6.4.3",
"karma-chrome-launcher": "~3.2.0",
"karma-coverage": "~2.2.1",
Expand Down
89 changes: 89 additions & 0 deletions scripts/post-build.js
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();

0 comments on commit 07d2d8c

Please sign in to comment.