diff --git a/.eslintignore b/.eslintignore index e7322c4..d6a1444 100644 --- a/.eslintignore +++ b/.eslintignore @@ -3,3 +3,5 @@ node_modules/ dist/ lib/ build/ +/website/.cache/ +/website/public/ \ No newline at end of file diff --git a/.github/ISSUE_TEMPLATE/feature.md b/.github/ISSUE_TEMPLATE/feature.md index 0100c2e..d216fbe 100644 --- a/.github/ISSUE_TEMPLATE/feature.md +++ b/.github/ISSUE_TEMPLATE/feature.md @@ -17,4 +17,4 @@ Please provide an example for how this feature would be used. ## Pitch -Why does this feature belong in the SVGR ecosystem? +Why does this feature belong in the Loadable Component ecosystem? diff --git a/.prettierignore b/.prettierignore index 267b176..5255f59 100644 --- a/.prettierignore +++ b/.prettierignore @@ -4,4 +4,6 @@ node_modules/ __fixtures__/ CHANGELOG.md package.json -lerna.json \ No newline at end of file +lerna.json +/website/.cache/ +/website/public/ \ No newline at end of file diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2398b97..793bae4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -8,7 +8,7 @@ We expect project participants to adhere to our Code of Conduct. Please read [th ## Open Development -All work on SVGR happens directly on [GitHub](/). Both core team members and external contributors send pull requests which go through the same review process. +All work on Loadable Components happens directly on [GitHub](/). Both core team members and external contributors send pull requests which go through the same review process. ### Workflow and Pull Requests diff --git a/README.md b/README.md index e556403..78306bb 100644 --- a/README.md +++ b/README.md @@ -12,32 +12,21 @@ [![DevDependencies](https://img.shields.io/david/dev/smooth-code/loadable-components.svg)](https://david-dm.org/smooth-code/loadable-components?type=dev) [![Small size](https://img.badgesize.io/https://unpkg.com/@loadable/component/dist/loadable.min.js?compression=gzip)](https://unpkg.com/@loadable/component/dist/loadable.min.js) -```sh +```bash npm install @loadable/component ``` -## Introduction +## [Docs](https://www.smooth-code.com/open-source/loadable-components) -Code splitting is supported out of the box by React using [`React.lazy`](https://reactjs.org/docs/code-splitting.html#reactlazy). So what is the goal of this library? +**See the documentation at [smooth-code.com/open-source/loadable-components](https://www.smooth-code.com/open-source/loadable-components)** for more information about using `loadable components`! -`@loadable/component` pushes the limit of Code splitting, it offers several features: +Quicklinks to some of the most-visited pages: -- 📚 Library splitting -- ⚡️ Prefetching -- 💫 Server Side Rendering -- 🎛 Full dynamic import `` import(`./${value}`) `` +- [**Getting started**](https://www.smooth-code.com/open-source/loadable-components/docs/getting-started/) +- [Comparison with React.lazy](https://www.smooth-code.com/open-source/loadable-components/docs/loadable-vs-react-lazy/) +- [Server Side Rendering](https://www.smooth-code.com/open-source/loadable-components/docs/server-side-rendering/) -## Comparison with React.lazy & react-loadable - -| Library | Suspense | SSR | Library splitting | `` import(`./${value}`) `` | -| --------------------- | -------- | --- | ----------------- | -------------------------- | -| `React.lazy` | ✅ | ❌ | ❌ | ❌ | -| `react-loadable` | ❌ | 🔶 | ❌ | ❌ | -| `@loadable/component` | ✅ | ✅ | ✅ | ✅ | - -## Getting started - -`loadable` lets you render a dynamic import as a regular component. +## Example ```js import loadable from '@loadable/component' @@ -53,280 +42,8 @@ function MyComponent() { } ``` -### Loading library +## Licence -`loadable.lib` lets you defer the loading of a library. It takes a render props called when the library is loaded. +Licensed under the MIT License, Copyright © 2017-present Smooth Code. -```js -import loadable from '@loadable/component' - -const Moment = loadable.lib(() => import('moment')) - -function FromNow({ date }) { - return ( -
- - {({ default: moment }) => moment(date).fromNow()} - -
- ) -} -``` - -You can also use a `ref`, populated when the library is loaded. - -```js -import loadable from '@loadable/component' - -const Moment = loadable.lib(() => import('moment')) - -class MyComponent { - moment = React.createRef() - - handleClick = () => { - if (this.moment.current) { - return alert(this.moment.current.default.format('HH:mm')) - } - } - - render() { - return ( -
- - -
- ) - } -} -``` - -> You can also pass a function to `ref`, called when the library is loaded. - -### Full dynamic import - -Webpack accepts [full dynamic imports](https://webpack.js.org/api/module-methods/#import-), you can use them to create a reusable Loadable Component. - -```js -import loadable from '@loadable/component' - -const AsyncPage = loadable(props => import(`./${props.page}`)) - -function MyComponent() { - return ( -
- - -
- ) -} -``` - -### Suspense - -`@loadable/component` exposes a `lazy` method that acts similarly as `React.lazy` one. - -```js -import { lazy } from '@loadable/component' - -const OtherComponent = lazy(() => import('./OtherComponent')) - -function MyComponent() { - return ( -
- Loading...
}> - - - - ) -} -``` - -> Use `lazy.lib` for libraries. - -> ⚠️ Suspense is not yet available for server-side rendering. - -### Fallback without Suspense - -You can specify a `fallback` in `loadable` options. - -```js -const OtherComponent = loadable(() => import('./OtherComponent'), { - fallback:
Loading...
, -}) - -function MyComponent() { - return ( -
- -
- ) -} -``` - -You can also specify a `fallback` in props: - -```js -const OtherComponent = loadable(() => import('./OtherComponent')) - -function MyComponent() { - return ( -
- Loading...
} /> - - ) -} -``` - -### 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](https://reactjs.org/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. - -```js -import MyErrorBoundary from '/MyErrorBoundary' -const OtherComponent = loadable(() => import('./OtherComponent')) -const AnotherComponent = loadable(() => import('./AnotherComponent')) - -const MyComponent = () => ( -
- -
- - -
-
-
-) -``` - -### Delay - -To avoid flashing a loader if the loading is very fast, you could implement a minimum delay. There is no built-in API in `@loadable/component` but you could do it using [`p-min-delay`](https://github.com/sindresorhus/p-min-delay). - -```js -import loadable from '@loadable/component' -import pMinDelay from 'p-min-delay' - -// Wait a minimum of 200ms before loading home. -export const OtherComponent = loadable(() => - pMinDelay(import('./OtherComponent'), 200), -) -``` - -### Timeout - -Infinite loading is not good for user experience, to avoid it implementing a timeout is a good workaround. You can do it using a third party module like [`promise-timeout`](https://github.com/building5/promise-timeout): - -```js -import loadable from '@loadable/component' -import { timeout } from 'promise-timeout' - -// Wait a maximum of 2s before sending an error. -export const OtherComponent = loadable(() => - timeout(import('./OtherComponent'), 2000), -) -``` - -### Prefetching - -Loadable Components is fully compatible with [webpack hints `webpackPrefetch` and `webpackPreload`](https://webpack.js.org/guides/code-splitting/#prefetching-preloading-modules). - -Most of the time, you want to "prefetch" a component, it means it will be loaded when the browser is idle. You can do it by adding `/* webpackPrefetch: true */` inside your import statement. - -```js -import loadable from '@loadable/component' - -const OtherComponent = loadable(() => - import(/* webpackPrefetch: true */ './OtherComponent'), -) -``` - -> You can extract prefetched resources server-side to add `` in your head. - -## [Server side rendering](https://github.com/smooth-code/loadable-components/tree/master/packages/server/README.md) - -👉 [See `@loadable/server` documentation](https://github.com/smooth-code/loadable-components/tree/master/packages/server/README.md). - - -## API - -### loadable - -Create a loadable component. - -| Arguments | Description | -| ------------------ | ---------------------------------------- | -| `loadFn` | The function call to load the component. | -| `options` | Optional options. | -| `options.fallback` | Fallback displayed during the loading. | - -```js -import loadable from '@loadable/component' - -const OtherComponent = loadable(() => import('./OtherComponent')) -``` - -### lazy - -Create a loadable component "Suspense" ready. - -| Arguments | Description | -| --------- | ---------------------------------------- | -| `loadFn` | The function call to load the component. | - -```js -import { lazy } from '@loadable/component' - -const OtherComponent = lazy(() => import('./OtherComponent')) -``` - -### LoadableComponent - -A component created using `loadable` or `lazy`. - -| Props | Description | -| ---------- | ------------------------------------------------- | -| `fallback` | Fallback displayed during the loading. | -| `...` | Props are forwarded as first argument of `loadFn` | - -### loadable.lib - -Create a loadable library. - -| Arguments | Description | -| ------------------ | ---------------------------------------- | -| `loadFn` | The function call to load the component. | -| `options` | Optional options. | -| `options.fallback` | Fallback displayed during the loading. | - -```js -import loadable from '@loadable/component' - -const Moment = loadable.lib(() => import('moment')) -``` - -### lazy.lib - -Create a loadable library "Suspense" ready. - -| Arguments | Description | -| --------- | ---------------------------------------- | -| `loadFn` | The function call to load the component. | - -```js -import { lazy } from '@loadable/component' - -const Moment = lazy.lib(() => import('moment')) -``` - -### LoadableLibrary - -A component created using `loadable.lib` or `lazy.lib`. - -| Props | Description | -| ---------- | ---------------------------------------------------- | -| `children` | Function called when the library is loaded. | -| `ref` | Accepts a ref, populated when the library is loaded. | -| `fallback` | Fallback displayed during the loading. | -| `...` | Props are forwarded as first argument of `loadFn` | - -## MIT +See [LICENSE](./LICENSE) for more information. diff --git a/examples/razzle/razzle.config.js b/examples/razzle/razzle.config.js index 63f5f24..d90b123 100644 --- a/examples/razzle/razzle.config.js +++ b/examples/razzle/razzle.config.js @@ -1,27 +1,27 @@ -const path = require('path'); -const LoadableWebpackPlugin = require('@loadable/webpack-plugin'); -const LoadableBabelPlugin = require('@loadable/babel-plugin'); -const babelPresetRazzle = require('razzle/babel'); +const path = require('path') +const LoadableWebpackPlugin = require('@loadable/webpack-plugin') +const LoadableBabelPlugin = require('@loadable/babel-plugin') +const babelPresetRazzle = require('razzle/babel') module.exports = { - modify: (config, { target }) => { - const appConfig = Object.assign({}, config); + modify: (config, { target }) => { + const appConfig = Object.assign({}, config) - if (target === 'web') { - const filename = path.resolve(__dirname, 'build/loadable-stats.json'); + if (target === 'web') { + const filename = path.resolve(__dirname, 'build/loadable-stats.json') - appConfig.plugins = [ - ...appConfig.plugins, - new LoadableWebpackPlugin({ writeToDisk: true, filename }) - ]; - } + appConfig.plugins = [ + ...appConfig.plugins, + new LoadableWebpackPlugin({ writeToDisk: true, filename }), + ] + } - return appConfig; - }, + return appConfig + }, - modifyBabelOptions: () => ({ - babelrc: false, - presets: [babelPresetRazzle], - plugins: [LoadableBabelPlugin] - }) -}; + modifyBabelOptions: () => ({ + babelrc: false, + presets: [babelPresetRazzle], + plugins: [LoadableBabelPlugin], + }), +} diff --git a/examples/razzle/src/About/index.js b/examples/razzle/src/About/index.js index 8ea2507..9a6c8bf 100644 --- a/examples/razzle/src/About/index.js +++ b/examples/razzle/src/About/index.js @@ -1,5 +1,5 @@ -import React from 'react'; +import React from 'react' -const About = () => (
About page
); +const About = () =>
About page
-export default About; \ No newline at end of file +export default About diff --git a/examples/razzle/src/App.js b/examples/razzle/src/App.js index e7bcfbb..bf1055d 100644 --- a/examples/razzle/src/App.js +++ b/examples/razzle/src/App.js @@ -1,12 +1,12 @@ -import React from 'react'; -import { Router, Link } from '@reach/router'; -import loadable from '@loadable/component'; -import './App.css'; +import React from 'react' +import { Router, Link } from '@reach/router' +import loadable from '@loadable/component' +import './App.css' -const NotFound = loadable(() => import('./NotFound')); -const Home = loadable(() => import('./Home')); -const About = loadable(() => import('./About')); -const Contact = loadable(() => import('./Contact')); +const NotFound = loadable(() => import('./NotFound')) +const Home = loadable(() => import('./Home')) +const About = loadable(() => import('./About')) +const Contact = loadable(() => import('./Contact')) const App = () => ( @@ -20,6 +20,6 @@ const App = () => ( loading...} /> -); +) -export default App; +export default App diff --git a/examples/razzle/src/Contact/index.js b/examples/razzle/src/Contact/index.js index f974cc9..8941afb 100644 --- a/examples/razzle/src/Contact/index.js +++ b/examples/razzle/src/Contact/index.js @@ -1,5 +1,5 @@ -import React from 'react'; +import React from 'react' -const Contact = () => (
Contact page
); +const Contact = () =>
Contact page
-export default Contact; \ No newline at end of file +export default Contact diff --git a/examples/razzle/src/Home/Intro.js b/examples/razzle/src/Home/Intro.js index 47705b0..0ddfba1 100644 --- a/examples/razzle/src/Home/Intro.js +++ b/examples/razzle/src/Home/Intro.js @@ -1,11 +1,11 @@ /* eslint-disable react/jsx-one-expression-per-line */ -import React from 'react'; +import React from 'react' const Intro = () => (

To get started, edit src/App.js or src/Home.js and save to reload.

-); +) -export default Intro; +export default Intro diff --git a/examples/razzle/src/Home/Logo.js b/examples/razzle/src/Home/Logo.js index 84d99bf..96de7c7 100644 --- a/examples/razzle/src/Home/Logo.js +++ b/examples/razzle/src/Home/Logo.js @@ -1,6 +1,6 @@ -import React from 'react'; -import logo from './react.svg'; +import React from 'react' +import logo from './react.svg' -const Logo = () => logo; +const Logo = () => logo -export default Logo; +export default Logo diff --git a/examples/razzle/src/Home/Welcome.js b/examples/razzle/src/Home/Welcome.js index 195b0fd..958bc0c 100644 --- a/examples/razzle/src/Home/Welcome.js +++ b/examples/razzle/src/Home/Welcome.js @@ -1,5 +1,5 @@ -import React from 'react'; +import React from 'react' -const Welcome = () =>

Welcome to Razzle

; +const Welcome = () =>

Welcome to Razzle

-export default Welcome; +export default Welcome diff --git a/examples/razzle/src/Home/index.js b/examples/razzle/src/Home/index.js index aa43bcc..9a156ac 100644 --- a/examples/razzle/src/Home/index.js +++ b/examples/razzle/src/Home/index.js @@ -1,10 +1,10 @@ -import React from 'react'; -import loadable from '@loadable/component'; -import './Home.css'; +import React from 'react' +import loadable from '@loadable/component' +import './Home.css' -const Intro = loadable(() => import('./Intro')); -const Welcome = loadable(() => import('./Welcome')); -const Logo = loadable(() => import('./Logo')); +const Intro = loadable(() => import('./Intro')) +const Welcome = loadable(() => import('./Welcome')) +const Logo = loadable(() => import('./Logo')) const Home = () => (
@@ -25,6 +25,6 @@ const Home = () => (
-); +) -export default Home; +export default Home diff --git a/examples/razzle/src/NotFound/index.js b/examples/razzle/src/NotFound/index.js index bd9eebd..7ff6641 100644 --- a/examples/razzle/src/NotFound/index.js +++ b/examples/razzle/src/NotFound/index.js @@ -1,5 +1,5 @@ -import React from 'react'; +import React from 'react' -const NotFound = () => (
Page could not be found
); +const NotFound = () =>
Page could not be found
-export default NotFound; \ No newline at end of file +export default NotFound diff --git a/examples/razzle/src/client.js b/examples/razzle/src/client.js index 5a31c59..ec8b404 100644 --- a/examples/razzle/src/client.js +++ b/examples/razzle/src/client.js @@ -1,18 +1,14 @@ -import React from 'react'; -import { hydrate } from 'react-dom'; -import { loadableReady } from '@loadable/component'; -import App from './App'; +import React from 'react' +import { hydrate } from 'react-dom' +import { loadableReady } from '@loadable/component' +import App from './App' -const root = document.getElementById('root'); +const root = document.getElementById('root') loadableReady(() => { - hydrate( - , - root - ); -}); + hydrate(, root) +}) if (module.hot) { - module.hot.accept(); + module.hot.accept() } - diff --git a/examples/razzle/src/index.js b/examples/razzle/src/index.js index 59b2595..adb2163 100644 --- a/examples/razzle/src/index.js +++ b/examples/razzle/src/index.js @@ -1,34 +1,34 @@ /* eslint-disable no-console, global-require */ -import http from 'http'; +import http from 'http' -let app = require('./server').default; +let app = require('./server').default -const server = http.createServer(app); +const server = http.createServer(app) -let currentApp = app; +let currentApp = app server.listen(process.env.PORT || 3000, error => { - if (error) { - console.log(error); - } + if (error) { + console.log(error) + } - console.log('🚀 started'); -}); + console.log('🚀 started') +}) if (module.hot) { - console.log('✅ Server-side HMR Enabled!'); + console.log('✅ Server-side HMR Enabled!') - module.hot.accept('./server', () => { - console.log('🔁 HMR Reloading `./server`...'); + module.hot.accept('./server', () => { + console.log('🔁 HMR Reloading `./server`...') - try { - app = require('./server').default; - server.removeListener('request', currentApp); - server.on('request', app); - currentApp = app; - } catch (error) { - console.error(error); - } - }); + try { + app = require('./server').default + server.removeListener('request', currentApp) + server.on('request', app) + currentApp = app + } catch (error) { + console.error(error) + } + }) } diff --git a/examples/razzle/src/server.js b/examples/razzle/src/server.js index 4636303..6e8cda4 100644 --- a/examples/razzle/src/server.js +++ b/examples/razzle/src/server.js @@ -1,28 +1,32 @@ -import path from 'path'; -import React from 'react'; -import express from 'express'; -import { html as htmlTemplate, oneLineTrim } from 'common-tags'; -import { renderToString } from 'react-dom/server'; -import { ServerLocation } from '@reach/router'; -import { ChunkExtractor, ChunkExtractorManager } from '@loadable/server'; -import App from './App'; +import path from 'path' +import React from 'react' +import express from 'express' +import { html as htmlTemplate, oneLineTrim } from 'common-tags' +import { renderToString } from 'react-dom/server' +import { ServerLocation } from '@reach/router' +import { ChunkExtractor, ChunkExtractorManager } from '@loadable/server' +import App from './App' -const server = express(); +const server = express() server .disable('x-powered-by') .use(express.static(process.env.RAZZLE_PUBLIC_DIR)) .get('/*', (req, res) => { - const extractor = new ChunkExtractor({ statsFile: path.resolve('build/loadable-stats.json'), entrypoints: ['client'] }); + const extractor = new ChunkExtractor({ + statsFile: path.resolve('build/loadable-stats.json'), + entrypoints: ['client'], + }) const html = renderToString( - - ); + , + ) - res.status(200).send(oneLineTrim(htmlTemplate` + res.status(200).send( + oneLineTrim(htmlTemplate` @@ -38,7 +42,8 @@ server ${extractor.getScriptTags()} - `)); - }); + `), + ) + }) -export default server; +export default server diff --git a/examples/server-side-rendering/src/server/main.js b/examples/server-side-rendering/src/server/main.js index 42522a1..716eea3 100644 --- a/examples/server-side-rendering/src/server/main.js +++ b/examples/server-side-rendering/src/server/main.js @@ -65,4 +65,5 @@ ${webExtractor.getStyleTags()} }), ) +// eslint-disable-next-line no-console app.listen(9000, () => console.log('Server started http://localhost:9000')) diff --git a/netlify.toml b/netlify.toml new file mode 100644 index 0000000..54490fb --- /dev/null +++ b/netlify.toml @@ -0,0 +1,11 @@ +# Global settings applied to the whole site. +# +# “publish” is the directory to publish (relative to root of your repo), +# “command” is your build command, +# “base” is directory to change to before starting build. if you set base: +# that is where we will look for package.json/.nvmrc/etc not repo root! + +[build] + base = "website" + publish = "website/public" + command = "yarn build:pp" \ No newline at end of file diff --git a/packages/webpack-plugin/src/index.js b/packages/webpack-plugin/src/index.js index 03640ef..0d302d2 100644 --- a/packages/webpack-plugin/src/index.js +++ b/packages/webpack-plugin/src/index.js @@ -5,7 +5,7 @@ class LoadablePlugin { constructor({ filename = 'loadable-stats.json', writeToDisk = false } = {}) { this.opts = { filename, writeToDisk } - // The Webpack compiler instance + // The Webpack compiler instance this.compiler = null } diff --git a/website/.eslintignore b/website/.eslintignore new file mode 100644 index 0000000..d24a37d --- /dev/null +++ b/website/.eslintignore @@ -0,0 +1,3 @@ +.cache/ +node_modules/ +/public/ \ No newline at end of file diff --git a/website/.gitignore b/website/.gitignore new file mode 100644 index 0000000..d24a37d --- /dev/null +++ b/website/.gitignore @@ -0,0 +1,3 @@ +.cache/ +node_modules/ +/public/ \ No newline at end of file diff --git a/website/README.md b/website/README.md new file mode 100644 index 0000000..5dd6a79 --- /dev/null +++ b/website/README.md @@ -0,0 +1,30 @@ +# Loadable Components website + +[Documentation site](https://www.smooth-code.com/open-source/loadable-components/) for [loadable components](https://github.com/smooth-code/loadable-components). This website is running on [gatsbyjs](gatsbyjs.org). + +## Getting Started + +To install and run the docs site locally: + +```bash +yarn +yarn dev +``` + +Then, open your favorite browser to [localhost:8000](http://localhost:8000/). GraphiQL runs at [localhost:8000/\_\_\_graphql](http://localhost:8000/___graphql). + +## Contributing + +Build the site to test locally. + +```bash +yarn build +``` + +Serve the build. + +```bash +yarn serve +``` + +Then, open your favorite browser to [localhost:9000](http://localhost:9000/) to verify everything looks correct. diff --git a/website/gatsby-config.js b/website/gatsby-config.js new file mode 100644 index 0000000..33d2121 --- /dev/null +++ b/website/gatsby-config.js @@ -0,0 +1,10 @@ +const { getGatsbyConfig } = require('smooth-doc/config') + +module.exports = getGatsbyConfig({ + root: __dirname, + name: 'Loadable Components', + slug: 'loadable-components', + github: 'https://github.com/smooth-code/loadable-components', + menu: ['Introduction', 'Guides', 'API'], + nav: [{ title: 'Docs', url: '/docs/' }], +}) diff --git a/website/gatsby-node.js b/website/gatsby-node.js new file mode 100644 index 0000000..4dd63c3 --- /dev/null +++ b/website/gatsby-node.js @@ -0,0 +1,13 @@ +const { getGatsbyNode } = require('smooth-doc/node') + +module.exports = getGatsbyNode({ + root: __dirname, +}) + +module.exports.createPages = ({ actions }) => { + actions.createRedirect({ + fromPath: `/docs/`, + toPath: `/docs/getting-started/`, + redirectInBrowser: true, + }) +} diff --git a/website/package.json b/website/package.json new file mode 100644 index 0000000..257ac01 --- /dev/null +++ b/website/package.json @@ -0,0 +1,13 @@ +{ + "private": true, + "scripts": { + "build": "gatsby build", + "build:pp": "gatsby build --prefix-paths", + "dev": "gatsby develop", + "serve": "gatsby serve" + }, + "dependencies": { + "gatsby": "^2.0.63", + "smooth-doc": "^0.0.1-alpha.5" + } +} diff --git a/website/src/images/home-logo.png b/website/src/images/home-logo.png new file mode 100644 index 0000000..398335e Binary files /dev/null and b/website/src/images/home-logo.png differ diff --git a/website/src/images/logo.png b/website/src/images/logo.png new file mode 100644 index 0000000..398335e Binary files /dev/null and b/website/src/images/logo.png differ diff --git a/website/src/pages/404.mdx b/website/src/pages/404.mdx new file mode 100644 index 0000000..6d762a8 --- /dev/null +++ b/website/src/pages/404.mdx @@ -0,0 +1,7 @@ +--- +title: Not Found +--- + +import { NotFound } from 'smooth-doc' + + diff --git a/website/src/pages/docs/api-loadable-component.mdx b/website/src/pages/docs/api-loadable-component.mdx new file mode 100644 index 0000000..4c68735 --- /dev/null +++ b/website/src/pages/docs/api-loadable-component.mdx @@ -0,0 +1,87 @@ +--- +menu: API +title: '@loadable/component' +order: 10 +--- + +# @loadable/component + +## Loadable + +Create a loadable component. + +| Arguments | Description | +| ------------------ | ---------------------------------------- | +| `loadFn` | The function call to load the component. | +| `options` | Optional options. | +| `options.fallback` | Fallback displayed during the loading. | + +```js +import loadable from '@loadable/component' + +const OtherComponent = loadable(() => import('./OtherComponent')) +``` + +## Lazy + +Create a loadable component "Suspense" ready. + +| Arguments | Description | +| --------- | ---------------------------------------- | +| `loadFn` | The function call to load the component. | + +```js +import { lazy } from '@loadable/component' + +const OtherComponent = lazy(() => import('./OtherComponent')) +``` + +## LoadableComponent + +A component created using `loadable` or `lazy`. + +| Props | Description | +| ---------- | ------------------------------------------------- | +| `fallback` | Fallback displayed during the loading. | +| `...` | Props are forwarded as first argument of `loadFn` | + +## loadable.lib + +Create a loadable library. + +| Arguments | Description | +| ------------------ | ---------------------------------------- | +| `loadFn` | The function call to load the component. | +| `options` | Optional options. | +| `options.fallback` | Fallback displayed during the loading. | + +```js +import loadable from '@loadable/component' + +const Moment = loadable.lib(() => import('moment')) +``` + +## lazy.lib + +Create a loadable library "Suspense" ready. + +| Arguments | Description | +| --------- | ---------------------------------------- | +| `loadFn` | The function call to load the component. | + +```js +import { lazy } from '@loadable/component' + +const Moment = lazy.lib(() => import('moment')) +``` + +## LoadableLibrary + +A component created using `loadable.lib` or `lazy.lib`. + +| Props | Description | +| ---------- | ---------------------------------------------------- | +| `children` | Function called when the library is loaded. | +| `ref` | Accepts a ref, populated when the library is loaded. | +| `fallback` | Fallback displayed during the loading. | +| `...` | Props are forwarded as first argument of `loadFn` | diff --git a/website/src/pages/docs/api-loadable-server.mdx b/website/src/pages/docs/api-loadable-server.mdx new file mode 100644 index 0000000..4f21f82 --- /dev/null +++ b/website/src/pages/docs/api-loadable-server.mdx @@ -0,0 +1,124 @@ +--- +menu: API +title: '@loadable/server' +order: 20 +--- + +# @loadable/server + +## ChunkExtractor + +Used to collect chunks server-side and get them as script tags or script elements. + +| Arguments | Description | +| --------------------- | ----------------------------------------------------------- | +| `options` | An object options. | +| `options.statsFile` | Stats file path generated using `@loadable/webpack-plugin`. | +| `options.stats` | Stats generated using `@loadable/webpack-plugin`. | +| `options.entrypoints` | Webpack entrypoints to load (default to `["main"]`). | +| `options.outputPath` | Optional output path (only for `requireEntrypoint`). | + +You must specify either `statsFile` or `stats` to be able to use `ChunkExtractor`. + +Using `statsFile` will automatically reload stats for you if they change. + +```js +import { ChunkExtractor } from '@loadable/server' + +const statsFile = path.resolve('../dist/loadable-stats.json') +const chunkExtractor = new ChunkExtractor({ statsFile }) +``` + +## chunkExtractor.collectChunks + +Wrap your application in a `ChunkExtractorManager`. + +| Arguments | Description | +| --------- | ------------------------------------------------------------ | +| `element` | JSX element that will be wrapped in `ChunkExtractorManager`. | + +```js +const app = chunkExtractor.collectChunks() +``` + +## chunkExtractor.requireEntrypoint + +Require the entrypoint of your application as a commonjs module. + +| Arguments | Description | +| --------- | ---------------------------------------------------------------- | +| `name` | Optional name the entrypoint, default to the first one (`main`). | + +```js +const { default: App } = chunkExtractor.requireEntrypoint() +const app = +``` + +## chunkExtractor.getScriptTags + +Get scripts as a string of `