Skip to content

Commit 08047c1

Browse files
authored
Pass full markup source to preprocessors (#6169)
1 parent a55295d commit 08047c1

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

site/content/docs/04-compile-time.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,11 +199,11 @@ result: {
199199
code: string,
200200
dependencies?: Array<string>
201201
}>,
202-
script?: (input: { content: string, attributes: Record<string, string>, filename: string }) => Promise<{
202+
script?: (input: { content: string, markup: string, attributes: Record<string, string>, filename: string }) => Promise<{
203203
code: string,
204204
dependencies?: Array<string>
205205
}>,
206-
style?: (input: { content: string, attributes: Record<string, string>, filename: string }) => Promise<{
206+
style?: (input: { content: string, markup: string, attributes: Record<string, string>, filename: string }) => Promise<{
207207
code: string,
208208
dependencies?: Array<string>
209209
}>
@@ -242,7 +242,7 @@ const { code } = await svelte.preprocess(source, {
242242

243243
---
244244

245-
The `script` and `style` functions receive the contents of `<script>` and `<style>` elements respectively. In addition to `filename`, they get an object of the element's attributes.
245+
The `script` and `style` functions receive the contents of `<script>` and `<style>` elements respectively (`content`) as well as the entire component source text (`markup`). In addition to `filename`, they get an object of the element's attributes.
246246

247247
If a `dependencies` array is returned, it will be included in the result object. This is used by packages like [rollup-plugin-svelte](https://github.com/sveltejs/rollup-plugin-svelte) to watch additional files for changes, in the case where your `<style>` tag has an `@import` (for example).
248248

src/compiler/preprocess/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ async function process_tag(
139139
preprocessor: Preprocessor,
140140
source: Source
141141
): Promise<SourceUpdate> {
142-
const { filename } = source;
142+
const { filename, source: markup } = source;
143143
const tag_regex =
144144
tag_name === 'style'
145145
? /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi
@@ -160,6 +160,7 @@ async function process_tag(
160160
const processed = await preprocessor({
161161
content: content || '',
162162
attributes: parse_tag_attributes(attributes || ''),
163+
markup,
163164
filename
164165
});
165166

src/compiler/preprocess/types.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,15 @@ export type MarkupPreprocessor = (options: {
2020
}) => Processed | Promise<Processed>;
2121

2222
export type Preprocessor = (options: {
23+
/**
24+
* The script/style tag content
25+
*/
2326
content: string;
2427
attributes: Record<string, string | boolean>;
28+
/**
29+
* The whole Svelte file content
30+
*/
31+
markup: string;
2532
filename?: string;
2633
}) => Processed | Promise<Processed>;
2734

0 commit comments

Comments
 (0)