Best Practices: Stagepass
TypeScript and SCSS for Webflow: Types, Imports and One Build
Write behaviour in TypeScript and styles in SCSS, split both into small files, and let one build turn them into the two plain files your Webflow site loads.
On this page
Webflow custom code fails quietly. A selector returns null on one template. A typo in a class name hides until a client clicks the wrong button. And the stylesheet in Head Code keeps growing until nobody dares to delete a rule.
TypeScript catches most of the first kind of mistake in your editor, before anything reaches the page. SCSS gives the second kind a structure you can find your way around in. The build turns both into files Webflow can load, and Stagepass lets you test those files on the real site without publishing.
This part of the series goes one level deeper than the overview: what the build does, how types for sp work, how to split code into modules and partials, and the one watch-mode detail that trips people up.
Why TypeScript and SCSS for Webflow
| You write | Webflow loads | What you gain | |
|---|---|---|---|
| Behaviour | TypeScript in src/ | One bundled dist/main.js | Null checks, autocomplete and rename-safety in your editor; imports instead of one long script |
| Styles | SCSS in src/ | One compressed dist/styles.css | Partials, variables, nesting and comments that never reach the visitor |
The key point: Webflow never sees TypeScript or SCSS. The loader only swaps the two built files. Everything that makes the source nicer to work with stays on your Mac.
Step 1: Start a project
stagepass new my-project --staging my-project.webflow.io
cd my-project
npm installThe wizard asks a few questions. Two of them matter for this guide; press Enter to keep the defaults:
Language (ts|js) (ts):
Styles (scss|css) (scss):With --yes you skip the questions and get the same defaults. --lang js or --styles css switch either one off. The parts that matter for this guide:
my-project/
src/main.ts # your TypeScript entry
src/styles.scss # your SCSS entry
scripts/build.mjs # the build (esbuild, plus Sass)
tsconfig.json # strict mode, types for sp
package.json # esbuild, typescript, sass, @stagepass/loader
stagepass.jsonStep 2: Know what the build does
scripts/build.mjs is plain Node you can read. In a TypeScript and SCSS project it does two things:
- JavaScript: esbuild bundles
src/main.tsand everything it imports into one file,dist/main.js. The output is wrapped in a function (IIFE), targets ES2018 and has no source map. - CSS: Sass compiles
src/styles.scss, including every partial it pulls in, into one compressed file,dist/styles.css.
Three commands run it:
| Command | Writes | Use it for |
|---|---|---|
stagepass build | dist/main.js, dist/styles.css, content-hashed copies and dist/manifest.json with SRI hashes | Production uploads |
stagepass watch | Only dist/main.js and dist/styles.css, again on every change | Rebuilding without live reload |
stagepass dev | Same as watch, plus live reload on your .sp domain | Day-to-day work on the live site |
Watch and dev leave manifest.json as the last stagepass build wrote it, and they only rewrite a file when its bytes changed.
Your editor shows type errors as you type. To check the whole project at once, for example before a production build, run the TypeScript compiler that the project already has installed:
npx tsc --noEmitIt reads tsconfig.json, checks every .ts file in src/ and writes nothing.
Step 3: Types for sp and your modules
The generated tsconfig.json runs in strict mode and lists the Stagepass types:
{
"compilerOptions": {
"target": "ES2018",
"module": "ESNext",
"moduleResolution": "bundler",
"strict": true,
"noEmit": true,
"skipLibCheck": true,
"lib": ["ES2018", "DOM"],
"types": ["@stagepass/loader"]
},
"include": ["src/**/*.ts"]
}@stagepass/loader types the global sp (the same object as window.stagepass): sp.log, sp.env(), sp.is(), sp.can(), sp.vars and the rest, with JSDoc in your editor.
Modules work the same way, but only for the ones your site actually loads. stagepass.json lists them under modules, and the CLI writes a matching entry such as @stagepass/modules/cookies into types. A module that is not declared has no types, so a call like sp.cookies is flagged in the editor instead of failing on the live site:
Property 'cookies' does not exist on type 'StagepassAPI'.Enabled a module later? Add its script tag in Webflow, add it to modules in stagepass.json, and run stagepass upgrade. The types follow.
Already have a project?
Since CLI 2.5.0, stagepass upgrade retrofits editor types in an existing project. It adds a missing @stagepass/loader devDependency and then prints npm install; run that yourself. If your tsconfig.json or jsconfig.json already has a types array, upgrade updates it in place. Otherwise it writes a small stagepass.d.ts with the type references. The CLI reference explains when your config needs to include that file.
Step 4: A safe entry point
The generated src/main.ts, shown with its comments shortened, is written for Webflow pages:
(() => {
const run = (): void => {
// Your project code here.
const sp = (window as Window & { sp?: { log?: (...args: unknown[]) => void } }).sp;
sp?.log?.('my-project: ready');
};
const start = (): void => {
const w = window as Window & {
Webflow?: { push?: (fn: () => void) => void };
};
if (w.Webflow && typeof w.Webflow.push === 'function') {
w.Webflow.push(run);
return;
}
run();
};
if (document.readyState === 'loading') {
document.addEventListener('DOMContentLoaded', start, { once: true });
} else {
start();
}
})();Three things it gets right:
- It waits for the page. If the DOM is still loading, it waits for
DOMContentLoaded. Otherwise it starts right away. - It waits for Webflow. When
Webflow.pushexists,rungoes into Webflow's queue, so your code runs after Webflow has set up its own interactions. - It keeps to itself. Everything lives inside one function. Nothing leaks into the global scope where it could clash with other scripts on the page.
The sp line uses a narrow, defensive cast that works even without the loader on the page. With @stagepass/loader in types you can also call the global directly, fully typed:
sp.log('my-project: ready');Step 5: Split your code into modules
Put each feature in its own file and import it. A small menu toggle in src/features/menu.ts:
export function initMenu(root: ParentNode = document): void {
const button = root.querySelector<HTMLButtonElement>('[data-menu-toggle]');
const panel = root.querySelector<HTMLElement>('[data-menu-panel]');
if (!button || !panel) return;
button.addEventListener('click', () => {
const open = panel.hasAttribute('hidden');
panel.toggleAttribute('hidden', !open);
button.setAttribute('aria-expanded', String(open));
});
}Then call it from run() in src/main.ts:
import { initMenu } from './features/menu';
// inside run():
initMenu();Delete the if (!button || !panel) return; line and strict mode answers immediately with 'button' is possibly 'null'. That is exactly the bug that would otherwise surface on the one template where the menu does not exist.
The build follows every import, including packages you install from npm, and still produces one dist/main.js. Webflow loads a single file, and you keep one feature per file.
Step 6: Styles in SCSS partials
SCSS works the same way. Keep shared values and each component in its own partial, and pull them into the entry with @use:
src/
styles.scss
styles/_tokens.scss
styles/_menu.scss// src/styles/_tokens.scss
$brand: #4353ff;
$radius: 8px;// src/styles/_menu.scss
@use 'tokens';
.sp-my-project [data-menu-panel] {
border-radius: tokens.$radius;
&[hidden] {
display: none;
}
a:hover {
color: tokens.$brand;
}
}// src/styles.scss
@use 'styles/menu';
:root {
--sp-project: 'my-project';
}The build compiles all of it into one compressed dist/styles.css. Variables, nesting and // comments stay in your source. The scaffold already names a class after your project, .sp-my-project. Give a wrapper element that class in the Designer, and rules nested under it stay out of the way of your Webflow classes.
Step 7: Test it on the live site
This is where the build pays off. If Stagepass is not set up on this Mac yet, run stagepass setup and stagepass start once (Getting started). Then link the project and start live reload:
stagepass link my-project
stagepass devstagepass new prints the Webflow snippets, and the project README.md keeps them. The loader goes into Head Code. The two built files go into Footer Code, tagged so Stagepass can swap them:
<script
data-src="https://YOUR-PRODUCTION-URL/js/main.js"
data-stagepass="true"
data-stagepass-path="dist/main.js"
></script>
<link
rel="stylesheet"
data-href="https://YOUR-PRODUCTION-URL/css/styles.css"
data-stagepass="true"
data-stagepass-path="dist/styles.css"
>Replace YOUR-PRODUCTION-URL with the host that serves your production files (Step 8). Publish once, then open https://my-project.webflow.io/?stagepass=my-project.sp. Your browser now loads dist/main.js and dist/styles.css from your Mac. Everyone else keeps getting the files from data-src and data-href.
With stagepass dev running:
- Save a TypeScript file, including any file it imports: the bundle rebuilds and the page reloads.
- Save
src/styles.scss: the new CSS swaps in without a reload.
The Loader docs explain the live reload in detail.
Step 8: Build for production
When it works in your session, build the production files:
npx tsc --noEmit
stagepass buildstagepass build writes content-hashed copies next to the stable files, for example main.a1b2c3d4.js, and records their names and SRI hashes in dist/manifest.json. Upload dist/ to wherever your production files live, then point data-src and data-href at the hosted files. If you are not sure where that should be, Where Do I Put My Files? compares the options.
When plain JavaScript and CSS are enough
- A few lines, written once. Paste them into Webflow and move on.
- You do not want a build step. Create the project with
--lang js --styles css. Your editor can still check plain JavaScript: the generatedjsconfig.jsonturns oncheckJsin strict mode. - Your team works on Windows or Linux. Stagepass runs on macOS only for now.
Further reading
- Webflow Custom Code, Done Properly, the overview of this series
- Write Webflow custom code in VS Code, or any editor
- CLI reference:
stagepass newandstagepass upgrade - Loader: TypeScript types
- Getting started
Frequently asked questions
Can I use TypeScript in Webflow?
Yes. You write TypeScript locally, and stagepass build bundles it into one plain JavaScript file. Webflow only ever loads that file. TypeScript is the default for new Stagepass projects. See the CLI reference.
Does the build check my TypeScript types?
No. esbuild strips the types without checking them, so a type error does not stop the build. Your editor shows errors as you type, and npx tsc --noEmit checks the whole project.
Can I use SCSS in Webflow?
Yes. Sass compiles src/styles.scss and every partial it uses into one compressed dist/styles.css, and Webflow loads that file. SCSS is the default for new Stagepass projects; --styles css switches to plain CSS.
Why doesn't my SCSS partial change show up?
Watch mode only watches src/styles.scss, not the partials it uses. Save src/styles.scss again, or run stagepass build. TypeScript imports are watched, so edits there rebuild on their own.
Can I import npm packages?
Yes. The build bundles everything src/main.ts imports, including installed npm packages, into the single dist/main.js.
Do visitors see my local files?
No. Only your browser, with an active Stagepass session, loads the files from your Mac. Everyone else gets the files from data-src and data-href. See Getting started.