Skip to content

feat!: preprocess style tags by default #756

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Oct 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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