Skip to content
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" into Russian #246

Merged
merged 40 commits into from Mar 14, 2019
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
40 commits
Select commit Hold shift + click to select a range
34af8f7
Translate "Code-Splitting" into Russian
avevlad Mar 11, 2019
5285951
Typo fix "Code-Splitting" #246
avevlad Mar 12, 2019
505785e
Typo fix е -> ё "Code-Splitting" #246
avevlad Mar 12, 2019
81be3a0
Update content/docs/code-splitting.md
rlax Mar 13, 2019
ec9da5e
Update content/docs/code-splitting.md
rlax Mar 13, 2019
4675e42
Update content/docs/code-splitting.md
gcor Mar 13, 2019
d049c8a
Update content/docs/code-splitting.md
gcor Mar 13, 2019
9ae6895
Update content/docs/code-splitting.md
gcor Mar 13, 2019
cc3ee66
Update content/docs/code-splitting.md
gcor Mar 13, 2019
94f1219
Update content/docs/code-splitting.md
gcor Mar 13, 2019
6b50548
Update content/docs/code-splitting.md
gcor Mar 13, 2019
2627a7a
Update content/docs/code-splitting.md
gcor Mar 13, 2019
16c8d4a
Update content/docs/code-splitting.md
gcor Mar 13, 2019
af87cac
Update content/docs/code-splitting.md
gcor Mar 13, 2019
fe9a5dd
Update content/docs/code-splitting.md
gcor Mar 13, 2019
e9bbbbe
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
72067d9
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
3b07732
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
8967e86
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
060ba89
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
ffdd931
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
cd88754
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
c631fbc
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
3745a6d
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
5c4b54e
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
2035406
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
f59e2db
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
30d6888
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
2fef9b1
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
bec28ed
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
79b556f
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
64ce5f0
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
42dd234
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
29b83b7
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
dc82ecb
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
9a2217b
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
50dc272
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
6f48dc8
Update content/docs/code-splitting.md
another-guy Mar 14, 2019
cfbd4f3
формулировка "Бандлинг"
avevlad Mar 14, 2019
e280402
Задержка, ожидание загрузки
avevlad Mar 14, 2019
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
141 changes: 66 additions & 75 deletions content/docs/code-splitting.md
@@ -1,20 +1,19 @@
---
id: code-splitting
title: Code-Splitting
title: Разделение кода
permalink: docs/code-splitting.html
---

## Bundling {#bundling}
## Бандлинг модулей {#bundling}
avevlad marked this conversation as resolved.
Show resolved Hide resolved

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 приложений собираются инструментами типа
avevlad marked this conversation as resolved.
Show resolved Hide resolved
[Webpack](https://webpack.js.org/) или [Browserify](http://browserify.org/) и состоят из нескольких "бандл" файлов.
avevlad marked this conversation as resolved.
Show resolved Hide resolved
Бандлинг -- это процесс группировки файлов с модулями и их зависимостями в один "бандл" файл.
avevlad marked this conversation as resolved.
Show resolved Hide resolved
Этот бандл после подключения на веб странице загружает всё приложение за один раз.
avevlad marked this conversation as resolved.
Show resolved Hide resolved

#### Example {#example}
#### Пример {#example}

**App:**
**Приложение:**

```js
// app.js
Expand All @@ -30,7 +29,7 @@ export function add(a, b) {
}
```

**Bundle:**
**Бандл:**

```js
function add(a, b) {
Expand All @@ -40,86 +39,82 @@ function add(a, b) {
console.log(add(16, 26)); // 42
```

> Note:
> Примечание:
>
> Your bundles will end up looking a lot different than this.
> Ваши бандлы будут выглядеть иначе, чем это.
avevlad 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 там настроен из коробки.
avevlad 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 самостоятельно.
Для этого ознакомьтесь со страницами
[Установка](https://webpack.js.org/guides/installation/) и
[Начала работы](https://webpack.js.org/guides/getting-started/) в документации по Webpack.
avevlad marked this conversation as resolved.
Show resolved Hide resolved

## Code Splitting {#code-splitting}
## Разделение кода {#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.
Бандлинг это отлично, но по мере того как ваше приложение растёт, ваш бандл тоже растёт.
avevlad marked this conversation as resolved.
Show resolved Hide resolved
Особенно если вы подключаете крупные сторонние библиотеки.
Вам нужно следить за тем что вы подключаете, чтобы случайно не сделать приложение настолько большим,
avevlad marked this conversation as resolved.
Show resolved Hide resolved
что его загрузка займёт слишком много времени.

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.
Чтобы предотвратить разрастание бандла, хорошо начать “разделять” ваш бандл.
avevlad marked this conversation as resolved.
Show resolved Hide resolved
[Разделение кода](https://webpack.js.org/guides/code-splitting/) это возможность которая поддерживается такими бандлерами
avevlad marked this conversation as resolved.
Show resolved Hide resolved
как Webpack или Browserify (с [factor-bundle](https://github.com/browserify/factor-bundle)), она может разделить
avevlad marked this conversation as resolved.
Show resolved Hide resolved
ваш бандл на несколько кусочков и загружать их по мере необходимости.
avevlad 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.
Хоть вы и не уменьшите общий объём кода вашего приложения, но вы избежите загрузки кода, который
avevlad marked this conversation as resolved.
Show resolved Hide resolved
может никогда не понадобиться пользователю и уменьшите объём кода необходимый для начальной загрузки.
avevlad marked this conversation as resolved.
Show resolved Hide resolved

## `import()` {#import}

The best way to introduce code-splitting into your app is through the dynamic
`import()` syntax.
Лучший способ внедрить разделение кода в приложение -- использовать синтаксис динамического импорта: `import()`.

**Before:**
**До:**

```js
import { add } from './math';

console.log(add(16, 26));
```

**After:**
**После:**

```js
import("./math").then(math => {
console.log(math.add(16, 26));
});
```

> 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)
> [предложение](https://github.com/tc39/proposal-dynamic-import),
> которое в данный момент не входит в стандарт языка. Ожидается, что он будет принят в ближайшем будущем.

Когда Webpack сталкивается с таким синтаксисом, он автоматически запускает разделение кода вашего приложения.
avevlad marked this conversation as resolved.
Show resolved Hide resolved
Если вы используете Create React App, то он уже настроен,
avevlad marked this conversation as resolved.
Show resolved Hide resolved
вы можете [начать использовать](https://facebook.github.io/create-react-app/docs/code-splitting) этот синтаксис прямо сейчас.
avevlad marked this conversation as resolved.
Show resolved Hide resolved
Он также поддерживается из коробки в [Next.js](https://github.com/zeit/next.js/#dynamic-import).
avevlad 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://facebook.github.io/create-react-app/docs/code-splitting) immediately. It's also supported
out of the box in [Next.js](https://github.com/zeit/next.js/#dynamic-import).
Если вы настраиваете Webpack самостоятельно, то вероятно, вы захотите прочитать [руководство Webpack по разделению кода](https://webpack.js.org/guides/code-splitting/).
avevlad marked this conversation as resolved.
Show resolved Hide resolved
Ваш Webpack конфиг должен выглядеть [примерно так](https://gist.github.com/gaearon/ca6e803f5c604d37468b0091d9959269).
avevlad 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).
Если вы используете [Babel](https://babeljs.io/), вам необходимо убедиться, что он понимает синтаксис динамического импорта.
Для этого вам необходимо установить плагин [@babel/plugin-syntax-dynamic-import](https://babeljs.io/docs/en/babel-plugin-syntax-dynamic-import).

When using [Babel](https://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).

## `React.lazy` {#reactlazy}

> 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 пока недоступны для рендеринга на стороне сервера.
avevlad marked this conversation as resolved.
Show resolved Hide resolved
> Если вам нужно разделение кода в серверном приложении, мы рекомендуем [Loadable Components](https://github.com/smooth-code/loadable-components).
> У них есть хорошее [руководство по разделению бандла](https://github.com/smooth-code/loadable-components/blob/master/packages/server/README.md) с серверным рендерингом.


The `React.lazy` function lets you render a dynamic import as a regular component.
Функция `React.lazy` позволяет рендерить динамический импорт как обычный компонент.

**Before:**
**До:**

```js
import OtherComponent from './OtherComponent';
Expand All @@ -133,7 +128,7 @@ function MyComponent() {
}
```

**After:**
**После:**

```js
const OtherComponent = React.lazy(() => import('./OtherComponent'));
Expand All @@ -147,13 +142,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()`.
avevlad marked this conversation as resolved.
Show resolved Hide resolved

### Suspense {#suspense}
avevlad marked this conversation as resolved.
Show resolved Hide resolved

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.
Если модуль, содержащий `OtherComponent`, ещё не загружен к моменту рендеринга `MyComponent`, мы должны показать запасное содержимое, пока ожидаем загрузки, например индикатор загрузки. Это можно сделать с помощью компонента `Suspense`.
avevlad marked this conversation as resolved.
Show resolved Hide resolved

```js
const OtherComponent = React.lazy(() => import('./OtherComponent'));
Expand All @@ -169,7 +164,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`.
avevlad marked this conversation as resolved.
Show resolved Hide resolved

```js
const OtherComponent = React.lazy(() => import('./OtherComponent'));
Expand All @@ -189,9 +184,10 @@ function MyComponent() {
}
```

### Error boundaries {#error-boundaries}
### Предохранители (компоненты Error Boundary) {#error-boundaries}
avevlad marked this conversation as resolved.
Show resolved Hide resolved

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.

Если какой-то модуль не загружается (например, из-за сбоя сети), это вызовет ошибку. Вы можете обрабатывать эти ошибки ради хорошего пользовательского опыта с помощью [Предохранителей](/docs/error-boundaries.html). Создав предохранитель, вы можете использовать его в любом месте над ленивыми компонентами для отображения состояния ошибки.
avevlad marked this conversation as resolved.
Show resolved Hide resolved

```js
import MyErrorBoundary from './MyErrorBoundary';
Expand All @@ -212,19 +208,14 @@ const MyComponent = () => (
);
```

## Route-based code splitting {#route-based-code-splitting}
## Разделение кода на основе маршрутов {#route-based-code-splitting}

Решение о том, где в вашем приложении ввести разделение кода, может быть немного сложным.
avevlad marked this conversation as resolved.
Show resolved Hide resolved

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.
Хороший вариант чтобы начать это -- маршруты. Большинство людей в интернете привыкли к переходам страниц, которые занимают некоторое время. Вы также склонны повторно рендерить всю страницу сразу, поэтому ваши пользователи вряд ли будут взаимодействовать с другими элементами на странице одновременно.
avevlad marked this conversation as resolved.
Show resolved Hide resolved

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.
Вот пример того, как организовать разделение кода на основе маршрутов в приложении с помощью библиотек, таких как [React Router](https://reacttraining.com/react-router/) с `React.lazy`.
avevlad 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`.

```js
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
Expand All @@ -245,9 +236,9 @@ const App = () => (
);
```

## Named Exports {#named-exports}
## Именованный Экспорт {#named-exports}
avevlad marked this conversation as resolved.
Show resolved Hide resolved

`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 будет работать.
avevlad marked this conversation as resolved.
Show resolved Hide resolved

```js
// ManyComponents.js
Expand Down
2 changes: 1 addition & 1 deletion content/docs/nav.yml
Expand Up @@ -40,7 +40,7 @@
- id: accessibility
title: Accessibility
- id: code-splitting
title: Code-Splitting
title: Разделение кода
- id: context
title: Контекст
- id: error-boundaries
Expand Down