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 2 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": 16441,
"minified": 7281,
"gzipped": 2578
},
"dist/loadable.esm.js": {
"bundled": 15975,
"minified": 7000,
"gzipped": 2495,
"bundled": 16062,
"minified": 6977,
"gzipped": 2508,
"treeshaked": {
"rollup": {
"code": 276,
Expand Down
7 changes: 5 additions & 2 deletions packages/component/src/loadableReady.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,11 @@ 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__
const prefix = namespace ? `__${namespace}` : ''
const loadedChunksKey = `${prefix}__LOADABLE_LOADED_CHUNKS__`
Copy link
Contributor Author

@wvanrensselaer wvanrensselaer Feb 5, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably a breaking change since namespace is separately used for getRequiredChunkKey above. Maybe I should add a separate more explicit option. Maybe just a chunkLoadingGlobal option for both loadableReady and LoadablePlugin which overrides the whole thing?


window[loadedChunksKey] = window[loadedChunksKey] || []
const loadedChunks = window[loadedChunksKey]
const originalPush = loadedChunks.push.bind(loadedChunks)

function checkReadyState() {
Expand Down
8 changes: 5 additions & 3 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,
namespace = null,
} = {}) {
this.opts = { filename, writeToDisk, outputAsset, path }
this.opts = { filename, writeToDisk, outputAsset, path, namespace }

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

const version = 'jsonpFunction' in compiler.options.output ? 4 : 5
const namespace = this.opts.namespace ? `__${this.opts.namespace}` : ''

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

if (this.opts.outputAsset || this.opts.writeToDisk) {
Expand Down
1 change: 1 addition & 0 deletions website/src/pages/docs/api-loadable-webpack-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ Create a webpack loadable plugin.
| `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.namespace` | Namespaces the chunk loading global to allow multiple Webpack runtimes |

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