diff --git a/content/docs/code-splitting.md b/content/docs/code-splitting.md index ff0cc84df..ccb9965e1 100644 --- a/content/docs/code-splitting.md +++ b/content/docs/code-splitting.md @@ -1,20 +1,19 @@ --- id: code-splitting -title: Code-Splitting +title: Dzielenie kodu permalink: docs/code-splitting.html --- -## Bundling {#bundling} +## Pakowanie {#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. +Większość reactowych aplikacji będzie "dołączała" swoje pliki poprzez narzędzia takie jak +[Webpack](https://webpack.js.org/) czy [Browserify](http://browserify.org/). +Pakowanie to proces śledzenia zaimportowanych plików i łączenia ich w pojedynczy plik tzw. "bundle" (pol. *paczka*). +Taka paczka może zostać umieszczona na stronie w celu załadowania całej aplikacji naraz. -#### Example {#example} +#### Przykład {#example} -**App:** +**Kod aplikacji:** ```js // app.js @@ -30,7 +29,7 @@ export function add(a, b) { } ``` -**Bundle:** +**Paczka:** ```js function add(a, b) { @@ -40,44 +39,45 @@ function add(a, b) { console.log(add(16, 26)); // 42 ``` -> Note: +> Uwaga: > -> Your bundles will end up looking a lot different than this. +> Twoje paczki prawdopodobnie będą się znacząco różnić. -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. +Jeśli używasz [Create React App](https://github.com/facebookincubator/create-react-app), +[Next.js](https://github.com/zeit/next.js/), [Gatsby](https://www.gatsbyjs.org/) +lub innego podobnego narzędzia, Webpack powinien być już skonfigurowany i gotowy do użytku. -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. +Jeśli nie, musisz skonfigurować Webpacka samodzielnie. +Przykłady znajdziesz w +[przewodniku po instalacji](https://webpack.js.org/guides/installation/) oraz +w sekcji pt. ["Podstawowe informacje"](https://webpack.js.org/guides/getting-started/) zawartych w dokumentacji +Webpacka. -## Code Splitting {#code-splitting} +## Dzielenie kodu {#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. +Tworzenie paczek jest świetne, ale wraz ze wzrostem objętości kodu twojej aplikacji, rośnie również objętość paczek. +Zwłaszcza gdy dołączasz do projektu duże, zewnętrzne biblioteki. +Musisz pilnować, aby twój pakiet nie był zbyt duży, ponieważ wtedy aplikacja +będzie się długo ładowała. -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. +Aby uniknąć problemu zbyt dużego pakietu, warto już na początku o tym pomyśleć +i rozpocząć "dzielenie" swojej paczki. + [Dzielenie kodu](https://webpack.js.org/guides/code-splitting/) to funkcja +wspierana przez narzędzia takie jak Webpack oraz Browserify (przez +[factor-bundle](https://github.com/browserify/factor-bundle)), które mogą tworzyć +wiele pakietów doładowywanych dynamicznie w czasie wykonania kodu aplikacji. -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. +Dzielenie kodu twojej aplikacji ułatwi ci użycie "leniwego ładowania" do wczytywania jedynie tych zasobów które są aktualnie wymagane przez +użytkownika zasobów, co może znacznie poprawić wydajność twojej aplikacji. +Mimo że nie zmniejszysz w ten sposób sumarycznej ilości kodu, unikniesz ładowania +funkcjonalności zbędnych dla użytkownika w danym momencie, co przełoży się na mniejszą ilość kodu do pobrania na starcie aplikacji. ## `import()` {#import} -The best way to introduce code-splitting into your app is through the dynamic -`import()` syntax. +Najlepszym sposobem na wprowadzenie podziału kodu do twojej aplikacji jest użycie dynamicznego wariantu funkcji +`import()`. -**Before:** +**Przed:** ```js import { add } from './math'; @@ -85,7 +85,7 @@ import { add } from './math'; console.log(add(16, 26)); ``` -**After:** +**Po:** ```js import("./math").then(math => { @@ -93,33 +93,36 @@ import("./math").then(math => { }); ``` -> Note: +> Uwaga: > -> 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. +> Dynamiczny `import()` to [propozycja](https://github.com/tc39/proposal-dynamic-import) +> dla ECMAScript (JavaScript), która aktualnie nie jest częścią standardu językowego. Oczekuje się jednak, +> że wkrótce zostanie zaakceptowana. -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). +Gdy Webpack natknie się na taką składnię, automatycznie zacznie dzielić kod w twojej aplikacji. +Jeśli używasz Create React App, posiadasz już gotową konfigurację i możesz natychmiast +[zacząć z niej korzystać](https://facebook.github.io/create-react-app/docs/code-splitting). +Jest ona również obsługiwana domyślnie przez [Next.js](https://github.com/zeit/next.js/#dynamic-import). -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). +Jeśli konfigurujesz Webpacka samodzielnie, zalecamy przeczytać +[przewodnik po dzieleniu kodu](https://webpack.js.org/guides/code-splitting/). +Twoja konfiguracja Webpacka powinna wyglądać podobnie [do tej](https://gist.github.com/gaearon/ca6e803f5c604d37468b0091d9959269). -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). +Kiedy używasz [Babela](https://babeljs.io/), musisz się upewnić, że jest on w stanie sparsować +składnię dynamicznego importu, ale jej nie przekształca w żaden sposób. W tym celu skorzystaj z pluginu [babel-plugin-syntax-dynamic-import](https://yarnpkg.com/en/package/babel-plugin-syntax-dynamic-import). ## `React.lazy` {#reactlazy} -> Note: +> Uwaga: > -> `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` i Suspense są jest jeszcze dostępne dla renderowania po stronie serwera. +> Jeśli chcesz dzielić kod dla aplikacji renderowanej na serwerze, sugerujemy skorzystać +> z pakietu [Loadable Components](https://github.com/smooth-code/loadable-components). +> Ma on przystępną [instrukcję dzielenia pakietów przy renderowaniu po stronie serwera](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. +Funkcja `React.lazy` pozwala renderować dynamicznie importowane komponenty jak zwykłe komponenty. -**Before:** +**Przed:** ```js import OtherComponent from './OtherComponent'; @@ -133,7 +136,7 @@ function MyComponent() { } ``` -**After:** +**Po:** ```js const OtherComponent = React.lazy(() => import('./OtherComponent')); @@ -147,13 +150,17 @@ function MyComponent() { } ``` -This will automatically load the bundle containing the `OtherComponent` when this component gets rendered. +Powyższy kod automatycznie załaduje paczkę zawierającą `OtherComponent` podczas renderowania komponentu. -`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` jako argument przyjmuje funkcję, która wywołuje dynamiczny `import()`. +Musi ona zwrócić obiekt (`Promise`) (pol. *obietnicę*), która rozwiązuje się do modułu z eksportem domyślnym (`default`) zawierającym +komponent reactowy. -### Suspense {#suspense} +### Zawieszenie (ang. *Suspense*) {#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. +Jeśli moduł zawierający `OtherComponent` nie zostanie załadowany przed renderowaniem komponentu +`MyComponent`, musimy wyświetlić alternatywną zawartość, dopóki trwa ładowanie - na przykład +wskaźnik ładowania. Robimy to za pomocą komponentu `Suspense`. ```js const OtherComponent = React.lazy(() => import('./OtherComponent')); @@ -161,7 +168,7 @@ const OtherComponent = React.lazy(() => import('./OtherComponent')); function MyComponent() { return (