2
2
3
3
## 前書き
4
4
5
- 多くの Vue プロジェクトでは、 グローバルコンポーネントは ` app.mount('#app') ` の後に各ページの body においてコンテナ要素のターゲットすることに続いて、` app.component() ` を使用して定義されています。
5
+ 多くの Vue プロジェクトでは、グローバルコンポーネントは ` app.mount('#app') ` の後に各ページの body においてコンテナ要素のターゲットすることに続いて、` app.component() ` を使用して定義されています。
6
6
7
7
これはある view を拡張するためだけに JavaScript が利用された中小規模のプロジェクトにおいてはとても有効です。しかしながら、あなたのフロントエンドで JavaScript 全体を操作するようなもっと複雑なプロジェクトでは、これらの点において不利益になります。:
8
8
9
9
- ** グローバル宣言** は全てのコンポーネントに一意な名前を強制すること
10
10
- シンタックスハイライトがない** 文字列テンプレート** と複数行 HTML の時の醜いスラッシュが強要されること
11
11
- ** CSS サポート無し** だと HTML と JavaScript がコンポーネントにモジュール化されている間、これ見よがしに無視されること
12
- - ** ビルド処理がない** と Pug(前 Jade)と Babel のようなプリプロセッサよりむしろ、 HTML や ES5 JavaScript を制限されること
12
+ - ** ビルド処理がない** と Pug(前 Jade)と Babel のようなプリプロセッサよりむしろ、HTML や ES5 JavaScript を制限されること
13
13
14
14
これらは全ては Webpack や Browserify のビルドツールにより実現された ` .vue ` 拡張子の** 単一ファイルコンポーネント** によって解決されます。
15
15
23
23
- [ CommonJS モジュール] ( https://webpack.js.org/concepts/modules/#what-is-a-webpack-module )
24
24
- [ コンポーネントスコープ CSS] ( https://vue-loader.vuejs.org/en/features/scoped-css.html )
25
25
26
- 約束した通り、Pug、Babel (ES2015 モジュールと一緒に) や Stylus など 綺麗で機能が豊富なコンポーネントもプリプロセッサとして利用することができます 。
26
+ 約束した通り、Pug、Babel (ES2015 モジュールと一緒に) や Stylus など綺麗で機能が豊富なコンポーネントもプリプロセッサとして利用することができます 。
27
27
28
28
<a href =" https://codesandbox.io/s/vue-single-file-component-with-pre-processors-mr3ik?file=/src/App.vue " target =" _blank " rel =" noopener noreferrer " ><img src =" /images/sfc-with-preprocessors.png " width =" 563 " alt =" Single-file component with pre-processors example (click for code as text) " style =" display : block ; margin : 15px auto ; max-width : 100% " ></a >
29
29
30
30
これらの特定の言語は単なる一例です。TypeScript、SCSS、PostCSS などの生産的なプリプロセッサも簡単に使うことができます。もし ` vue-loader ` で Webpack を使用しているならば、CSS Modules 向けに素晴らしいサポートがあります。
31
31
32
32
### 関心の分離について
33
33
34
- 注意すべき重要な点の1つは 、** 関心の分離がファイルタイプの分離とは等しくないことです。** 現代の UI 開発では、私たちはコードベースを互いに織りなす3つの巨大なレイヤーに分割するのではなく 、それらを疎結合なコンポーネントに分割して構成する方がはるかに理にかなっていることを発見しました。コンポーネントの内部では、そのテンプレート、ロジック、スタイルが本質的に結合しており、実際にそれらを配置することでコンポーネントの一貫性と保守性が高くなります。
34
+ 注意すべき重要な点の 1 つは 、** 関心の分離がファイルタイプの分離とは等しくないことです。** 現代の UI 開発では、私たちはコードベースを互いに織りなす 3 つの巨大なレイヤーに分割するのではなく 、それらを疎結合なコンポーネントに分割して構成する方がはるかに理にかなっていることを発見しました。コンポーネントの内部では、そのテンプレート、ロジック、スタイルが本質的に結合しており、実際にそれらを配置することでコンポーネントの一貫性と保守性が高くなります。
35
35
36
36
単一ファイルコンポーネントのアイディアが好きではなくても、JavaScript と CSS を別々ファイルに分けることによってホットリロードとプリコンパイル機能を活用することができます:
37
37
58
58
59
59
- ** ES2015/16 を使ったモダンな JavaScript** : Babel の [ Learn ES2015 guide] ( https://babeljs.io/docs/en/learn ) を読んでみてください。今すぐには全ての機能を暗記する必要はないですが、参考として戻れるようにしておいてください。
60
60
61
- これらのリソースに没頭した後は、[ Vue CLI] ( https://cli.vuejs.org/ ) を確認することをお勧めします。 指示に従うことであっという間に ` .vue ` コンポーネントと ES2015、 Webpack、ホットリローティングを備えた Vue プロジェクトを手に入れられるはずです!
61
+ これらのリソースに没頭した後は、[ Vue CLI] ( https://cli.vuejs.org/ ) を確認することをお勧めします。 指示に従うことであっという間に ` .vue ` コンポーネントと ES2015、Webpack、ホットリローティングを備えた Vue プロジェクトを手に入れられるはずです!
62
62
63
63
### 上級ユーザ向け
64
64
65
65
CLI はツールの設定の大部分の面倒を見てくれますが、[ 設定オプション] ( https://cli.vuejs.org/config/ ) を通してよりきめ細かいカスタマイズをすることもできます。
66
66
67
67
あなたが独自のビルドセットアップをゼロから作ることを好む場合、webpack と [ vue-loader] ( https://vue-loader.vuejs.org ) を手動で設定する必要があるでしょう。webpack についてもっと学びたいときは、[ 公式ドキュメント] ( https://webpack.js.org/configuration/ ) や [ webpack learning academy] ( https://webpack.academy/p/the-core-concepts ) を参照してください。
68
68
69
- ### Building with rollup
69
+ ### Rollup でビルド
70
70
71
- Most of the time when developing a third-party library we want to build it in a way that allows the consumers of the library to [ tree shake ] ( https://webpack.js.org/guides/tree-shaking/ ) it. To enable tree-shaking we need to build ` esm ` modules. Since webpack and, in turn, vue-cli do not support building ` esm ` modules we need to rely on [ rollup ] ( https://rollupjs.org/ ) .
71
+ ほとんどの場合、サードパーティのライブラリを開発するときは、そのライブラリの利用者が [ Tree Shaking ] ( https://webpack.js.org/guides/tree-shaking/ ) できるような方法でビルドしたいと考えています。Tree Shaking を有効にするためには、 ` esm ` モジュールをビルドする必要があります。 Webpack や Vue CLI は ` esm ` モジュールのビルドをサポートしていないため、 [ Rollup ] ( https://rollupjs.org/ ) の依存が必要です。
72
72
73
- #### Installing Rollup
73
+ #### Rollup のインストール
74
74
75
- We will need to install Rollup and a few dependencies :
75
+ Rollup といくつかの依存関係をインストールする必要があります :
76
76
77
77
``` bash
78
78
npm install --save-dev rollup @rollup/plugin-commonjs rollup-plugin-vue
79
79
```
80
80
81
- These are the minimal amount of rollup plugins that we need to use to compile the code in an ` esm ` module. We may want to also add [ rollup-plugin-babel] ( https://github.com/rollup/plugins/tree/master/packages/babel ) to transpile their code and [ node-resolve] ( https://github.com/rollup/plugins/tree/master/packages/node-resolve ) if we use dependencies that we want to bundle with our library.
81
+ これらは ` esm ` モジュールのコードをコンパイルするために必要な最小限の Rollup プラグインです。コードをコンパイルするために [ rollup-plugin-babel] ( https://github.com/rollup/plugins/tree/master/packages/babel ) を、ライブラリにバンドルしたい依存関係を使うならば [ node-resolve] ( https://github.com/rollup/plugins/tree/master/packages/node-resolve ) を追加できます。
82
82
83
- #### Configuring Rollup
83
+ #### Rollup の設定
84
84
85
- To configure our build with Rollup we will need to create a ` rollup.config.js ` file in the root of our project :
85
+ Rollup でビルドを設定するには、プロジェクトのルートに ` rollup.config.js ` ファイルを作成する必要があります :
86
86
87
87
``` bash
88
88
touch rollup.config.js
89
89
```
90
90
91
- Once the file is created we will need to open it with our editor of choice and add the following code.
91
+ ファイルを作成したら、お好きなエディタで開き、以下のコードを追加します。
92
92
93
93
``` js
94
- // import our third party plugins
94
+ // サードパーティのプラグインをインポート
95
95
import commonjs from ' rollup-plugin-commonjs'
96
96
import VuePlugin from ' rollup-plugin-vue'
97
- import pkg from ' ./package.json' // import our package.json file to re-use the naming
97
+ import pkg from ' ./package.json' // 名前を再利用するために package.json ファイルをインポート
98
98
99
99
export default {
100
- // this is the file containing all our exported components/functions
100
+ // エクスポートされたすべてのコンポーネントや関数を含むファイル
101
101
input: ' src/index.js' ,
102
- // this is an array of outputted formats
102
+ // 出力されるフォーマットの配列
103
103
output: [
104
104
{
105
- file: pkg .module , // the name of our esm library
106
- format: ' esm' , // the format of choice
107
- sourcemap: true , // ask rollup to include sourcemaps
105
+ file: pkg .module , // esm ライブラリの名前
106
+ format: ' esm' , // フォーマットの選択
107
+ sourcemap: true , // ソースマップを含めるか
108
108
}
109
109
],
110
- // this is an array of the plugins that we are including
110
+ // 含めるプラグインの配列
111
111
plugins: [
112
112
commonjs (),
113
113
VuePlugin ()
114
114
],
115
- // ask rollup to not bundle Vue in the library
115
+ // ライブラリに Vue をバンドルしないように
116
116
external: [' vue' ]
117
117
}
118
118
```
119
119
120
- #### Configuring package.json
120
+ #### package.json の設定
121
121
122
- To take advantage of our newly created ` esm ` module we need to add a few fields in our ` package.json ` file :
122
+ 新しく作成した ` esm ` モジュールを利用するためには、 ` package.json ` ファイルにいくつかのフィールドを追加する必要があります :
123
123
124
124
``` json
125
125
"scripts" : {
@@ -133,15 +133,15 @@ To take advantage of our newly created `esm` module we need to add a few fields
133
133
],
134
134
```
135
135
136
- Here we are specifying :
136
+ ここで指定しています :
137
137
138
- - how to build our package
139
- - what files we want to bundle in our package
140
- - what file represents our ` esm ` module
138
+ - パッケージのビルド方法
139
+ - パッケージにバンドルするファイル
140
+ - ` esm ` モジュールを示すファイル
141
141
142
- #### Bundling ` umd ` and ` cjs ` modules
142
+ #### ` umd ` と ` cjs ` のバンドル
143
143
144
- To also build ` umd ` and ` cjs ` modules we can simply add a few lines of configuration to our ` rollup.config.js ` and ` package.json `
144
+ ` umd ` と ` cjs ` モジュールを一緒にビルドするには、 ` rollup.config.js ` と ` package.json ` に数行の設定を追加するだけです。
145
145
146
146
##### rollup.config.js
147
147
``` js
0 commit comments