Skip to content

Commit bc0b667

Browse files
committed
Added custom preprocessor
1 parent b4ec619 commit bc0b667

File tree

6 files changed

+50
-4
lines changed

6 files changed

+50
-4
lines changed
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
2+
import { svelteRevealPreprocess } from 'svelte-reveal';
23

34
export default {
45
// Consult https://svelte.dev/docs#compile-time-svelte-preprocess
56
// for more information about preprocessors
6-
preprocess: vitePreprocess()
7+
preprocess: [vitePreprocess(), svelteRevealPreprocess()]
78
};

examples/with-sveltekit/src/routes/+page.svelte

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
<script lang="ts">
22
import { reveal, type RevealOptions } from 'svelte-reveal';
3-
import 'svelte-reveal/styles.css';
43
54
const config: RevealOptions[] = [
65
{ preset: 'fade', duration: 2000 },
@@ -15,7 +14,7 @@
1514
<main>
1615
{#each config as element}
1716
<section>
18-
<div use:reveal={{ ...element }} class="wrapper sr__hide">
17+
<div use:reveal={{ ...element }} class="wrapper">
1918
<h1>{element.preset} transition</h1>
2019
</div>
2120
</section>

examples/with-sveltekit/svelte.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import adapter from '@sveltejs/adapter-auto';
22
import { vitePreprocess } from '@sveltejs/vite-plugin-svelte';
3+
import { svelteRevealPreprocess } from 'svelte-reveal';
34

45
/** @type {import('@sveltejs/kit').Config} */
56
const config = {
67
// Consult https://kit.svelte.dev/docs/integrations#preprocessors
78
// for more information about preprocessors
8-
preprocess: vitePreprocess(),
9+
preprocess: [vitePreprocess(), svelteRevealPreprocess({ ssr: true })],
910

1011
kit: {
1112
// adapter-auto only supports some environments, see https://kit.svelte.dev/docs/adapter-auto for a list.

packages/svelte-reveal/CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Added
11+
12+
- Custom preprocessor to reduce boilerplate when using SvelteKit
13+
814
## [1.1.0] - 2024-04-10
915

1016
### Changed

packages/svelte-reveal/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,5 +13,6 @@ export {
1313
setConfig,
1414
setDefaultOptions
1515
} from './internal/API.ts';
16+
export { svelteRevealPreprocess } from '@/preprocessor.ts';
1617
export type { RevealConfig } from '@/types/config.ts';
1718
export type { RevealOptions } from '@/types/options.ts';
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { PreprocessorGroup } from 'svelte/compiler';
2+
3+
export function svelteRevealPreprocess(options?: { ssr?: boolean }): PreprocessorGroup {
4+
const ssr = options?.ssr ?? false;
5+
6+
return {
7+
markup({ content }) {
8+
if (!ssr) return { code: content };
9+
10+
const cssClassToAdd = 'sr__hide';
11+
const regex = /(<[\w-]+\s+[^>]*)(use:reveal)([^>]*>)/g;
12+
13+
const modifiedContent = content.replace(regex, (match, start, directive, end) => {
14+
if (/class="[^"]*"/.test(match)) {
15+
return match.replace(/class="([^"]*)"/, `class="$1 ${cssClassToAdd}"`);
16+
}
17+
return `${start}class="${cssClassToAdd}" ${directive}${end}`;
18+
});
19+
20+
return { code: modifiedContent };
21+
},
22+
script({ content }) {
23+
if (!ssr) return { code: content };
24+
25+
const importStatement = `import 'svelte-reveal/styles.css';`;
26+
const importRegex = /import\s*['"]svelte-reveal\/styles\.css['"]/;
27+
28+
// If the import already exists, return the content as is
29+
if (importRegex.test(content)) return { code: content };
30+
31+
// If the script tag exists, add the import at the top
32+
if (content.trim().length > 0) return { code: `${importStatement}\n${content}` };
33+
34+
// If no script tag exists, create one with the import statement
35+
return { code: `<script>\n${importStatement}\n</script>\n${content}` };
36+
}
37+
};
38+
}

0 commit comments

Comments
 (0)