How to parse vue files
This time I will show you how to parse vue files, and what are the precautions for parsing vue files. The following is a practical case, let's take a look.
The .vue files we usually write are called SFC (Single File Components). This article introduces how the process of parsing SFC into descriptors is performed in vue.
vue provides a compiler.parseComponent(file, [options]) method to parse the .vue file into a descriptor:
// an object format describing a single-file component. declare type SFCDescriptor = { template: ?SFCBlock; script: ?SFCBlock; styles: Array<SFCBlock>; customBlocks: Array<SFCBlock>; };
File entry
The entry point for parsing sfc files is in src/sfc/parser.js. This file exports the parseComponent method. The parseComponent method is used to compile single-file components.
Next let’s take a look at what the parseComponent method does.
parseComponent method
function start(tag, attrs, unary, start, end,){ } function end(tag, start, end){ } parseHTML(content, { start, end })
The parseComponent method defines two functions, start``end, and then calls the parseHTML method to compile the contents of the .vue file.
So what does this parseHTML method do?
parseHTML method
You can tell from the name that this method is an html-parser. It can be simply understood that when each start tag is parsed, the option in option is called. start; at the end of each label, call the end in option.
Corresponding to this is to call the start and end functions defined in the parseComponent method respectively.
Maintain a depth variable in parseComponent, set depth in start and depth-- in end. Then, each tag with depth === 0 is the information we need to obtain, including template, script, style and some custom tags.
start
Whenever a start tag is encountered, the start function is executed.
1. Record currentBlock.
Each currentBlock contains the following content:
declare type SFCBlock = { type: string; content: string; start?: number; end?: number; lang?: string; src?: string; scoped?: boolean; module?: string | boolean; };
2. According to the tag name, put the currentBlock object in the returned result object.
The returned result object is defined as sfc. If the tag is not any of script, style, and template, it is placed in sfc.customBlocks. If it is style, put it in sfc.styles. script and template are placed directly under sfc.
if (isSpecialTag(tag)) { checkAttrs(currentBlock, attrs) if (tag === 'style') { sfc.styles.push(currentBlock) } else { sfc[tag] = currentBlock } } else { // custom blocks sfc.customBlocks.push(currentBlock) }
end
Whenever an end tag is encountered, the end function is executed.
1. If the current label is the first layer (depth === 1), and the currentBlock variable exists, then take out this part of the text and put it in currentBlock.content.
if (depth === 1 && currentBlock) { currentBlock.end = start let text = deindent(content.slice(currentBlock.start, currentBlock.end)) // pad content so that linters and pre-processors can output correct // line numbers in errors and warnings if (currentBlock.type !== 'template' && options.pad) { text = padContent(currentBlock, options.pad) + text } currentBlock.content = text currentBlock = null }
2, depth-- .
Get descriptor
After traversing the entire .vue, the sfc object obtained is the result we need.
Generate .js?
compiler.parseComponent(file, [options]) gets only the SFCDescriptor of a component, and the final compilation into a .js file is handed over to libraries such as vue-loader.
I believe you have mastered the method after reading the case in this article. For more exciting information, please pay attention to other related articles on the php Chinese website!
Recommended reading:
How to solve the error in configuring the packaging file path
How to make the font switching effect of clicking the title text
The above is the detailed content of How to parse vue files. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Frequently Asked Questions and Solutions for Front-end Thermal Paper Ticket Printing In Front-end Development, Ticket Printing is a common requirement. However, many developers are implementing...

There is no absolute salary for Python and JavaScript developers, depending on skills and industry needs. 1. Python may be paid more in data science and machine learning. 2. JavaScript has great demand in front-end and full-stack development, and its salary is also considerable. 3. Influencing factors include experience, geographical location, company size and specific skills.

JavaScript is the cornerstone of modern web development, and its main functions include event-driven programming, dynamic content generation and asynchronous programming. 1) Event-driven programming allows web pages to change dynamically according to user operations. 2) Dynamic content generation allows page content to be adjusted according to conditions. 3) Asynchronous programming ensures that the user interface is not blocked. JavaScript is widely used in web interaction, single-page application and server-side development, greatly improving the flexibility of user experience and cross-platform development.

How to merge array elements with the same ID into one object in JavaScript? When processing data, we often encounter the need to have the same ID...

Learning JavaScript is not difficult, but it is challenging. 1) Understand basic concepts such as variables, data types, functions, etc. 2) Master asynchronous programming and implement it through event loops. 3) Use DOM operations and Promise to handle asynchronous requests. 4) Avoid common mistakes and use debugging techniques. 5) Optimize performance and follow best practices.

Discussion on the realization of parallax scrolling and element animation effects in this article will explore how to achieve similar to Shiseido official website (https://www.shiseido.co.jp/sb/wonderland/)...

In-depth discussion of the root causes of the difference in console.log output. This article will analyze the differences in the output results of console.log function in a piece of code and explain the reasons behind it. �...

The latest trends in JavaScript include the rise of TypeScript, the popularity of modern frameworks and libraries, and the application of WebAssembly. Future prospects cover more powerful type systems, the development of server-side JavaScript, the expansion of artificial intelligence and machine learning, and the potential of IoT and edge computing.
