@@ -7,11 +7,13 @@ import { LetDirectiveCollections } from "./let-directive-collection"
77import { getParserName } from "../parser/resolve-parser"
88
99export class ScriptsSourceCode {
10- private readonly raw
10+ private raw : string
11+
12+ private trimmedRaw : string
1113
1214 public readonly attrs : Record < string , string | undefined >
1315
14- private _vcode : string | null = null
16+ private _appendScriptLets : string | null = null
1517
1618 public separateSemiIndex : number
1719
@@ -20,32 +22,44 @@ export class ScriptsSourceCode {
2022 attrs : Record < string , string | undefined > ,
2123 ) {
2224 this . raw = script
25+ this . trimmedRaw = script . trimEnd ( )
2326 this . attrs = attrs
2427 this . separateSemiIndex = script . length
2528 }
2629
2730 public get vcode ( ) : string {
28- if ( this . _vcode == null ) {
31+ if ( this . _appendScriptLets == null ) {
2932 return this . raw
3033 }
31- return this . _vcode
34+ return this . trimmedRaw + this . _appendScriptLets
3235 }
3336
3437 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
4144 }
42- const start = this . _vcode . length
43- this . _vcode += letCode
45+ const start = this . vcode . length
46+ this . _appendScriptLets += letCode
4447 return {
4548 start,
46- end : this . _vcode . length ,
49+ end : this . vcode . length ,
4750 }
4851 }
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+ }
4963}
5064export type ContextSourceCode = {
5165 template : string
@@ -202,6 +216,10 @@ export class Context {
202216
203217 return ( this . state . isTypeScript = false )
204218 }
219+
220+ public stripScriptCode ( start : number , end : number ) : void {
221+ this . sourceCode . scripts . stripCode ( start , end )
222+ }
205223}
206224
207225/** Extract <script> blocks */
0 commit comments