0
0
mirror of https://github.com/sveltejs/svelte.git synced 2024-11-30 00:46:29 +01:00

Pass full markup source to preprocessors (#6169)

This commit is contained in:
Maxime LUCE 2021-04-12 18:00:38 +02:00 committed by GitHub
parent a55295de8c
commit 08047c14b6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 12 additions and 4 deletions

View File

@ -199,11 +199,11 @@ result: {
code: string,
dependencies?: Array<string>
}>,
script?: (input: { content: string, attributes: Record<string, string>, filename: string }) => Promise<{
script?: (input: { content: string, markup: string, attributes: Record<string, string>, filename: string }) => Promise<{
code: string,
dependencies?: Array<string>
}>,
style?: (input: { content: string, attributes: Record<string, string>, filename: string }) => Promise<{
style?: (input: { content: string, markup: string, attributes: Record<string, string>, filename: string }) => Promise<{
code: string,
dependencies?: Array<string>
}>
@ -242,7 +242,7 @@ const { code } = await svelte.preprocess(source, {
---
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.
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.
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).

View File

@ -139,7 +139,7 @@ async function process_tag(
preprocessor: Preprocessor,
source: Source
): Promise<SourceUpdate> {
const { filename } = source;
const { filename, source: markup } = source;
const tag_regex =
tag_name === 'style'
? /<!--[^]*?-->|<style(\s[^]*?)?(?:>([^]*?)<\/style>|\/>)/gi
@ -160,6 +160,7 @@ async function process_tag(
const processed = await preprocessor({
content: content || '',
attributes: parse_tag_attributes(attributes || ''),
markup,
filename
});

View File

@ -20,8 +20,15 @@ export type MarkupPreprocessor = (options: {
}) => Processed | Promise<Processed>;
export type Preprocessor = (options: {
/**
* The script/style tag content
*/
content: string;
attributes: Record<string, string | boolean>;
/**
* The whole Svelte file content
*/
markup: string;
filename?: string;
}) => Processed | Promise<Processed>;