From 17a3e2885576b9d660bf502ae627195d231ad8fb Mon Sep 17 00:00:00 2001 From: Maarten van Sambeek Date: Wed, 27 May 2020 14:08:37 +0200 Subject: [PATCH] Adds a markup parameter to Preprocessors. This parameter will hold the entire processed source file up until the current preprocessor. Fixes #4912 --- src/compiler/preprocess/index.ts | 7 +++++-- test/preprocess/samples/script-markup/_config.js | 12 ++++++++++++ test/preprocess/samples/script-markup/input.svelte | 5 +++++ test/preprocess/samples/script-markup/output.svelte | 5 +++++ 4 files changed, 27 insertions(+), 2 deletions(-) create mode 100644 test/preprocess/samples/script-markup/_config.js create mode 100644 test/preprocess/samples/script-markup/input.svelte create mode 100644 test/preprocess/samples/script-markup/output.svelte diff --git a/src/compiler/preprocess/index.ts b/src/compiler/preprocess/index.ts index 1a13b869e7af..ccb36f04e1ec 100644 --- a/src/compiler/preprocess/index.ts +++ b/src/compiler/preprocess/index.ts @@ -17,6 +17,7 @@ export type Preprocessor = (options: { content: string; attributes: Record; filename?: string; + markup: string; }) => Processed | Promise; function parse_attributes(str: string) { @@ -103,7 +104,8 @@ export default async function preprocess( const processed = await fn({ content, attributes: parse_attributes(attributes), - filename + filename, + markup: source }); if (processed && processed.dependencies) dependencies.push(...processed.dependencies); return processed ? `${processed.code}` : match; @@ -122,7 +124,8 @@ export default async function preprocess( const processed: Processed = await fn({ content, attributes: parse_attributes(attributes), - filename + filename, + markup: source }); if (processed && processed.dependencies) dependencies.push(...processed.dependencies); return processed ? `${processed.code}` : match; diff --git a/test/preprocess/samples/script-markup/_config.js b/test/preprocess/samples/script-markup/_config.js new file mode 100644 index 000000000000..6a14ef35eb0c --- /dev/null +++ b/test/preprocess/samples/script-markup/_config.js @@ -0,0 +1,12 @@ +export default { + preprocess: { + script: ({ content, markup }) => { + return { + code: content.replace( + "__HASDIVTAG__", + markup && //g.test(markup) ? "'yes'" : "'no'" + ), + }; + }, + }, +}; diff --git a/test/preprocess/samples/script-markup/input.svelte b/test/preprocess/samples/script-markup/input.svelte new file mode 100644 index 000000000000..1c910afed751 --- /dev/null +++ b/test/preprocess/samples/script-markup/input.svelte @@ -0,0 +1,5 @@ + + +
diff --git a/test/preprocess/samples/script-markup/output.svelte b/test/preprocess/samples/script-markup/output.svelte new file mode 100644 index 000000000000..9ca386742a8e --- /dev/null +++ b/test/preprocess/samples/script-markup/output.svelte @@ -0,0 +1,5 @@ + + +