From 07d2d8c85dce1bcac3f8068106a6186952f5c744 Mon Sep 17 00:00:00 2001 From: lucasferreiralimax Date: Fri, 18 Oct 2024 02:43:33 -0300 Subject: [PATCH] add post build scripts --- package-lock.json | 12 ++++++ package.json | 19 ++++++++- scripts/post-build.js | 89 +++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 119 insertions(+), 1 deletion(-) create mode 100644 scripts/post-build.js diff --git a/package-lock.json b/package-lock.json index 5b9abd3..a82453e 100644 --- a/package-lock.json +++ b/package-lock.json @@ -26,6 +26,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", @@ -9151,6 +9152,17 @@ "graceful-fs": "^4.1.6" } }, + "node_modules/jsonminify": { + "version": "0.4.2", + "resolved": "https://registry.npmjs.org/jsonminify/-/jsonminify-0.4.2.tgz", + "integrity": "sha512-mEtP5ECD0293D+s45JhDutqF5mFCkWY8ClrPFxjSFR2KUoantofky7noSzyKnAnD9Gd8pXHZSUd5bgzLDUBbfA==", + "dev": true, + "license": "MIT", + "engines": { + "node": ">=0.8.0", + "npm": ">=1.1.0" + } + }, "node_modules/jsonparse": { "version": "1.3.1", "resolved": "https://registry.npmjs.org/jsonparse/-/jsonparse-1.3.1.tgz", diff --git a/package.json b/package.json index e59223a..0375b25 100644 --- a/package.json +++ b/package.json @@ -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": { @@ -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", diff --git a/scripts/post-build.js b/scripts/post-build.js new file mode 100644 index 0000000..9a4d993 --- /dev/null +++ b/scripts/post-build.js @@ -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();