@@ -7,11 +7,13 @@ import { LetDirectiveCollections } from "./let-directive-collection"
7
7
import { getParserName } from "../parser/resolve-parser"
8
8
9
9
export class ScriptsSourceCode {
10
- private readonly raw
10
+ private raw : string
11
+
12
+ private trimmedRaw : string
11
13
12
14
public readonly attrs : Record < string , string | undefined >
13
15
14
- private _vcode : string | null = null
16
+ private _appendScriptLets : string | null = null
15
17
16
18
public separateSemiIndex : number
17
19
@@ -20,32 +22,44 @@ export class ScriptsSourceCode {
20
22
attrs : Record < string , string | undefined > ,
21
23
) {
22
24
this . raw = script
25
+ this . trimmedRaw = script . trimEnd ( )
23
26
this . attrs = attrs
24
27
this . separateSemiIndex = script . length
25
28
}
26
29
27
30
public get vcode ( ) : string {
28
- if ( this . _vcode == null ) {
31
+ if ( this . _appendScriptLets == null ) {
29
32
return this . raw
30
33
}
31
- return this . _vcode
34
+ return this . trimmedRaw + this . _appendScriptLets
32
35
}
33
36
34
37
public addLet ( letCode : string ) : { start : number ; end : number } {
35
- if ( this . _vcode == null ) {
36
- this . _vcode = this . raw . trimEnd ( )
37
- this . separateSemiIndex = this . _vcode . length
38
- this . _vcode += ";"
39
- const after = this . raw . slice ( this . _vcode . length )
40
- this . _vcode += after
38
+ if ( this . _appendScriptLets == null ) {
39
+ this . _appendScriptLets = ""
40
+ this . separateSemiIndex = this . vcode . length
41
+ this . _appendScriptLets += ";"
42
+ const after = this . raw . slice ( this . vcode . length )
43
+ this . _appendScriptLets += after
41
44
}
42
- const start = this . _vcode . length
43
- this . _vcode += letCode
45
+ const start = this . vcode . length
46
+ this . _appendScriptLets += letCode
44
47
return {
45
48
start,
46
- end : this . _vcode . length ,
49
+ end : this . vcode . length ,
47
50
}
48
51
}
52
+
53
+ public stripCode ( start : number , end : number ) : void {
54
+ this . raw =
55
+ this . raw . slice ( 0 , start ) +
56
+ this . raw . slice ( start , end ) . replace ( / [ ^ \n \r ] / g, " " ) +
57
+ this . raw . slice ( end )
58
+ this . trimmedRaw =
59
+ this . trimmedRaw . slice ( 0 , start ) +
60
+ this . trimmedRaw . slice ( start , end ) . replace ( / [ ^ \n \r ] / g, " " ) +
61
+ this . trimmedRaw . slice ( end )
62
+ }
49
63
}
50
64
export type ContextSourceCode = {
51
65
template : string
@@ -202,6 +216,10 @@ export class Context {
202
216
203
217
return ( this . state . isTypeScript = false )
204
218
}
219
+
220
+ public stripScriptCode ( start : number , end : number ) : void {
221
+ this . sourceCode . scripts . stripCode ( start , end )
222
+ }
205
223
}
206
224
207
225
/** Extract <script> blocks */
0 commit comments