From 3fb9626eb45076101a5974c77163d8c2504f9b84 Mon Sep 17 00:00:00 2001 From: Tan Li Hau Date: Tue, 12 Nov 2019 09:01:07 +0800 Subject: [PATCH] skip comments for preprocessors --- src/compiler/preprocess/index.ts | 11 ++++++-- test/preprocess/samples/comments/_config.js | 8 ++++++ test/preprocess/samples/comments/input.svelte | 25 +++++++++++++++++++ .../preprocess/samples/comments/output.svelte | 25 +++++++++++++++++++ 4 files changed, 67 insertions(+), 2 deletions(-) create mode 100644 test/preprocess/samples/comments/_config.js create mode 100644 test/preprocess/samples/comments/input.svelte create mode 100644 test/preprocess/samples/comments/output.svelte diff --git a/src/compiler/preprocess/index.ts b/src/compiler/preprocess/index.ts index 2faa3b97f2..4549517fbd 100644 --- a/src/compiler/preprocess/index.ts +++ b/src/compiler/preprocess/index.ts @@ -94,8 +94,12 @@ export default async function preprocess( for (const fn of script) { source = await replace_async( source, - /([^]*?)<\/script>/gi, + /|([^]*?)<\/script>/gi, async (match, attributes = '', content) => { + if (!attributes && !content) { + return match; + } + attributes = attributes || ''; const processed = await fn({ content, attributes: parse_attributes(attributes), @@ -110,8 +114,11 @@ export default async function preprocess( for (const fn of style) { source = await replace_async( source, - /([^]*?)<\/style>/gi, + /|([^]*?)<\/style>/gi, async (match, attributes = '', content) => { + if (!attributes && !content) { + return match; + } const processed: Processed = await fn({ content, attributes: parse_attributes(attributes), diff --git a/test/preprocess/samples/comments/_config.js b/test/preprocess/samples/comments/_config.js new file mode 100644 index 0000000000..4416d121d0 --- /dev/null +++ b/test/preprocess/samples/comments/_config.js @@ -0,0 +1,8 @@ +export default { + preprocess: [ + { + script: ({ content }) => ({ code: content.replace(/one/g, 'two') }), + style: ({ content }) => ({ code: content.replace(/one/g, 'three') }), + }, + ], +}; diff --git a/test/preprocess/samples/comments/input.svelte b/test/preprocess/samples/comments/input.svelte new file mode 100644 index 0000000000..1a01971f7f --- /dev/null +++ b/test/preprocess/samples/comments/input.svelte @@ -0,0 +1,25 @@ + + + + + + + + + + + + + diff --git a/test/preprocess/samples/comments/output.svelte b/test/preprocess/samples/comments/output.svelte new file mode 100644 index 0000000000..2d3538af17 --- /dev/null +++ b/test/preprocess/samples/comments/output.svelte @@ -0,0 +1,25 @@ + + + + + + + + + + + + +