Skip to content

Commit d351367

Browse files
feat: support multiple Webpack runtimes (#701)
* feat: support multiple Webpack runtimes * namespace chunk loading global in loadableReady * Change namespace to chunkLoadingGlobal * Update size snapshot
1 parent b640c82 commit d351367

File tree

5 files changed

+31
-28
lines changed

5 files changed

+31
-28
lines changed

packages/component/.size-snapshot.json

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
{
22
"dist/loadable.cjs.js": {
3-
"bundled": 16354,
4-
"minified": 7304,
5-
"gzipped": 2565
3+
"bundled": 16504,
4+
"minified": 7301,
5+
"gzipped": 2586
66
},
77
"dist/loadable.esm.js": {
8-
"bundled": 15975,
9-
"minified": 7000,
10-
"gzipped": 2495,
8+
"bundled": 16125,
9+
"minified": 6997,
10+
"gzipped": 2516,
1111
"treeshaked": {
1212
"rollup": {
1313
"code": 276,

packages/component/src/loadableReady.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ const BROWSER = typeof window !== 'undefined'
88

99
export default function loadableReady(
1010
done = () => {},
11-
{ namespace = '' } = {},
11+
{ namespace = '', chunkLoadingGlobal = '__LOADABLE_LOADED_CHUNKS__' } = {},
1212
) {
1313
if (!BROWSER) {
1414
warn('`loadableReady()` must be called in browser only')
@@ -49,8 +49,8 @@ export default function loadableReady(
4949
let resolved = false
5050

5151
return new Promise(resolve => {
52-
window.__LOADABLE_LOADED_CHUNKS__ = window.__LOADABLE_LOADED_CHUNKS__ || []
53-
const loadedChunks = window.__LOADABLE_LOADED_CHUNKS__
52+
window[chunkLoadingGlobal] = window[chunkLoadingGlobal] || []
53+
const loadedChunks = window[chunkLoadingGlobal]
5454
const originalPush = loadedChunks.push.bind(loadedChunks)
5555

5656
function checkReadyState() {

packages/webpack-plugin/src/index.js

+5-4
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ class LoadablePlugin {
1010
path,
1111
writeToDisk,
1212
outputAsset = true,
13+
chunkLoadingGlobal = '__LOADABLE_LOADED_CHUNKS__',
1314
} = {}) {
14-
this.opts = { filename, writeToDisk, outputAsset, path }
15+
this.opts = { filename, writeToDisk, outputAsset, path, chunkLoadingGlobal }
1516

1617
// The Webpack compiler instance
1718
this.compiler = null
@@ -86,11 +87,11 @@ class LoadablePlugin {
8687

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

89-
// Add a custom chunk loading callback __LOADABLE_LOADED_CHUNKS__
90+
// Add a custom chunk loading callback
9091
if (version === 4) {
91-
compiler.options.output.jsonpFunction = '__LOADABLE_LOADED_CHUNKS__'
92+
compiler.options.output.jsonpFunction = this.opts.chunkLoadingGlobal
9293
} else {
93-
compiler.options.output.chunkLoadingGlobal = '__LOADABLE_LOADED_CHUNKS__'
94+
compiler.options.output.chunkLoadingGlobal = this.opts.chunkLoadingGlobal
9495
}
9596

9697
if (this.opts.outputAsset || this.opts.writeToDisk) {

website/src/pages/docs/api-loadable-component.mdx

+9-8
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ const OtherComponent = loadable(() => import('./OtherComponent'))
2626
```
2727

2828
### `options.resolveComponent`
29+
2930
This is a function that receives the imported module (what the `import()` call resolves to) and the props, and returns the component.
3031

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

4546
const LoadableApple = loadable(() => import('./components'), {
46-
resolveComponent: (components) => components.Apple,
47+
resolveComponent: components => components.Apple,
4748
})
4849

4950
const LoadableOrange = loadable(() => import('./components'), {
50-
resolveComponent: (components) => components.Orange,
51+
resolveComponent: components => components.Orange,
5152
})
5253

5354
const LoadableFruit = loadable(() => import('./components'), {
5455
resolveComponent: (components, props) => components[props.fruit],
5556
})
56-
5757
```
5858

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

170170
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/).
171171

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

178179
```js
179180
import { loadableReady } from '@loadable/component'

website/src/pages/docs/api-loadable-webpack-plugin.mdx

+8-7
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,14 @@ order: 30
1010

1111
Create a webpack loadable plugin.
1212

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

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

0 commit comments

Comments
 (0)