How can I separate a preamble (with functions and formatting rules) from my main typ file?

Hi,
I want to split my typ file in 2 files: one preamble and one main file.
I tried import, include … but nothing seems to work. I put below an example of the problem: if I use this file, it’s working my text is blue and I have number on heading.
But if I put the first part in preamble.typ (the first part finish before the first import), I am unable to get the same result. As you see in comment, I tried different thing but without getting the expected output.
Do you have ideas where is the problem please ? Many thanks.
Tom

#let chapter-counter = counter("chapter")
#chapter-counter.update(0)

#set page(margin: 2cm)                 // Set page margin globally
#set heading(numbering: "I.1.a.i")     // Set a custom numbering format for headings
#set text(fill: blue, size: 14pt)     // Set global text color and size

// Function to define a chapter
#let define-chapter(title) = {
  pagebreak(weak: true)
  context {
    align(center)[
      #block(inset: 2em, breakable: false)[
        #set text(size: 24pt, weight: "bold")
        #smallcaps[Chapter]
        #v(0cm)
        #title
      ]
    ]
  }

}

// #import "preamble.typ": define-chapter
// #import "preamble.typ": *
// #include("preamble.typ")
#define-chapter("Introduction to Typst")
= Test <test>
#lorem(700)

Check this post:

The tutorial page Making a Template should be helpful in your case.

Citing from that tutorial:

In Typst, templates are functions in which you can wrap your whole document.

So if you #import something (a function) from a template it is not applied automatically. You have to call it (e.g. for your custom chapter function), or wrap your whole document in it (e.g. for the text set rules).

Hi @tom1, welcome and thank you for your question! I have changed your post’s title to bring it in line with the question guidelines and thus make it easier to understand from the title:

Good titles are questions you would ask your friend about Typst.

I also added the templates tag, as it makes your question easier to find.

Many thanks for your replies.
Currently I have not enough time to understand the overall process to convert to a template, but I find a solution which seems working except for the number of the chapter but I think I forgot to add it.

I hope the solution will be interesting for some people.

Listing of template_test.typ:

// Counter for chapters
#let chapter-counter = counter("chapter")

// Function to define a chapter
#let define-chapter(title) = {
  pagebreak(weak: true)
  context {
    align(center)[
      #block(inset: 2em, breakable: false)[
        #set text(size: 24pt, weight: "bold")
        #smallcaps[Chapter]
        #v(0cm)
        #title
      ]
    ]
  }
}

// Initialize document with all styles
#let init(doc) = {
  // Initialize counter
  chapter-counter.update(0)
  
  // Set document styles
  set page(margin: 2cm)
  set heading(numbering: "I.1.a.i")
  set text(fill: blue, size: 14pt)
  
  // Return the styled document
  doc
}

Listing of main.typ:

#import "template_test.typ": init, define-chapter

#show: doc => init(doc)

#define-chapter("Introduction to Typst")
= Test <test>
#lorem(700)

Tom