Skip to content

Commit

Permalink
chore: phrasing (10-minimum-example/091-parse-sfc)
Browse files Browse the repository at this point in the history
  • Loading branch information
ubugeeei committed Dec 14, 2024
1 parent de2804b commit 839e897
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 18 deletions.
26 changes: 15 additions & 11 deletions book/online-book/src/10-minimum-example/091-parse-sfc.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,8 @@ export default function vitePluginChibivue(): Plugin {
}
```

Now, let's implement the SFC compiler. However, it may be difficult to imagine without any substance, so let's implement a playground and do it while running it.
Now, let's implement the SFC compiler. \
However, it may be difficult to imagine without any substance, so let's implement a playground and do it while running it.
We will create a simple SFC and load it.

```sh
Expand Down Expand Up @@ -143,9 +144,10 @@ Of course, it will result in an error. Well done (?).

## Resolving the Error

Let's resolve the error for now. We don't aim for perfection right away.
First, let's limit the target of `transform` to "\*.vue".
We can write a branching statement with `id` as we did in the sample, but since Vite provides a function called `createFilter`, let's create a filter using that. (There is no particular reason for this.)
Let's resolve the error for now. We don't aim for perfection right away.\
First, let's limit the target of `transform` to "\*.vue".\
We can write a branching statement with `id` as we did in the sample, but since Vite provides a function called `createFilter`, let's create a filter using that.\
(There is no particular reason for this.)

`~/packages/@extensions/vite-plugin-chibivue/index.ts`

Expand All @@ -167,13 +169,13 @@ export default function vitePluginChibivue(): Plugin {
}
```

We created a filter and transformed the file content to `export default {}` if it was a Vue file.
We created a filter and transformed the file content to `export default {}` if it was a Vue file.\
The error should disappear and the screen should not display anything.

## Implementation of the Parser on compiler-sfc

Now, this is just a temporary solution, so let's implement a proper solution.
The role of vite-plugin is to enable transformation with Vite, so the parsing and compilation are in the main Vue package.
Now, this is just a temporary solution, so let's implement a proper solution.\
The role of vite-plugin is to enable transformation with Vite, so the parsing and compilation are in the main Vue package.\
That is the `compiler-sfc` directory.

```mermaid
Expand Down Expand Up @@ -203,7 +205,8 @@ That is the `compiler-sfc` directory.

https://github.com/vuejs/core/blob/main/.github/contributing.md#package-dependencies

The SFC compiler is the same for both Vite and Webpack. The core implementation is in `compiler-sfc`.
The SFC compiler is the same for both Vite and Webpack. \
The core implementation is in `compiler-sfc`.

Let's create `compiler-sfc`.

Expand Down Expand Up @@ -252,10 +255,11 @@ export declare interface SFCStyleBlock extends SFCBlock {
}
```

Well, there's nothing particularly difficult. It's just an object that represents the SFC information.
Well, there's nothing particularly difficult. \
It's just an object that represents the SFC information.

In `packages/compiler-sfc/parse.ts`, we will parse the SFC file (string) into `SFCDescriptor`.
Some of you may be thinking, "What? You worked so hard on the template parser, and now you're creating another parser...? It's a hassle." But don't worry.
In `packages/compiler-sfc/parse.ts`, we will parse the SFC file (string) into `SFCDescriptor`.\
Some of you may be thinking, "What? You worked so hard on the template parser, and now you're creating another parser...? It's a hassle." But don't worry.\
The parser we're going to implement here is not a big deal. That's because we're just separating the template, script, and style by combining what we've created so far.

First, as a preparation, export the template parser we created earlier.
Expand Down
14 changes: 7 additions & 7 deletions book/online-book/src/ja/10-minimum-example/091-parse-sfc.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

## 準備

先ほど作ったサンプルのプラグインなのですが,もう不要なので消してしまいましょう
先ほど作ったサンプルのプラグインなのですが,もう不要なので消してしまいましょう

```sh
pwd # ~
Expand All @@ -16,7 +16,7 @@ pwd # ~
ni vite
```

plugin の本体なのですが,本来これは vuejs/core の範囲外なので packages に`@extensions`というディレクトリを切ってそこに実装していきます.
plugin の本体なのですが,本来これは vuejs/core の範囲外なので packages に `@extensions` というディレクトリを切ってそこに実装していきます.

```sh
pwd # ~
Expand Down Expand Up @@ -144,7 +144,7 @@ export default defineConfig({
## エラーの解消

とりあえずエラーを解消していきましょう.いきなり完璧なものは目指しません.
まず,transform の対象を「\*.vue」に限定してあげましょう.
まず,transform の対象を「\*.vue」に限定してあげましょう.\
sample でやったように id で分岐を書いてもいいのですが,せっかく vite から createFilter という関数が提供されているのでそれでフィルターを作ります.(特に理由はないです.)

`~/packages/@extensions/vite-plugin-chibivue/index.ts`
Expand Down Expand Up @@ -203,9 +203,9 @@ vite-plugin での役割はあくまで vite を利用する際に vite で tran

https://github.com/vuejs/core/blob/main/.github/contributing.md#package-dependencies

SFC のコンパイラは vite だろうが webpack だろうがコアな部分は同じです.それらの実装をになっているのが`compiler-sfc`です.
SFC のコンパイラは vite だろうが webpack だろうがコアな部分は同じです.それらの実装をになっているのが `compiler-sfc` です.

`compiler-sfc`を作っていきましょう.
`compiler-sfc` を作っていきましょう.

```sh
pwd # ~
Expand Down Expand Up @@ -254,7 +254,7 @@ export declare interface SFCStyleBlock extends SFCBlock {

まあ,特に難しいことはないです.SFC の情報をオブジェクトで表現しただけです.

`packages/compiler-sfc/parse.ts`では SFC ファイル(文字列)を `SFCDescriptor` にパースします.
`packages/compiler-sfc/parse.ts` では SFC ファイル(文字列)を `SFCDescriptor` にパースします.
「ええ.あんだけテンプレートのパースを頑張ったのにまたパーサつくるのかよ..面倒臭い」と思った方もいるかも知れませんが,安心してください.
ここで実装するパーサは大したものではないです.というのも,これまで作ってきたものを組み合わせて template,script,style を分離するだけなので楽ちんです.

Expand Down Expand Up @@ -404,7 +404,7 @@ export default function vitePluginChibivue(): Plugin {
}
```

このコードは vite が動いているプロセス,つまり node で実行されるので console はターミナルに出力されているかと思います.
このコードは vite が動いているプロセス,つまり node で実行されるので console はターミナルに出力されているかと思います.\

![parse_sfc1](https://raw.githubusercontent.com/chibivue-land/chibivue/main/book/images/parse_sfc1.png)

Expand Down

0 comments on commit 839e897

Please sign in to comment.