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

feat: support multiple Webpack runtimes #701

Merged
merged 4 commits into from
Apr 5, 2021
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
12 changes: 6 additions & 6 deletions packages/component/.size-snapshot.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
{
"dist/loadable.cjs.js": {
"bundled": 16354,
"minified": 7304,
"gzipped": 2565
"bundled": 16504,
"minified": 7301,
"gzipped": 2586
},
"dist/loadable.esm.js": {
"bundled": 15975,
"minified": 7000,
"gzipped": 2495,
"bundled": 16125,
"minified": 6997,
"gzipped": 2516,
"treeshaked": {
"rollup": {
"code": 276,
Expand Down
6 changes: 3 additions & 3 deletions packages/component/src/loadableReady.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ const BROWSER = typeof window !== 'undefined'

export default function loadableReady(
done = () => {},
{ namespace = '' } = {},
{ namespace = '', chunkLoadingGlobal = '__LOADABLE_LOADED_CHUNKS__' } = {},
) {
if (!BROWSER) {
warn('`loadableReady()` must be called in browser only')
Expand Down Expand Up @@ -49,8 +49,8 @@ export default function loadableReady(
let resolved = false

return new Promise(resolve => {
window.__LOADABLE_LOADED_CHUNKS__ = window.__LOADABLE_LOADED_CHUNKS__ || []
const loadedChunks = window.__LOADABLE_LOADED_CHUNKS__
window[chunkLoadingGlobal] = window[chunkLoadingGlobal] || []
const loadedChunks = window[chunkLoadingGlobal]
const originalPush = loadedChunks.push.bind(loadedChunks)

function checkReadyState() {
Expand Down
9 changes: 5 additions & 4 deletions packages/webpack-plugin/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ class LoadablePlugin {
path,
writeToDisk,
outputAsset = true,
chunkLoadingGlobal = '__LOADABLE_LOADED_CHUNKS__',
} = {}) {
this.opts = { filename, writeToDisk, outputAsset, path }
this.opts = { filename, writeToDisk, outputAsset, path, chunkLoadingGlobal }

// The Webpack compiler instance
this.compiler = null
Expand Down Expand Up @@ -86,11 +87,11 @@ class LoadablePlugin {

const version = 'jsonpFunction' in compiler.options.output ? 4 : 5

// Add a custom chunk loading callback __LOADABLE_LOADED_CHUNKS__
// Add a custom chunk loading callback
if (version === 4) {
compiler.options.output.jsonpFunction = '__LOADABLE_LOADED_CHUNKS__'
compiler.options.output.jsonpFunction = this.opts.chunkLoadingGlobal
} else {
compiler.options.output.chunkLoadingGlobal = '__LOADABLE_LOADED_CHUNKS__'
compiler.options.output.chunkLoadingGlobal = this.opts.chunkLoadingGlobal
}

if (this.opts.outputAsset || this.opts.writeToDisk) {
Expand Down
17 changes: 9 additions & 8 deletions website/src/pages/docs/api-loadable-component.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ const OtherComponent = loadable(() => import('./OtherComponent'))
```

### `options.resolveComponent`

This is a function that receives the imported module (what the `import()` call resolves to) and the props, and returns the component.

The default value assumes that the component is exported as a default export.
Expand All @@ -43,17 +44,16 @@ export const Orange = () => 'Orange!'
// loadable.js

const LoadableApple = loadable(() => import('./components'), {
resolveComponent: (components) => components.Apple,
resolveComponent: components => components.Apple,
})

const LoadableOrange = loadable(() => import('./components'), {
resolveComponent: (components) => components.Orange,
resolveComponent: components => components.Orange,
})

const LoadableFruit = loadable(() => import('./components'), {
resolveComponent: (components, props) => components[props.fruit],
})

```

**Note:** The default `resolveComponent` breaks Typescript type inference due to CommonJS compatibility.
Expand Down Expand Up @@ -169,11 +169,12 @@ A component created using `loadable.lib` or `lazy.lib`.

Wait for all loadable components to be loaded. This method must only be used with Server Side Rendering, see [Server Side Rendering guide](/docs/server-side-rendering/).

| Arguments | Description |
| ------------------- | ----------------------------------------------------------------------------------------- |
| `done` | Function called when all components are loaded. |
| `options` | Optional options. |
| `options.namespace` | Namespace of your application (use only if you have several React apps on the same page). |
| Arguments | Description |
| ---------------------------- | ----------------------------------------------------------------------------------------- |
| `done` | Function called when all components are loaded. |
| `options` | Optional options. |
| `options.namespace` | Namespace of your application (use only if you have several React apps on the same page). |
| `options.chunkLoadingGlobal` | A custom `chunkLoadingGlobal` if set in the Webpack plugin |

```js
import { loadableReady } from '@loadable/component'
Expand Down
15 changes: 8 additions & 7 deletions website/src/pages/docs/api-loadable-webpack-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,14 @@ order: 30

Create a webpack loadable plugin.

| Arguments | Description |
| ------------------------------ | ----------------------------------------------------------------------------------- |
| `options` | Optional options |
| `options.filename` | Stats filename (default to `loadable-stats.json`) |
| `options.outputAsset` | Always write stats file to the `output.path` directory. Defaults to `true` |
| `options.writeToDisk` | Accepts `boolean` or `object`. Always write stats file to disk. Default to `false`. |
| `options.writeToDisk.filename` | Write assets to disk at given `filename` location |
| Arguments | Description |
| ------------------------------ | -------------------------------------------------------------------------------------------- |
| `options` | Optional options |
| `options.filename` | Stats filename (default to `loadable-stats.json`) |
| `options.outputAsset` | Always write stats file to the `output.path` directory. Defaults to `true` |
| `options.writeToDisk` | Accepts `boolean` or `object`. Always write stats file to disk. Default to `false`. |
| `options.writeToDisk.filename` | Write assets to disk at given `filename` location |
| `options.chunkLoadingGlobal` | Overrides Webpack's `chunkLoadingGlobal` allowing multiple Webpack runtimes on the same page |

```js
new LoadablePlugin({ filename: 'stats.json', writeToDisk: true })
Expand Down