Skip to content

Commit 7c45024

Browse files
authored
feat!: preprocess style tags by default (#756)
1 parent b194ef4 commit 7c45024

File tree

6 files changed

+35
-8
lines changed

6 files changed

+35
-8
lines changed

.changeset/lovely-tables-scream.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@sveltejs/vite-plugin-svelte': major
3+
---
4+
5+
Preprocess style tags by default with vitePreprocess

docs/preprocess.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,13 @@ However, `svelte-preprocess` does provide extra functionalities not available wi
1414
A Svelte preprocessor that supports transforming TypeScript, PostCSS, SCSS, Less, Stylus, and SugarSS. These are transformed when the script or style tags have the respective `lang` attribute.
1515

1616
- TypeScript: `<script lang="ts">`
17-
- PostCSS: `<style lang="postcss">`
1817
- SCSS: `<style lang="scss">`
1918
- Less: `<style lang="less">`
2019
- Stylus: `<style lang="stylus">`
2120
- SugarSS: `<style lang="sss">`
2221

22+
By default, PostCSS or LightningCSS ([if configured in Vite](https://vitejs.dev/config/shared-options.html#css-transformer)) is applied to all `<style>` tags.
23+
2324
If required, you can turn script or style transforming off by setting the `script` or `style` option to `false`. The `style` option also accepts Vite's `InlineConfig` and `ResolvedConfig` types for advanced usage.
2425

2526
**Example:**
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.foo {
2+
color: green;
3+
}

packages/vite-plugin-svelte/__tests__/preprocess.spec.js

+19
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,25 @@ describe('vitePreprocess', () => {
1717
});
1818

1919
describe('style', async () => {
20+
it('preprocess with postcss if no lang', async () => {
21+
const preprocessorGroup = vitePreprocess({ style: {} });
22+
const style = /**@type {import('svelte/types/compiler/preprocess').Preprocessor} */ (
23+
preprocessorGroup.style
24+
);
25+
expect(style).toBeDefined();
26+
27+
const pcss = "@import './foo';";
28+
const processed = await style({
29+
content: pcss,
30+
attributes: {},
31+
markup: '', // not read by vitePreprocess
32+
filename: `${fixtureDir}/File.svelte`
33+
});
34+
35+
expect(processed).toBeDefined();
36+
expect(processed.code).not.toContain('@import');
37+
});
38+
2039
it('produces sourcemap with relative filename', async () => {
2140
const preprocessorGroup = vitePreprocess({ style: { css: { devSourcemap: true } } });
2241
const style = /**@type {import('svelte/types/compiler/preprocess').Preprocessor} */ (

packages/vite-plugin-svelte/__tests__/sourcemaps.spec.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ const filename = 'File.svelte';
1212

1313
describe('removeLangSuffix', () => {
1414
it('removes suffix', () => {
15-
const suffix = `${lang_sep}scss`;
15+
const suffix = `${lang_sep}.scss`;
1616
const map = {
1717
file: `${fixtureDir}/${filename}${suffix}`,
1818
sources: ['foo.scss', `${fixtureDir}/${filename}${suffix}`],

packages/vite-plugin-svelte/src/preprocess.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
import { preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
1+
import { isCSSRequest, preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
22
import { mapToRelative, removeLangSuffix } from './utils/sourcemaps.js';
33

44
/**
55
* @typedef {(code: string, filename: string) => Promise<{ code: string; map?: any; deps?: Set<string> }>} CssTransform
66
*/
77

8-
const supportedStyleLangs = ['css', 'less', 'sass', 'scss', 'styl', 'stylus', 'postcss', 'sss'];
98
const supportedScriptLangs = ['ts'];
109

11-
export const lang_sep = '.vite-preprocess.';
10+
export const lang_sep = '.vite-preprocess';
1211

1312
/** @type {import('./index.d.ts').vitePreprocess} */
1413
export function vitePreprocess(opts) {
@@ -63,8 +62,8 @@ function viteStyle(config = {}) {
6362
let transform;
6463
/** @type {import('svelte/types/compiler/preprocess').Preprocessor} */
6564
const style = async ({ attributes, content, filename = '' }) => {
66-
const lang = /** @type {string} */ (attributes.lang);
67-
if (!supportedStyleLangs.includes(lang)) return;
65+
const ext = attributes.lang ? `.${attributes.lang}` : '.css';
66+
if (attributes.lang && !isCSSRequest(ext)) return;
6867
if (!transform) {
6968
/** @type {import('vite').ResolvedConfig} */
7069
let resolvedConfig;
@@ -82,7 +81,7 @@ function viteStyle(config = {}) {
8281
}
8382
transform = getCssTransformFn(resolvedConfig);
8483
}
85-
const suffix = `${lang_sep}${lang}`;
84+
const suffix = `${lang_sep}${ext}`;
8685
const moduleId = `${filename}${suffix}`;
8786
const { code, map, deps } = await transform(content, moduleId);
8887
removeLangSuffix(map, suffix);

0 commit comments

Comments
 (0)