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

renameChunkLoader #4

Merged
merged 1 commit into from
Feb 24, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 10 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ _Code splitting with minimal boiler plate_

_This is a fork of [react-loadable](https://github.com/jamiebuilds/react-loadable), differences and new features include:_
* _A modified API to support new features_
* _Full render control using a HOC wrapped component_
* _Improved re-use of import components_
* _Improved support for route code splitting_
* _Preloading all chunks required to render an entire route_
* _Pre-loading all chunks required to render an entire route_
* _Option to _hoist_ static methods of imported components_
* _Option to enable retry support with backoff_
* _Manually invoking a retry after timeout or error_
Expand Down Expand Up @@ -195,6 +196,8 @@ best for your app.

Its often useful to assign _names_ to webpack chunks. This can be achieved easily using inline code comments.

Be aware that naming chunks **impacts how webpack bundles your code**. You should [read about webpack code splitting](https://webpack.js.org/guides/code-splitting/#dynamic-imports).

```js
import { chunk, chunks } from 'react-chunk';

Expand Down Expand Up @@ -402,8 +405,8 @@ const FooChunk = chunk(() => import('./Foo'))();
const BarChunk = chunk(() => import('./Bar'))();

preloadChunks([
FooChunk.getLoader(),
BarChunk.getLoader(),
FooChunk.getChunkLoader(),
BarChunk.getChunkLoader(),
]).then(() => {
// use 'setState()' to render using the loaded components
}).catch(err => {
Expand Down Expand Up @@ -741,14 +744,14 @@ resolve to update your UI. In most cases it creates a bad user experience.

[Read more about preloading](#preloading).

#### `getLoader()`
#### `getChunkLoader()`

This is a static method that can be used to obtain a reference to the components loader. It should be used in conjuntion with [preloadChunks()](#preloadchunks)

```js
const ChunkComponent = chunk({...});

ChunkComponent.getLoader();
ChunkComponent.getChunkLoader();
```


Expand Down Expand Up @@ -1152,10 +1155,11 @@ res.send(`
</head>
<body>
<div id="app">${html}</div>
<script src="/dist/main.js"></script>
<script src="/dist/manifest.js"></script>
${scripts.map(script => {
return `<script src="/dist/${script.file}"></script>`
}).join('\n')}
<script src="/dist/main.js"></script>
</body>
</html>
`);
Expand Down
6 changes: 3 additions & 3 deletions __tests__/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -359,7 +359,7 @@ describe('chunk', () => {

expect.assertions(1);
try {
await preloadChunks([ChunkMyComponent.getLoader()]);
await preloadChunks([ChunkMyComponent.getChunkLoader()]);
} catch (e) {
expect(e.message).toEqual('import err');
}
Expand All @@ -383,7 +383,7 @@ describe('chunk', () => {
MyComponent.myStatic = jestFn;
let ChunkMyComponent = chunk(createLoader(400, () => MyComponent), { hoist: true })();

await preloadChunks([ChunkMyComponent.getLoader()]);
await preloadChunks([ChunkMyComponent.getChunkLoader()]);

expect(ChunkMyComponent.myStatic).toBe(jestFn);
delete MyComponent.myStatic;
Expand Down Expand Up @@ -550,7 +550,7 @@ test('preloadChunks', async () => {
let LoadableMyComponent = chunk(createLoader(300, () => MyComponent))(SingleImport);
let LoadableMapComponent = chunks({ MyComponent: createLoader(300, () => MyComponent) })(MapImport);

const loaders = [LoadableMyComponent.getLoader(), LoadableMapComponent.getLoader()];
const loaders = [LoadableMyComponent.getChunkLoader(), LoadableMapComponent.getChunkLoader()];

await preloadChunks(loaders);

Expand Down
6 changes: 3 additions & 3 deletions example/components/PreLoadButton.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@ export default class PreLoadButton extends React.Component {

// Verify webpack only submits a single request
preloadChunks([
ContentChunk.getLoader(),
ContentChunk.getLoader(),
ContentChunk.getLoader()
ContentChunk.getChunkLoader(),
ContentChunk.getChunkLoader(),
ContentChunk.getChunkLoader()
]).then(() => {
console.log('pre-loading modules');
this.setState({ isLoaded: true });
Expand Down
2 changes: 1 addition & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ function createChunkComponent(loadFn, options) {
return init(true);
}

static getLoader() {
static getChunkLoader() {
return init;
}

Expand Down