Dividere il tema SCSS in più file

I temi e i componenti dei temi stanno diventando sempre più potenti e gli sviluppatori diventano sempre più ambiziosi. Per facilitare il lavoro degli sviluppatori, i temi possono ora suddividere il loro SCSS in più file.

Quando si crea un nuovo tema con la theme CLI, o si condivide un tema su github, è sufficiente creare una nuova cartella chiamata scss. Riempila con i tuoi file .scss, seguendo qualsiasi struttura di cartelle, e tutti i file saranno disponibili per l’importazione nelle sezioni SCSS comuni / desktop / mobile del tuo tema.

Ad esempio, se vuoi avere una variabile comune disponibile sia per lo SCSS mobile che per quello desktop, potresti fare qualcosa di simile:

scss/my-folder/variables.scss

$favourite-color = red;

desktop/desktop.scss

@import "my-folder/variables";
body {
  background-color: $favourite-color;
}

mobile/mobile.scss

@import "my-folder/variables";
body {
  color: $favourite-color;
}

Questa funzionalità è stata aggiunta in v2.3.0.beta8, quindi non utilizzare queste funzionalità per il momento se devi mantenere la retrocompatibilità con le versioni precedenti di Discourse. Puoi utilizzare il parametro minimum_discourse_version di about.json per assicurarti che il tuo componente non venga utilizzato su una versione precedente.


Questo documento è controllato tramite versione - suggerisci modifiche su github.

20 Mi Piace

This is amazing @david! Nice work!

I can’t wait to give this a whirl.

8 Mi Piace

This is a great! I can now remove my hack for getting around not being able to do this.

5 Mi Piace

@david this is great, but unfortunately it doesn’t seem to work when the theme is a component. Not sure if that’s a known bug.

You can see it happen with this very simple theme: GitHub - LeoMcA/discourse-multi-scss-component

When converted to a stand-alone theme the background colour will be green, but when used as a component within another theme it doesn’t change the background colour at all.

5 Mi Piace

Thanks @LeoMcA, you’re right - this wasn’t working in components. The issue should be fixed in

4 Mi Piace

Is that still supposed to work? My default scss files (common/desktop/mobile) don’t work when I add an @import.

Or is there a new recommendation for splitting files?

This should still work. Can you share the precise @import statement you’re using, and the file structure of your stylesheets?

The theme setup has default file structure and I added scss/test.scss Then I state @import "test";

On the console (using discourse_theme watch) I get:
✘ Error in common scss: Error: File to import not found or unreadable: test. on line 1 of common.scss

Does your test.scss file have any content?

Just declared one variable (btw there’s a typo on the first code block above, declared with equals: $favourite-color = red;)

But just solved it. I set the minimum version in about.json as v2.3.0.beta8 and when I remove that, it works just fine. Never used that before, so not sure what’s wrong with the declaration.

1 Mi Piace

Interesting! If you add the minimum version again, does it consistently break? If so, would you mind zipping up the broken theme and sharing it here? (or PMing it to me)

1 Mi Piace

As said, I never set a min version before. So I just copied it above as v2.3.0.beta8. This breaks, but 2.3.0.beta8 works just fine.

È possibile utilizzare file scss da un pacchetto node_modules? Ad esempio:

@import 'bootstrap/scss/bootstrap.scss';
@import '../node_modules/bootstrap/scss/bootstrap.scss';

In entrambi i casi non riesco a importare questo dalla cartella /scss.