Skip to content

Build e Deployment โ€‹

Questa guida copre il processo di build e deployment delle estensioni KIMU, inclusi strumenti, configurazioni e best practices.

Processo di Build โ€‹

Build di una Singola Estensione โ€‹

Il framework KIMU fornisce script dedicati per compilare le estensioni:

bash
# Compila una singola estensione
node scripts/build-extension.js my-extension

# Oppure usa npm script (se configurato)
npm run build:extension my-extension

Build di Tutte le Estensioni โ€‹

bash
# Compila tutte le estensioni
node scripts/build-all-extensions.js

# Oppure usa npm script
npm run build:extensions

Configurazione di Build โ€‹

ESBuild Configuration โ€‹

Le estensioni vengono compilate usando ESBuild con la seguente configurazione:

javascript
// scripts/build-extension.js
const buildConfig = {
  entryPoints: [entry],         // src/extensions/nome/component.ts
  bundle: true,                 // Bundle tutte le dipendenze
  minify: true,                 // Minifica il codice
  format: 'esm',               // Formato ES modules
  outfile: outFile,            // dist/extensions/nome/component.js
  platform: 'browser',        // Target browser
  target: 'es2020',           // Target ES2020
  sourcemap: true,            // Genera source maps
  external: [                 // Dipendenze esterne
    'kimu-core',
    'kimu-framework'
  ]
};

Configurazione Personalizzata โ€‹

Puoi personalizzare la build aggiungendo un file build.config.js nella cartella dell'estensione:

javascript
// src/extensions/my-extension/build.config.js
export default {
  // Personalizzazioni ESBuild
  minify: false,              // Disabilita minificazione per debug
  sourcemap: 'inline',       // Source maps inline
  target: 'es2022',          // Target piรน recente
  
  // Plugin personalizzati
  plugins: [
    // Plugin per CSS
    cssPlugin(),
    // Plugin per asset
    assetPlugin()
  ],
  
  // Definizioni globali
  define: {
    '__DEV__': 'true',
    '__VERSION__': '"1.0.0"'
  },
  
  // Dipendenze esterne aggiuntive
  external: [
    'my-custom-lib'
  ]
};

Struttura di Output โ€‹

Directory di Build โ€‹

dist/
โ”œโ”€โ”€ extensions/
โ”‚   โ”œโ”€โ”€ my-extension/
โ”‚   โ”‚   โ”œโ”€โ”€ component.js        # Entry point compilato
โ”‚   โ”‚   โ”œโ”€โ”€ component.js.map    # Source map
โ”‚   โ”‚   โ”œโ”€โ”€ assets/             # Asset processati
โ”‚   โ”‚   โ”‚   โ”œโ”€โ”€ styles.css
โ”‚   โ”‚   โ”‚   โ””โ”€โ”€ images/
โ”‚   โ”‚   โ””โ”€โ”€ manifest.json       # Manifest copiato
โ”‚   โ””โ”€โ”€ other-extension/
โ”‚       โ””โ”€โ”€ ...
โ””โ”€โ”€ core/                       # Core framework
    โ””โ”€โ”€ ...

Manifest Compilato โ€‹

Durante il build, viene generato un manifest compilato che include informazioni aggiuntive:

json
{
  "tag": "my-extension",
  "path": "my-extension",
  "name": "My Extension",
  "version": "1.0.0",
  "buildInfo": {
    "buildTime": "2024-01-15T10:30:00Z",
    "buildHash": "abc123def456",
    "sourceSize": 15420,
    "minifiedSize": 8932,
    "gzipSize": 3241
  },
  "assets": [
    {
      "file": "component.js",
      "size": 8932,
      "hash": "sha256:def789..."
    }
  ]
}

Asset Processing โ€‹

Gestione CSS โ€‹

typescript
// CSS importato come stringa
import styles from './styles.css?inline';

@KimuComponent({
  tag: 'my-extension',
  styles // Incluso automaticamente
})
export class MyExtension extends HTMLElement {
  // ...
}

Gestione Immagini โ€‹

typescript
// Import di immagini
import logoUrl from './assets/logo.png';

export class MyExtension extends HTMLElement {
  render() {
    this.shadowRoot.innerHTML = `
      <img src="${logoUrl}" alt="Logo" />
    `;
  }
}

Asset Dinamici โ€‹

typescript
import { KimuAssetManager } from '../../core/kimu-asset-manager';

export class MyExtension extends HTMLElement {
  private async loadDynamicAsset() {
    const assetManager = KimuAssetManager.getInstance();
    const imageUrl = await assetManager.getAsset('my-extension/dynamic-image.png');
    return imageUrl;
  }
}

Environment Configuration โ€‹

Configurazione Multi-Environment โ€‹

javascript
// scripts/build-extension.js
const environments = {
  development: {
    minify: false,
    sourcemap: true,
    define: {
      '__DEV__': 'true',
      '__API_URL__': '"http://localhost:3000"'
    }
  },
  
  production: {
    minify: true,
    sourcemap: false,
    define: {
      '__DEV__': 'false',
      '__API_URL__': '"https://api.production.com"'
    }
  },
  
  staging: {
    minify: true,
    sourcemap: true,
    define: {
      '__DEV__': 'false',
      '__API_URL__': '"https://api.staging.com"'
    }
  }
};

const env = process.env.NODE_ENV || 'development';
const config = environments[env];

Utilizzo nelle Estensioni โ€‹

typescript
declare const __DEV__: boolean;
declare const __API_URL__: string;

export class MyExtension extends HTMLElement {
  private debug = __DEV__;
  private apiUrl = __API_URL__;
  
  private log(message: string) {
    if (this.debug) {
      console.log(`[MyExtension] ${message}`);
    }
  }
}

Watch Mode โ€‹

Development Server โ€‹

bash
# Avvia watch mode per sviluppo
node scripts/watch-extensions.js

# Watch singola estensione
node scripts/watch-extension.js my-extension

Configurazione Watch โ€‹

javascript
// scripts/watch-extension.js
import { build } from 'esbuild';

const watchConfig = {
  ...buildConfig,
  watch: {
    onRebuild(error, result) {
      if (error) {
        console.error('โŒ Build failed:', error);
      } else {
        console.log('โœ… Build succeeded');
        // Hot reload se configurato
        notifyHotReload();
      }
    }
  }
};

Deployment โ€‹

Deployment Locale โ€‹

bash
# Copia file nella directory di sviluppo
npm run deploy:local

# Equivalente a:
cp -r dist/extensions/* ../kimu-app/public/extensions/

Deployment CDN โ€‹

bash
# Upload su CDN
npm run deploy:cdn

# Equivalente a:
aws s3 sync dist/extensions/ s3://kimu-extensions/v1.0.0/

Deployment Registry โ€‹

bash
# Publica nel registry delle estensioni
npm run publish:extension my-extension

# Equivalente a:
kimu publish my-extension --version 1.0.0

CI/CD Pipeline โ€‹

GitHub Actions โ€‹

yaml
# .github/workflows/build-extensions.yml
name: Build Extensions

on:
  push:
    branches: [main]
  pull_request:
    branches: [main]

jobs:
  build:
    runs-on: ubuntu-latest
    
    steps:
    - uses: actions/checkout@v3
    
    - name: Setup Node.js
      uses: actions/setup-node@v3
      with:
        node-version: '18'
        cache: 'npm'
    
    - name: Install dependencies
      run: npm ci
    
    - name: Lint extensions
      run: npm run lint:extensions
    
    - name: Test extensions
      run: npm run test:extensions
    
    - name: Build extensions
      run: npm run build:extensions
    
    - name: Upload build artifacts
      uses: actions/upload-artifact@v3
      with:
        name: built-extensions
        path: dist/extensions/
    
    - name: Deploy to staging
      if: github.ref == 'refs/heads/main'
      run: npm run deploy:staging

Validation Pipeline โ€‹

bash
# Valida tutte le estensioni
npm run validate:extensions

# Include:
# - Syntax check
# - Manifest validation
# - Dependency check
# - Size limits
# - Performance check

Ottimizzazione โ€‹

Code Splitting โ€‹

typescript
// Lazy loading di componenti pesanti
export class MyExtension extends HTMLElement {
  private async loadHeavyComponent() {
    const { HeavyComponent } = await import('./heavy-component');
    return new HeavyComponent();
  }
}

Tree Shaking โ€‹

javascript
// build.config.js
export default {
  treeShaking: true,
  sideEffects: false,
  
  // Aiuta il tree shaking
  external: [
    'lodash-es', // Usa versione ES modules
    'rxjs/operators' // Import specifici
  ]
};

Bundle Analysis โ€‹

bash
# Analizza bundle size
npm run analyze:bundle my-extension

# Genera report dettagliato
npm run bundle:report

Testing โ€‹

Unit Testing โ€‹

bash
# Test singola estensione
npm run test:extension my-extension

# Test tutte le estensioni
npm run test:extensions

E2E Testing โ€‹

bash
# Test end-to-end
npm run test:e2e:extensions

# Include deployment test
npm run test:deployment

Monitoring โ€‹

Build Metrics โ€‹

javascript
// scripts/build-metrics.js
const metrics = {
  buildTime: Date.now() - startTime,
  bundleSize: fs.statSync(outputFile).size,
  dependencies: getDependencyCount(),
  warnings: warningsCount,
  errors: errorsCount
};

// Invia metriche
sendMetrics(metrics);

Performance Monitoring โ€‹

typescript
// Monitoraggio performance runtime
export class MyExtension extends HTMLElement {
  connectedCallback() {
    const startTime = performance.now();
    
    this.render();
    
    const endTime = performance.now();
    this.reportMetric('render-time', endTime - startTime);
  }
  
  private reportMetric(name: string, value: number) {
    // Invia metrica al sistema di monitoring
  }
}

Troubleshooting โ€‹

Build Errors Comuni โ€‹

bash
# Pulisci cache build
npm run clean:build

# Rebuild da zero
npm run rebuild:extensions

# Debug build verbose
DEBUG=true npm run build:extension my-extension

Dependency Issues โ€‹

bash
# Controlla dipendenze
npm run check:deps

# Aggiorna dipendenze
npm run update:deps

# Risolvi conflitti
npm run resolve:deps

Best Practices โ€‹

  1. Versioning: Usa semantic versioning per gli artifact
  2. Caching: Implementa cache intelligente per accelerare i build
  3. Parallelization: Compila estensioni in parallelo quando possibile
  4. Validation: Valida sempre prima del deployment
  5. Monitoring: Monitor build performance e bundle size
  6. Documentation: Documenta configurazioni custom
  7. Testing: Test automated per ogni build

Riferimenti โ€‹

Released under Creative Commons Attribution 4.0 International (CC BY 4.0)