Skip to content

Commit 157f8ea

Browse files
committed
Revert "switch from regex check to svelteCompiler.parse"
This reverts commit 731cdc0.
1 parent 731cdc0 commit 157f8ea

File tree

2 files changed

+13
-6
lines changed

2 files changed

+13
-6
lines changed

packages/svelte/rollup.npm.config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,6 @@ import { makeBaseNPMConfig, makeNPMConfigVariants } from '../../rollup/index.js'
33
export default makeNPMConfigVariants(
44
makeBaseNPMConfig({
55
// Prevent 'svelte/internal' stuff from being included in the built JS
6-
packageSpecificConfig: { external: ['svelte/internal', 'svelte/compiler'] },
6+
packageSpecificConfig: { external: ['svelte/internal'] },
77
}),
88
);

packages/svelte/src/preprocessors.ts

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import MagicString from 'magic-string';
2-
import * as svelteCompiler from 'svelte/compiler';
32
import { PreprocessorGroup } from 'svelte/types/compiler/preprocess';
43

54
import { ComponentTrackingInitOptions, SentryPreprocessorGroup, TrackComponentOptions } from './types';
@@ -124,8 +123,16 @@ function getBaseName(filename: string): string {
124123
}
125124

126125
function hasScriptTag(content: string): boolean {
127-
const ast = svelteCompiler.parse(content);
128-
// ast.instance holds whatever is defined in <script>
129-
// if there is no script tag, it's undefined.
130-
return ast.instance !== undefined;
126+
// This regex is taken from the Svelte compiler code.
127+
// They use this regex to find matching script tags that are passed to the `script` preprocessor hook:
128+
// https://github.com/sveltejs/svelte/blob/bb83eddfc623437528f24e9fe210885b446e72fa/src/compiler/preprocess/index.ts#L144
129+
// However, we remove the first part of the regex to not match HTML comments
130+
const scriptTagRegex = /<script(\s[^]*?)?(?:>([^]*?)<\/script>|\/>)/gi;
131+
132+
// Regex testing is not a super safe way of checking for the presence of a <script> tag in the Svelte
133+
// component file but I think we can use it as a start.
134+
// A case that is not covered by regex-testing HTML is e.g. nested <script> tags but I cannot
135+
// think of why one would do this in Svelte components. For instance, the Svelte compiler errors
136+
// when there's more than one top-level script tag.
137+
return scriptTagRegex.test(content);
131138
}

0 commit comments

Comments
 (0)