The plugin analyzes chunks and extracts libraries to separated chunks with disabled tree-shaking (or not?).
The plugin teaches entries and runtime to work with shared chunks.
Entry points and runtime are loaded.
Entry point checks if a shared required to run chunks are loaded and notifies the runtime about it.
Runtime downloads all not downloaded libraries and marks them as downloaded.
Runtime runs an app.
Tree shaking is dead-code elimination. Read more
Because the plugin doesn't know which the library part will use in another application, it disabled tree shaking. Therefore bundles become large.
For the solution, the plugin provides an usedExports option. It can be an array of import names to be used by another application. See the example in the demo.
The plugin can install by any package manager.
or
Add plugin to webpack configuration file and set a list of libraries for sharing. There is an example with lodash
:
The plugin adds one more chunk with a hashed name to the bundle. It should be a shared lodash. When the application is loaded, the webpack runtime will check if lodash is loaded.
SharedLibraryWebpackPlugin
SharedLibraryWebpackPlugin
is a class that implements webpack plugin functionality,
libs
string | SharedLibrarySearchConfig | Array<string | SharedLibrarySearchConfig>
An option that configures the search for shared libraries and the formation of a chunk name. It can be a string or or an array of them.
namespace
string
The namespace for saving exported libraries
disableDefaultJsonpFunctionChange
boolean
By default: false
If true jsonpFunction
will be replaced with a random name
SharedLibrarySearchConfig
SharedLibrarySearchConfig
configures the search for sharing library and the formation of a chunk name.
pattern
string
An option to search for libraries in a bundle.
name
string
A name to search for a library in a bundle.
chunkName
string
A name of a shared chunk
If a pattern exists chunkName
is ignored
suffix
string
A chunk name suffix
By default library version {major}.{minor}-{prerelease}
separator
string
Separator for a chunk name and suffix
deps
string[]
Libraries that the current one depends on.
usedExports
string[]
The import names to be used by another application