Replies: 2 comments 6 replies
-
Ciao @gabacode, scrivo qui per un dubbio. Ho ripetuto l'installazione di gatsby
è normale? devo fare qualcosa? posso procedere al |
Beta Was this translation helpful? Give feedback.
6 replies
-
@gabacode grazie mille! |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Con questa serie di "guide" documenterò lo sviluppo del nuovo sito di OpenDataSicilia, cercando di essere il piu semplice possibile, cercando anche di imparare a scrivere delle guide, che è una cosa molto nuova per me. 😊
Questa serie di guide verrà aggiornata aggiungendo risorse all'occorrenza, è quindi da reputarsi anch'essa un "work in progress".
Per prima cosa settiamo l'environment in cui andremo ad utilizzare node e gatsby per lo sviluppo.
1. Installiamo Node e Gatsby
Il metodo che consiglio per installare Node è utilizzando nvm, un version manager per Node. In questo modo possiamo cambiare versione all'occorrenza, o scaricare altre versioni. Altrimenti si può anche scaricare direttamente dal sito ufficiale. Per questa guida utilizzeremo Node v16.13.1 'Gallium' (LTS).
Per assicurarci che Node sia installato basterà lanciare il comando
node -v
dal terminale.Una volta installato Node, avremo anche accesso a npm, il package manager predefinito di Node, con cui gestiremo il progetto, e installeremo vari pacchetti e librerie che ci semplificheranno la vita, tramite il comando
npm install <nomepacchetto>
.Utilizzando la flag
-g
possiamo installare un pacchetto globalmente, in modo tale da poterlo utilizzare sulla nostra macchina al di là del progetto su cui stiamo lavorando. Tutti gli altri pacchetti installati non globalmente, si troveranno nella cartella/node_modules
.Ad esempio, installiamo Gatsby command line tool (CLI) globalmente con npm:
2. Inizializziamo
All'interno della cartella appena creata, lanciamo
npm init -y
che setterà il nostro progetto e creerà il file package.json, al cui interno terremo conto di tutte le dipendenze e metadata del progetto.Adesso possiamo installare le dipendenze principali che ci aiuteranno ad avviare il progetto, tramite il comando:
3. Struttura del progetto
Gatsby è un framework magico, che integra strumenti come React, GraphQL e Webpack, per facilitare lo sviluppo di pagine web e ottimizzarne i contenuti. Esistono alcune convenzioni che vanno rispettate per sfruttare a pieno le sue capacità. Una di queste è la struttura delle cartelle, e alcuni files essenziali che andremo a modificare in seguito.
La cartella
/src
sarà quella che terrà tutto il codice sorgente della nostra applicazione, al suo interno possiamo creare altre cartelle a nostro piacimento, ma in ogni caso, la sottocartella/pages
sarà quella in cui Gatsby andrà a cercare le nostre pagine. Questa è una bella rivoluzione già cominciata da Next.js, altro framework molto potente.Quindi prima di tutto creiamo la cartella
/src/pages
e magari inseriamo la nostra homepage all'interno:src/pages/index.js
.Se andiamo adesso a lanciare il comando
gatsby develop
dal terminale, all'interno della cartella root del progetto, possiamo notare che il progetto viene renderizzato, e l'homepage mostrata all'indirizzo http://localhost:8000. Gatsby avrà creato altre due cartelle: .cache, e public, all'interno delle quali sono presenti tutti gli asset del progetto, minimizzati e ottimizzati per essere serviti in fase di sviluppo. Quando lanceremo l'app in produzione tramitegatsby build
anche la cartella./build
verrà creata con assets ottimizzati per ambito production.Queste cartelle vanno incluse all'interno del file
.gitignore
, che andremo a creare, che comunicherà a git quali files e quali cartelle non andranno inseriti all'interno del repository.All'interno di questa cartella creeremo dei componenti riutilizzabili che hanno una specifica funzione. I componenti non sono nient'altro che funzioni che possiamo esportare e importare in diverse pagine, magari modificandone i parametri, e quindi i risultati. Ad esempio come un frullatore è un componente, può ricevere arance e fare un succo d'arancia, o mirtilli per il succo al mirtillo. A seconda di cosa ci serve prendiamo il frullatore e lo mettiamo in una pagina particolare, così da poterne riutilizzare le funzionalità. Il nome della cartella è una convenzione che viene principalmente da React, su cui Gatbsy è basato.
Qui andremo a creare delle strutture di design predefinite (ma customizzabili) per ogni tipo di contenuto che vogliamo mostrare in maniera coerente e ripetibile. Ad esempio, potremo creare un template per ogni articolo di blog, un template per ogni tipo di evento e così via.
Aggiungiamo al .gitignore le cartelle che non vogliamo portarci sul repository.
Altri due files fondamentali che andremo a creare nella root del progetto, sono gatsby-config.js e gatsby-node.js, che ci aiuteranno a gestire la configurazione del progetto, e i nodi GraphQL che andremo a creare.
La struttura corrente del progetto a questo punto dovrebbe essere piu o meno questa:
4. Conclusione
Adesso siamo pronti a sviluppare la nostra applicazione con Gatsby.js! Vedremo piu avanti come collegarlo, nel nostro caso, ad una installazione preesistente di Wordpress.
Beta Was this translation helpful? Give feedback.
All reactions