Skip to content

Commit

Permalink
Pass full markup source to preprocessors (#6169)
Browse files Browse the repository at this point in the history
  • Loading branch information
SomaticIT committed Apr 12, 2021
1 parent a55295d commit 08047c1
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 3 additions & 3 deletions site/content/docs/04-compile-time.md
Original file line number Diff line number Diff line change
Expand Up @@ -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>
}>
Expand Down Expand Up @@ -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).

Expand Down
3 changes: 2 additions & 1 deletion src/compiler/preprocess/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -160,6 +160,7 @@ async function process_tag(
const processed = await preprocessor({
content: content || '',
attributes: parse_tag_attributes(attributes || ''),
markup,
filename
});

Expand Down
7 changes: 7 additions & 0 deletions src/compiler/preprocess/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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>;

Expand Down

0 comments on commit 08047c1

Please sign in to comment.