-
Notifications
You must be signed in to change notification settings - Fork 180
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
Translate Code-Splitting #58
Merged
smikitky
merged 14 commits into
reactjs:master
from
sugoikondo:translate/code-splitting
Feb 10, 2019
Merged
Changes from 8 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
b790b03
WIP translating.
sugoikondo 9d4aa3f
translate complete
sugoikondo 9c920db
lint
sugoikondo 6802a92
fix statements on Browserify.
sugoikondo 936e9b0
delete unnessesary statements.
sugoikondo 5cf1524
delete unnessesary statements.
sugoikondo 6570f0a
fix typo
sugoikondo e0d0623
fix typo
sugoikondo bd73272
resolve conflicts
sugoikondo 2ae259a
Apply suggestions from code review
koba04 34773ec
fix breakings.
sugoikondo 92dc443
Apply suggestions from code review
smikitky 2523d51
fix suggestions.
sugoikondo 2e244fd
fix mistranslation on webpack configuration
sugoikondo 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 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 | ||||
---|---|---|---|---|---|---|
@@ -1,18 +1,14 @@ | ||||||
--- | ||||||
id: code-splitting | ||||||
title: Code-Splitting | ||||||
title: コード分割 | ||||||
permalink: docs/code-splitting.html | ||||||
--- | ||||||
|
||||||
## Bundling | ||||||
## バンドル | ||||||
|
||||||
Most React apps will have their files "bundled" using tools like | ||||||
[Webpack](https://webpack.js.org/) or [Browserify](http://browserify.org/). | ||||||
Bundling is the process of following imported files and merging them into a | ||||||
single file: a "bundle". This bundle can then be included on a webpage to load | ||||||
an entire app at once. | ||||||
多くの React アプリケーションは、[Webpack](https://webpack.js.org/)や [Browserify](http://browserify.org/) などのツールを使ってファイルを「バンドル」しています。バンドルはインポートされたファイルをたどって、それらを1つのファイルにまとめるプロセスです。このバンドルされたファイルを Web ページ内に置くことによって、アプリ全体を一度に読み込むことができます。 | ||||||
|
||||||
#### Example | ||||||
#### 例 | ||||||
|
||||||
**App:** | ||||||
|
||||||
|
@@ -40,42 +36,28 @@ function add(a, b) { | |||||
console.log(add(16, 26)); // 42 | ||||||
``` | ||||||
|
||||||
> Note: | ||||||
> 補足: | ||||||
> | ||||||
> Your bundles will end up looking a lot different than this. | ||||||
> バンドルされたファイルはこれらのファイルとは似てもつかないような見た目をしているでしょう。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
If you're using [Create React App](https://github.com/facebookincubator/create-react-app), [Next.js](https://github.com/zeit/next.js/), [Gatsby](https://www.gatsbyjs.org/), or a similar tool, you will have a Webpack setup out of the box to bundle your | ||||||
app. | ||||||
もしあなたが [Create React App](https://github.com/facebookincubator/create-react-app) や [Next.js](https://github.com/zeit/next.js/), [Gatsby](https://www.gatsbyjs.org/) またはこれらに類するツールを使用している場合、Webpack は自動的にそれらをバンドルしてくれます。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
If you aren't, you'll need to setup bundling yourself. For example, see the | ||||||
[Installation](https://webpack.js.org/guides/installation/) and | ||||||
[Getting Started](https://webpack.js.org/guides/getting-started/) guides on the | ||||||
Webpack docs. | ||||||
そうでない場合は、自分でバンドルを設定する必要があります。設定方法に関しては、Webpack のドキュメントにある [Installation](https://webpack.js.org/guides/installation/) や [Getting Started](https://webpack.js.org/guides/getting-started/) などを参照してみてください。 | ||||||
|
||||||
## Code Splitting | ||||||
## コード分割 | ||||||
|
||||||
Bundling is great, but as your app grows, your bundle will grow too. Especially | ||||||
if you are including large third-party libraries. You need to keep an eye on | ||||||
the code you are including in your bundle so that you don't accidentally make | ||||||
it so large that your app takes a long time to load. | ||||||
バンドルは確かに素晴らしいですが、アプリが大きくなるにつれて、バンドルのサイズも大きくなります。特にサイズの大きなサードパーティ製のライブラリを含む場合は顕著にサイズが増大します。 | ||||||
不用意に大きなバンドルを作成してしまいアプリの読み込みに多くの時間がかかってしまうという事態にならないためにも、常に注意を払い続けなければなりません。 | ||||||
|
||||||
To avoid winding up with a large bundle, it's good to get ahead of the problem | ||||||
and start "splitting" your bundle. | ||||||
[Code-Splitting](https://webpack.js.org/guides/code-splitting/) is a feature | ||||||
supported by bundlers like Webpack and Browserify (via | ||||||
[factor-bundle](https://github.com/browserify/factor-bundle)) which can create | ||||||
multiple bundles that can be dynamically loaded at runtime. | ||||||
大きなバンドルを不注意に生成してしまわないように、コードを「分割」して問題を回避しましょう。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
[Code-Splitting](https://webpack.js.org/guides/code-splitting/) は、実行時にロードする複数のバンドルを生成できる Webpack や Browserify([factor-bundle](https://github.com/browserify/factor-bundle) を使用)などのバンドルによってサポートされている機能です。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Code-splitting your app can help you "lazy-load" just the things that are | ||||||
currently needed by the user, which can dramatically improve the performance of | ||||||
your app. While you haven't reduced the overall amount of code in your app, | ||||||
you've avoided loading code that the user may never need, and reduced the amount | ||||||
of code needed during the initial load. | ||||||
コード分割することによって「遅延読み込み」が可能となり、アプリのパフォーマンスを劇的に向上させることができます。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
アプリの全体的なコード量を減らすことはできませんが、ユーザが必要としないコードを読み込まなくて済むため、初期ロードの際に読む込むコード量を削減でき、読み込みにかかる時間を短縮できます。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
## `import()` | ||||||
|
||||||
The best way to introduce code-splitting into your app is through the dynamic | ||||||
`import()` syntax. | ||||||
コード分割をアプリに導入する最も良い手段は動的インポート`import()`を使用することです。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
**Before:** | ||||||
|
||||||
|
@@ -93,31 +75,23 @@ import("./math").then(math => { | |||||
}); | ||||||
``` | ||||||
|
||||||
> Note: | ||||||
> 補足: | ||||||
> | ||||||
> The dynamic `import()` syntax is a ECMAScript (JavaScript) | ||||||
> [proposal](https://github.com/tc39/proposal-dynamic-import) not currently | ||||||
> part of the language standard. It is expected to be accepted in the | ||||||
> near future. | ||||||
> `import()` 構文はECMAScript (JavaScript) が提案している、現時点ではまだ言語標準として実装されていない構文です。近い将来に標準化される予定です。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
When Webpack comes across this syntax, it automatically starts code-splitting | ||||||
your app. If you're using Create React App, this is already configured for you | ||||||
and you can [start using it](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#code-splitting) immediately. It's also supported | ||||||
out of the box in [Next.js](https://github.com/zeit/next.js/#dynamic-import). | ||||||
Webpackがこの構文を見つけると、自動的にアプリのコードを分割します。Create React App を使用している場合はすでに設定がされているため、[すぐに使用を開始することができます。](https://github.com/facebookincubator/create-react-app/blob/master/packages/react-scripts/template/README.md#code-splitting)Next.jsも同様です。 | ||||||
smikitky marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
If you're setting up Webpack yourself, you'll probably want to read Webpack's | ||||||
[guide on code splitting](https://webpack.js.org/guides/code-splitting/). Your Webpack config should look vaguely [like this](https://gist.github.com/gaearon/ca6e803f5c604d37468b0091d9959269). | ||||||
もし Webpack を自分でセットアップしていた場合には、Webpack の[コード分割に関するガイド](https://webpack.js.org/guides/code-splitting/)を読むと良いでしょう。きっとあなたの Webpack の設定は[これくらい](https://gist.github.com/gaearon/ca6e803f5c604d37468b0091d9959269)がらんとしているでしょうから。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
When using [Babel](http://babeljs.io/), you'll need to make sure that Babel can | ||||||
parse the dynamic import syntax but is not transforming it. For that you will need [babel-plugin-syntax-dynamic-import](https://yarnpkg.com/en/package/babel-plugin-syntax-dynamic-import). | ||||||
もし [Babel](http://babeljs.io/) を使用している場合は、Babel が動的インポート構文を解析できても変換してしまわないようにする必要があります。そのためには [babel-plugin-syntax-dynamic-import](https://yarnpkg.com/en/package/babel-plugin-syntax-dynamic-import) を利用すると良いでしょう。 | ||||||
smikitky marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
## `React.lazy` | ||||||
|
||||||
> Note: | ||||||
> 補足: | ||||||
> | ||||||
> `React.lazy` and Suspense is not yet available for server-side rendering. If you want to do code-splitting in a server rendered app, we recommend [Loadable Components](https://github.com/smooth-code/loadable-components). It has a nice [guide for bundle splitting with server-side rendering](https://github.com/smooth-code/loadable-components/blob/master/packages/server/README.md). | ||||||
> `React.lazy`と Suspense はまだサーバーサイドレンダリングには使用できません。サーバーサイドでレンダリングされたアプリでコード分割をしたい場合には、[Loadable Components](https://github.com/smooth-code/loadable-components) の使用をおすすめします。こちらは[サーバーサイドレンダリングでのバンドル分割のための素晴らしいガイド](https://github.com/smooth-code/loadable-components/blob/master/packages/server/README.md)も提供していくれているので、参考にしてみてください。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
The `React.lazy` function lets you render a dynamic import as a regular component. | ||||||
`React.lazy` 関数を使用すると、動的インポートを通常のコンポーネントとしてレンダリングすることができます。 | ||||||
|
||||||
**Before:** | ||||||
|
||||||
|
@@ -147,13 +121,13 @@ function MyComponent() { | |||||
} | ||||||
``` | ||||||
|
||||||
This will automatically load the bundle containing the `OtherComponent` when this component gets rendered. | ||||||
このコンポーネントがレンダリングされる際には、`OtherComponent` を含むバンドルを自動的にロードしてくれます。 | ||||||
|
||||||
`React.lazy` takes a function that must call a dynamic `import()`. This must return a `Promise` which resolves to a module with a `default` export containing a React component. | ||||||
`React.lazy` は動的インポート構文 `import()` を呼び出す関数を引数として取ります。これは React コンポーネントを含む `default` export を持つモジュールに解決される `Promise` を返さなければなりません。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
### Suspense | ||||||
|
||||||
If the module containing the `OtherComponent` is not yet loaded by the time `MyComponent` renders, we must show some fallback content while we're waiting for it to load - such as a loading indicator. This is done using the `Suspense` component. | ||||||
`MyComponent` がレンダリングされるまでに、`OtherComponent` を含むモジュールがまだロードされていない場合、例えばロードインジケータなどのようなフォールバックコンテンツをロードが完了するまで表示する必要があります。これは `Suspense` コンポーネントを使って実装することができます。 | ||||||
|
||||||
```js | ||||||
const OtherComponent = React.lazy(() => import('./OtherComponent')); | ||||||
|
@@ -169,7 +143,7 @@ function MyComponent() { | |||||
} | ||||||
``` | ||||||
|
||||||
The `fallback` prop accepts any React elements that you want to render while waiting for the component to load. You can place the `Suspense` component anywhere above the lazy component. You can even wrap multiple lazy components with a single `Suspense` component. | ||||||
`fallback` プロパティはコンポーネントがロードされるのを待っている間に表示したいあらゆる React 要素を受け取ります。`Suspense` コンポーネントは遅延コンポーネントより上位のどこにでも配置することができます。また、複数の遅延コンポーネントを単一の`Suspense` コンポーネントでラップすることもできます。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
```js | ||||||
const OtherComponent = React.lazy(() => import('./OtherComponent')); | ||||||
|
@@ -191,7 +165,7 @@ function MyComponent() { | |||||
|
||||||
### Error boundaries | ||||||
|
||||||
If the other module fails to load (for example, due to network failure), it will trigger an error. You can handle these errors to show a nice user experience and manage recovery with [Error Boundaries](/docs/error-boundaries.html). Once you've created your Error Boundary, you can use it anywhere above your lazy components to display an error state when there's a network error. | ||||||
もし他のモジュールがロードに失敗した場合(例えば、ネットワークの障害など)、エラーが発生します。その際には [Error Boundaries](/docs/error-boundaries.html) を使用することによってこれらのエラーをハンドリングし、エラーの回復やユーザ体験の向上に繋げることができます。Error Boundary を作成したら、遅延コンポーネントより上位のあらゆる場所で使用でき、ネットワークエラーが発生した際にエラー内容を表示することができます。 | ||||||
|
||||||
```js | ||||||
import MyErrorBoundary from './MyErrorBoundary'; | ||||||
|
@@ -212,19 +186,13 @@ const MyComponent = () => ( | |||||
); | ||||||
``` | ||||||
|
||||||
## Route-based code splitting | ||||||
## ルート単位でのコード分割 | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 以降の文にてルートがトップレベルのルートと混同されるように感じたのでルーティングと明確にしたのですが如何でしょうか? (imo)
Suggested change
|
||||||
|
||||||
Deciding where in your app to introduce code splitting can be a bit tricky. You | ||||||
want to make sure you choose places that will split bundles evenly, but won't | ||||||
disrupt the user experience. | ||||||
アプリ内のどこにコード分割を導入するかを決めるのは少し面倒です。バンドルを均等に分割する場所を確実に選択したいところですが、ユーザ体験を妨げてはなりません。 | ||||||
|
||||||
A good place to start is with routes. Most people on the web are used to | ||||||
page transitions taking some amount of time to load. You also tend to be | ||||||
re-rendering the entire page at once so your users are unlikely to be | ||||||
interacting with other elements on the page at the same time. | ||||||
コード分割を導入するにあたって適している場所はルートです。Webを使用するほとんどの人は、ロード時間がかかるページ遷移に慣れています。また、ユーザはページ全体を一度に再描画する傾向にあるため、同時に他の要素を同時に見る機会はほとんどありません。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
Here's an example of how to setup route-based code splitting into your app using | ||||||
libraries like [React Router](https://reacttraining.com/react-router/) with `React.lazy`. | ||||||
これは [React Router](https://reacttraining.com/react-router/) のようなライブラリを `React.lazy` を使用することでアプリにルートベースのコード分割を導入する方法の例です。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
```js | ||||||
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; | ||||||
|
@@ -245,9 +213,9 @@ const App = () => ( | |||||
); | ||||||
``` | ||||||
|
||||||
## Named Exports | ||||||
## 名前付きエクスポート | ||||||
|
||||||
`React.lazy` currently only supports default exports. If the module you want to import uses named exports, you can create an intermediate module that reexports it as the default. This ensures that treeshaking keeps working and that you don't pull in unused components. | ||||||
`React.lazy` は現在デフォルトエクスポートのみサポートしています。インポートしたいモジュールが名前付きエクスポートを使用している場合、それをデフォルトとして再エクスポートする中間モジュールを作成できます。これにより、treeshaking が機能し未使用のコンポーネントを取り込まず済むようにできます。 | ||||||
sugoikondo marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||
|
||||||
```js | ||||||
// ManyComponents.js | ||||||
|
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.