Skip to content

Commit

Permalink
feat!: preprocess style tags by default (#756)
Browse files Browse the repository at this point in the history
  • Loading branch information
bluwy authored Oct 6, 2023
1 parent b194ef4 commit 7c45024
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
5 changes: 5 additions & 0 deletions .changeset/lovely-tables-scream.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/vite-plugin-svelte': major
---

Preprocess style tags by default with vitePreprocess
3 changes: 2 additions & 1 deletion docs/preprocess.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,13 @@ However, `svelte-preprocess` does provide extra functionalities not available wi
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.

- TypeScript: `<script lang="ts">`
- PostCSS: `<style lang="postcss">`
- SCSS: `<style lang="scss">`
- Less: `<style lang="less">`
- Stylus: `<style lang="stylus">`
- SugarSS: `<style lang="sss">`

By default, PostCSS or LightningCSS ([if configured in Vite](https://vitejs.dev/config/shared-options.html#css-transformer)) is applied to all `<style>` tags.

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.

**Example:**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.foo {
color: green;
}
19 changes: 19 additions & 0 deletions packages/vite-plugin-svelte/__tests__/preprocess.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,25 @@ describe('vitePreprocess', () => {
});

describe('style', async () => {
it('preprocess with postcss if no lang', async () => {
const preprocessorGroup = vitePreprocess({ style: {} });
const style = /**@type {import('svelte/types/compiler/preprocess').Preprocessor} */ (
preprocessorGroup.style
);
expect(style).toBeDefined();

const pcss = "@import './foo';";
const processed = await style({
content: pcss,
attributes: {},
markup: '', // not read by vitePreprocess
filename: `${fixtureDir}/File.svelte`
});

expect(processed).toBeDefined();
expect(processed.code).not.toContain('@import');
});

it('produces sourcemap with relative filename', async () => {
const preprocessorGroup = vitePreprocess({ style: { css: { devSourcemap: true } } });
const style = /**@type {import('svelte/types/compiler/preprocess').Preprocessor} */ (
Expand Down
2 changes: 1 addition & 1 deletion packages/vite-plugin-svelte/__tests__/sourcemaps.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ const filename = 'File.svelte';

describe('removeLangSuffix', () => {
it('removes suffix', () => {
const suffix = `${lang_sep}scss`;
const suffix = `${lang_sep}.scss`;
const map = {
file: `${fixtureDir}/${filename}${suffix}`,
sources: ['foo.scss', `${fixtureDir}/${filename}${suffix}`],
Expand Down
11 changes: 5 additions & 6 deletions packages/vite-plugin-svelte/src/preprocess.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import { preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
import { isCSSRequest, preprocessCSS, resolveConfig, transformWithEsbuild } from 'vite';
import { mapToRelative, removeLangSuffix } from './utils/sourcemaps.js';

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

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

export const lang_sep = '.vite-preprocess.';
export const lang_sep = '.vite-preprocess';

/** @type {import('./index.d.ts').vitePreprocess} */
export function vitePreprocess(opts) {
Expand Down Expand Up @@ -63,8 +62,8 @@ function viteStyle(config = {}) {
let transform;
/** @type {import('svelte/types/compiler/preprocess').Preprocessor} */
const style = async ({ attributes, content, filename = '' }) => {
const lang = /** @type {string} */ (attributes.lang);
if (!supportedStyleLangs.includes(lang)) return;
const ext = attributes.lang ? `.${attributes.lang}` : '.css';
if (attributes.lang && !isCSSRequest(ext)) return;
if (!transform) {
/** @type {import('vite').ResolvedConfig} */
let resolvedConfig;
Expand All @@ -82,7 +81,7 @@ function viteStyle(config = {}) {
}
transform = getCssTransformFn(resolvedConfig);
}
const suffix = `${lang_sep}${lang}`;
const suffix = `${lang_sep}${ext}`;
const moduleId = `${filename}${suffix}`;
const { code, map, deps } = await transform(content, moduleId);
removeLangSuffix(map, suffix);
Expand Down

0 comments on commit 7c45024

Please sign in to comment.