Skip to content

Commit

Permalink
skip comments for preprocessors
Browse files Browse the repository at this point in the history
  • Loading branch information
tanhauhau committed Nov 11, 2019
1 parent ad2aac5 commit 5c53cf2
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 12 deletions.
26 changes: 14 additions & 12 deletions src/compiler/preprocess/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,18 @@ interface Replacement {
async function replace_async(str: string, re: RegExp, func: (...any) => Promise<string>) {
const replacements: Array<Promise<Replacement>> = [];
str.replace(re, (...args) => {
replacements.push(
func(...args).then(
res => // eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
({
offset: args[args.length - 2],
length: args[0].length,
replacement: res,
}) as Replacement
)
);
if (args.slice(1, args.length - 2).some(Boolean)) {
replacements.push(
func(...args).then(
res => // eslint-disable-next-line @typescript-eslint/no-object-literal-type-assertion
({
offset: args[args.length - 2],
length: args[0].length,
replacement: res,
}) as Replacement
)
);
}
return '';
});
let out = '';
Expand Down Expand Up @@ -94,7 +96,7 @@ export default async function preprocess(
for (const fn of script) {
source = await replace_async(
source,
/<script(\s[^]*?)?>([^]*?)<\/script>/gi,
/<!--[^]*?-->|<script(\s[^]*?)?>([^]*?)<\/script>/gi,
async (match, attributes = '', content) => {
const processed = await fn({
content,
Expand All @@ -110,7 +112,7 @@ export default async function preprocess(
for (const fn of style) {
source = await replace_async(
source,
/<style(\s[^]*?)?>([^]*?)<\/style>/gi,
/<!--[^]*?-->|<style(\s[^]*?)?>([^]*?)<\/style>/gi,
async (match, attributes = '', content) => {
const processed: Processed = await fn({
content,
Expand Down
8 changes: 8 additions & 0 deletions test/preprocess/samples/comments/_config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
export default {
preprocess: [
{
script: ({ content }) => ({ code: content.replace(/one/g, 'two') }),
style: ({ content }) => ({ code: content.replace(/one/g, 'three') }),
},
],
};
25 changes: 25 additions & 0 deletions test/preprocess/samples/comments/input.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<style>
one
</style>
<script>
one
</script>
<!-- <style>
one
</style> -->
<!-- <script>
one
</script> -->
<style>
<!-- one -->
</style>
<script>
<!-- one -->
</script>
25 changes: 25 additions & 0 deletions test/preprocess/samples/comments/output.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<style>
three
</style>
<script>
two
</script>
<!-- <style>
one
</style> -->
<!-- <script>
one
</script> -->
<style>
<!-- three -->
</style>
<script>
<!-- two -->
</script>

0 comments on commit 5c53cf2

Please sign in to comment.