forked from vuejs/vue2-ssr-docs
-
Notifications
You must be signed in to change notification settings - Fork 9
Japanese Translation: ja/css.md #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
kazupon
merged 5 commits into
open-source-translators:master
from
kaorios:gitlocalize-42
May 16, 2017
Merged
Changes from 3 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
# CSS の管理 | ||
|
||
CSS を管理するためのおすすめの方法は、シンプルに単一ファイルコンポーネントである `*.vue` の中で `<style>`を使うことです。これは以下を提供します: | ||
|
||
- 関連するコンポーネントのスコープ付き CSS | ||
- プリプロセッサや PostCSS を活用する機能 | ||
- 開発時のホットリロード | ||
|
||
さらに重要なことは、`vue-loader` によって内部的に使われている `vue-style-loader` はサーバーレンダリングのためのいくつかの特別な機能を持っています: | ||
|
||
- クライアントとサーバーのための一般的な変換 | ||
- `bundleRenderer` を使用した時の自動的なCSS評価 | ||
|
||
もしサーバレンダリングで使用するなら、コンポーネントの CSS はHTMLに集められてインライン化されます ( `template` オプションを使用していれば自動で扱われます ) 。クライアント上で、コンポーネントが初めて使用されたとき、`vue-style-loader` は既にそのコンポーネントにサーバーインラインCSSがあるかチェックします。もし存在しない場合、そのCSSは動的に `<style>` タグ経由で注入されます。 | ||
|
||
- 共通する CSS の抽出 | ||
|
||
この設定は [`extract-text-webpack-plugin`](https://github.com/webpack-contrib/extract-text-webpack-plugin) を使ってメインチャンクのCSSを個別のCSSに抽出することをサポートします ( `template` で自動注入されます ) 。これは、ファイルが個々にキャッシュされることをゆるしています。共通するCSSがたくさんある場合にこの方法をおすすめします。 | ||
|
||
非同期コンポーネントの CSS は JavaScript の文字列としてインライン化されたままになり、`vue-style-loader` によって扱われます。 | ||
|
||
## CSS 抽出の有効化 | ||
|
||
`*.vue` から CSS を抽出するために、`vue-loader` の `extractCSS` オプションを使います ( `vue-loader>=12.0.0` が必要 ) : | ||
|
||
```js | ||
// webpack.config.js | ||
const ExtractTextPlugin = require('extract-text-webpack-plugin') | ||
// CSS extraction should only be enabled for production | ||
// so that we still get hot-reload during development. | ||
const isProduction = process.env.NODE_ENV === 'production' | ||
module.exports = { | ||
// ... | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.vue$/, | ||
loader: 'vue-loader', | ||
options: { | ||
// enable CSS extraction | ||
extractCSS: isProduction | ||
} | ||
}, | ||
// ... | ||
] | ||
}, | ||
plugins: isProduction | ||
// make sure to add the plugin! | ||
? [new ExtractTextPlugin({ filename: 'common.[chunkhash].css' })] | ||
: [] | ||
} | ||
``` | ||
|
||
上記の設定は `*.vue` ファイルのスタイルのみに適用されますが、外部 CSS を Vue コンポーネントにインポートするために `<style src="./foo.css"></style>` を使うことができます。 | ||
|
||
もし `import 'foo.css'` のように JavaScriptからCSSをインポートしたいならば、適切な loader の設定が必要です: | ||
|
||
```js | ||
module.exports = { | ||
// ... | ||
module: { | ||
rules: [ | ||
{ | ||
test: /\.css$/, | ||
// important: use vue-style-loader instead of style-loader | ||
use: isProduction | ||
? ExtractTextPlugin.extract({ | ||
use: 'css-loader', | ||
fallback: 'vue-style-loader' | ||
}) | ||
: ['vue-style-loader', 'css-loader'] | ||
} | ||
] | ||
}, | ||
// ... | ||
} | ||
``` | ||
|
||
## 依存関係からのスタイルのインポート | ||
|
||
NPM 依存で CSS をインポートするときに気を付けることがいくつかあります: | ||
|
||
1. サーバービルドで外部化しないでください。 | ||
2. もし CSS抽出 + `CommonsChunkPlugin` でベンダー抽出を使用している場合、抽出されたベンダーのチャンクに抽出された CSS があれば、`extract-text-webpack-plugin` に問題が発生します。この問題を解決するためには、ベンダーのチャンクに CSS ファイルを含めないでください。クライアント側の webpack の設定例です: | ||
|
||
```js | ||
module.exports = { | ||
// ... | ||
plugins: [ | ||
// it is common to extract deps into a vendor chunk for better caching. | ||
new webpack.optimize.CommonsChunkPlugin({ | ||
name: 'vendor', | ||
minChunks: function (module) { | ||
// a module is extracted into the vendor chunk when... | ||
return ( | ||
// if it's inside node_modules | ||
/node_modules/.test(module.context) && | ||
// do not externalize if the request is a CSS file | ||
!/\.css$/.test(module.request) | ||
) | ||
} | ||
}), | ||
// extract webpack runtime & manifest | ||
new webpack.optimize.CommonsChunkPlugin({ | ||
name: 'manifest' | ||
}), | ||
// ... | ||
] | ||
} | ||
``` |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Universal authoring experience
の訳ですが、Vue の SSR はクライアント向け、サーバー向けどちらに対応できるようよしなにCSSに対する面倒な処理をやってくれる、以降の説明の内容と、front-end界隈のコンテキストからすると、一般的な変換
ではなく、ユニバーサル変換処理の体験
にしましょう。There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@kazupon レビューありがとうございます!最後まで迷っていた箇所なので、ご指摘ありがたいです。修正しているので、ご確認お願いいたします。