@@ -54,6 +54,10 @@ import {
54
54
} from "../common/parser-options"
55
55
import sortedIndexBy from "lodash/sortedIndexBy"
56
56
import sortedLastIndexBy from "lodash/sortedLastIndexBy"
57
+ import type {
58
+ CustomTemplateTokenizer ,
59
+ CustomTemplateTokenizerConstructor ,
60
+ } from "./custom-tokenizer"
57
61
58
62
const DIRECTIVE_NAME = / ^ (?: v - | [ . : @ # ] ) .* [ ^ . : @ # ] $ / u
59
63
const DT_DD = / ^ d [ d t ] $ / u
@@ -167,7 +171,7 @@ function propagateEndLocation(node: VDocumentFragment | VElement): void {
167
171
* This is not following to the HTML spec completely because Vue.js template spec is pretty different to HTML.
168
172
*/
169
173
export class Parser {
170
- private tokenizer : IntermediateTokenizer
174
+ private tokenizer : IntermediateTokenizer | CustomTemplateTokenizer
171
175
private locationCalculator : LocationCalculatorForHtml
172
176
private baseParserOptions : ParserOptions
173
177
private isSFC : boolean
@@ -480,12 +484,17 @@ export class Parser {
480
484
/**
481
485
* Process the given template text token with a configured template tokenizer, based on language.
482
486
* @param token The template text token to process.
483
- * @param lang The template language the text token should be parsed as .
487
+ * @param templateTokenizerOption The template tokenizer option .
484
488
*/
485
- private processTemplateText ( token : Text , lang : string ) : void {
486
- // eslint-disable-next-line @typescript-eslint/no-require-imports
487
- const TemplateTokenizer = require ( this . baseParserOptions
488
- . templateTokenizer ! [ lang ] )
489
+ private processTemplateText (
490
+ token : Text ,
491
+ templateTokenizerOption : string | CustomTemplateTokenizerConstructor ,
492
+ ) : void {
493
+ const TemplateTokenizer : CustomTemplateTokenizerConstructor =
494
+ typeof templateTokenizerOption === "function"
495
+ ? templateTokenizerOption
496
+ : // eslint-disable-next-line @typescript-eslint/no-require-imports
497
+ require ( templateTokenizerOption )
489
498
const templateTokenizer = new TemplateTokenizer (
490
499
token . value ,
491
500
this . text ,
@@ -696,13 +705,13 @@ export class Parser {
696
705
( a ) => a . key . name === "lang" ,
697
706
)
698
707
const lang = ( langAttribute ?. value as VLiteral ) ?. value
699
- if (
700
- lang &&
701
- lang !== "html" &&
702
- this . baseParserOptions . templateTokenizer ?. [ lang ]
703
- ) {
704
- this . processTemplateText ( token , lang )
705
- return
708
+ if ( lang && lang !== "html" ) {
709
+ const templateTokenizerOption =
710
+ this . baseParserOptions . templateTokenizer ?. [ lang ]
711
+ if ( templateTokenizerOption ) {
712
+ this . processTemplateText ( token , templateTokenizerOption )
713
+ return
714
+ }
706
715
}
707
716
}
708
717
parent . children . push ( {
0 commit comments