Skip to content

Commit 3fdcc49

Browse files
committed
feat(blog): add experimental single file components post
Introduced a new blog post detailing the experimental support for Single File Components (SFCs) in Aurelia 2 with Vite. The article covers benefits, setup instructions, and key features of SFCs to enhance development workflows.
1 parent 56fa6e6 commit 3fdcc49

File tree

1 file changed

+194
-0
lines changed

1 file changed

+194
-0
lines changed
Lines changed: 194 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,194 @@
1+
+++
2+
title = "Experimental Single File Components Support For Aurelia 2 + Vite"
3+
authors = ["Dwayne Charrington"]
4+
description = "We're excited to announce experimental support for Single File Components (SFCs) in Aurelia 2. Learn how SFCs simplify your development workflow."
5+
date = 2025-02-05T05:46:52+10:00
6+
lastmod = 2025-02-05T05:46:52+10:00
7+
tags = ["aurelia2", "vite", "sfc"]
8+
+++
9+
10+
We're excited to announce **experimental support** for Single File Components (SFCs) in Aurelia 2 through a new Vite plugin. This marks a major step towards a more integrated and streamlined development experience in your Aurelia projects.
11+
12+
## What Are Single File Components?
13+
14+
Single File Components (SFCs) let you encapsulate your component's template, logic, and styles in a single `.au` file. If you've used Vue or other modern frameworks, you already appreciate the benefits, including:
15+
16+
- **Modularity:** Organize your component's structure without juggling multiple files.
17+
- **Scoped Styles:** Automatically scope your styles to each component to prevent global CSS conflicts.
18+
- **Preprocessor Support:** Easily work with Sass, Stylus, or even custom preprocessors.
19+
- **Enhanced HMR:** Enjoy fast hot module replacement via Vite's efficient bundling.
20+
21+
## Why Are We Adding This Feature?
22+
23+
The Aurelia 2 team is committed to reducing boilerplate and making component development more enjoyable. With SFCs you can:
24+
25+
- **Streamline Development:** Combine HTML, CSS, and JavaScript into a single file.
26+
- **Leverage Modern Tools:** Benefit from Vite's significant performance improvements, particularly during development.
27+
- **Improve Maintainability:** Keep your codebase modular and easier to manage.
28+
29+
## Getting Started
30+
31+
To try out the experimental SFC support for Aurelia 2, install the SFC Vite plugin:
32+
33+
```bash
34+
npm install @aurelia/sfc-vite --save-dev
35+
```
36+
37+
In your `vite.config.js` or `vite.config.ts`:
38+
39+
```javascript
40+
import { defineConfig } from 'vite';
41+
import aurelia from '@aurelia/vite-plugin';
42+
import aureliaSingleFileComponent from '@aurelia/sfc-vite';
43+
44+
export default defineConfig({
45+
plugins: [
46+
aurelia(),
47+
aureliaSingleFileComponent()
48+
]
49+
});
50+
```
51+
52+
## How It Works
53+
54+
The SFC plugin transforms `.au` files into Aurelia-compatible components through:
55+
56+
1. **Virtual Modules:** Each section (`script`, `template`, `style`) becomes a virtual module for Vite to process
57+
2. **Automatic Composition:** Combines template and class into a proper Aurelia component
58+
3. **Style Scoping:** Adds unique data attributes for CSS isolation when using `scoped` attribute
59+
60+
## Using SFCs in Aurelia
61+
62+
Here's a complete example showing key features:
63+
64+
```html
65+
<script lang="ts">
66+
import { bindable } from 'aurelia';
67+
68+
export default class MyComponent {
69+
@bindable greeting = 'Hello';
70+
}
71+
</script>
72+
73+
<template>
74+
<div>
75+
<h1>${greeting}, ${name}!</h1>
76+
<p if.bind="showMessage">This is scoped to this component</p>
77+
</div>
78+
</template>
79+
80+
<style scoped lang="scss">
81+
$primary: #2c3e50;
82+
83+
h1 {
84+
color: $primary;
85+
font-family: system-ui;
86+
}
87+
88+
p {
89+
padding: 1rem;
90+
background: lighten($primary, 60%);
91+
}
92+
</style>
93+
```
94+
95+
And then register the component in your `main.ts` or `main.js`:
96+
97+
```typescript
98+
import Aurelia from 'aurelia';
99+
import { MyApp } from './my-app';
100+
101+
import MyComponent from './my-component.au';
102+
103+
Aurelia
104+
.register(MyComponent)
105+
.app(MyApp)
106+
.start();
107+
```
108+
109+
If you're working with a TypeScript project, you will need to add the following to your `resources.d.ts` file to get rid of the type error:
110+
111+
```typescript
112+
declare module '*.au';
113+
```
114+
115+
## Key Features Deep Dive
116+
117+
### 1. Style Scoping Magic ✨
118+
When using `<style scoped>`, the plugin automatically:
119+
- Generates unique data attributes (e.g., `data-v-7ba5bd90`)
120+
- Applies them to all root template elements
121+
- Transforms CSS selectors to match scoped attributes
122+
123+
```css
124+
/* Input */
125+
h1 { color: blue; }
126+
127+
/* Output */
128+
h1[data-v-7ba5bd90] { color: blue; }
129+
```
130+
131+
### 2. Preprocessor Power ⚡
132+
The plugin supports popular CSS preprocessers out of the box:
133+
134+
```html
135+
<style lang="scss">
136+
/* SCSS features work! */
137+
$colors: red, green, blue;
138+
139+
@each $color in $colors {
140+
.text-#{$color} { color: $color; }
141+
}
142+
</style>
143+
```
144+
145+
### 3. Custom Configuration
146+
Advanced users can configure processing options:
147+
148+
```javascript
149+
// vite.config.js
150+
export default defineConfig({
151+
plugins: [
152+
aureliaSingleFileComponent({
153+
style: {
154+
preprocessors: {
155+
scss: (code) => require('sass').compileString(code).css,
156+
// Add custom preprocessors here
157+
}
158+
}
159+
})
160+
]
161+
});
162+
```
163+
164+
## Current Limitations
165+
166+
While exciting, this experimental implementation has some constraints and is definitely very much a work in progress. Some of the limitations include:
167+
168+
- 🚧 Components must be registered in your `main.ts` or `main.js` file
169+
- 🚧 Single root element templates work best
170+
- 🚧 No support for template-level type checking
171+
- 🚧 Complex style selectors might need manual scoping
172+
- 🚧 TypeScript source maps need additional configuration
173+
- 🚧 No template pre-processors yet (Pug, Haml, etc.)
174+
175+
## Why Try It Now?
176+
177+
Even in this experimental state, SFCs offer:
178+
179+
- 🧪 Faster prototyping with colocated code
180+
- 🧪 Foundation for future community standards
181+
- 🧪 Opportunity to shape the SFC specification
182+
183+
## What's Next?
184+
185+
We're actively working on:
186+
187+
- Better TypeScript integration
188+
- Expanded template features
189+
- Official VSCode extension support
190+
- Performance optimizations
191+
192+
Give it a try and share your feedback! Our goal is to make Aurelia SFCs the most developer-friendly implementation in the framework ecosystem.
193+
194+
> **Important**: This feature is purely opt-in - existing component formats will continue to work unchanged. We're committed to maintaining Aurelia's legendary backward compatibility.

0 commit comments

Comments
 (0)