Share Loader
The share loader allows you to share modules between webpack builds via a global namespace
Install
yarn add share-loader --save-dev
Usage
Webpack config of the core/host application
module: {
rules: [
{
test: /\.js?$/,
use: [
{
loader: 'share-loader',
options: {
modules: ['@material-ui/core', 'react', 'react-dom'],
namespace: 'some-name-space',
},
},
],
},
]
}
Webpack config of consumer apps
const { Externals } = require('share-loader')
externals: [
Externals({
namespace: 'some-name-space',
modules: ['@material-ui/core', 'react']
})
],
output: {
library: ['some-name-space', packageJson.name],
libraryTarget: 'umd'
}
You can see your shared dependencies in the Window object under the namespace variable you declared in your Webpack config.
Demo
core-app
- In the root folder,
cd
intocore-app
- Run
yarn install
- Run
yarn dev
- This will spin up a local dev server on
localhost:1234
feature-app
- In a seperate terminal window,
cd
intofeature-app
- Run
yarn install
- Run
yarn serve
- This will spin up a local server on
localhost:4567
and allow you to access the built JS file vialocalhost:4567/feature-app.js
- There is also a
yarn dev
command to allow you to make whatever changes you need.
Combining the Two
- Go to
localhost:1234
- Click on
Feature 1
in the top nav bar - Watch as
core-app
pulls infeature-app.js
and dynamically loads the page (you can see this in your browser's network tab)
Notes
- If you make changes in the feature app and they are not being reflected in the core app, make sure you disable caching in your browser's console.