Skip to content

Instantly share code, notes, and snippets.

@crkrenn
Created May 11, 2024 16:26
Show Gist options
  • Save crkrenn/162f5c9dc707b32862278ce8cd98464c to your computer and use it in GitHub Desktop.
Save crkrenn/162f5c9dc707b32862278ce8cd98464c to your computer and use it in GitHub Desktop.
chat gpt shared webpack components
Using Webpack to bundle shared components in a monorepo setup with multiple React applications involves creating a separate package or directory for these components, configuring Webpack to build this package, and then integrating the bundled output into your individual applications. Here's a step-by-step guide to achieve this:
### Step 1: Organize Your Repository Structure
First, set up your monorepo structure to separate shared components from individual applications. Here’s a basic layout:
```
/my-monorepo
|-- /packages
| |-- /shared-components
| | |-- /src
| | | |-- Button.js
| | | |-- NavBar.js
| | |-- package.json
| | |-- webpack.config.js
|-- /apps
| |-- /app-one
| | |-- /src
| | |-- package.json
| | |-- webpack.config.js
| |-- /app-two
| | |-- /src
| | |-- package.json
| | |-- webpack.config.js
|-- package.json
```
### Step 2: Set Up Webpack for Shared Components
Configure Webpack in the `shared-components` directory to bundle these components into a distributable format. Here’s a simple example of a `webpack.config.js` for the shared components:
```javascript
const path = require('path');
module.exports = {
entry: './src/index.js', // Entry point where you export all components
output: {
path: path.resolve(__dirname, 'dist'),
filename: 'shared-components.bundle.js',
library: 'SharedComponents',
libraryTarget: 'umd'
},
module: {
rules: [
{
test: /\.js$/,
exclude: /node_modules/,
use: {
loader: 'babel-loader', // Transpile JSX and ES6
options: {
presets: ['@babel/preset-react', '@babel/preset-env']
}
}
}
]
},
externals: {
react: 'React',
'react-dom': 'ReactDOM'
}
};
```
**Key Points**:
- **entry**: Points to the file (usually `index.js`) where all shared components are exported.
- **output**: Configures where the bundled file is saved and how it is structured.
- **libraryTarget**: `'umd'` allows this bundle to be used in various environments (CommonJS, AMD, and as global variables).
- **externals**: Avoid bundling React and ReactDOM into your shared components library; they should be provided by the consumer applications.
### Step 3: Build Your Shared Components
Run Webpack in the `shared-components` directory to produce the bundled file. Add a script in `package.json` of `shared-components` to simplify this:
```json
"scripts": {
"build": "webpack"
}
```
Then, run `npm run build` in the `shared-components` directory.
### Step 4: Integrate into Applications
After building the shared components, you have a few options to integrate the output into your applications:
1. **Directly Reference the Bundle**:
Include the script in your applications' HTML file or import it directly in your JavaScript:
```html
<script src="../shared-components/dist/shared-components.bundle.js"></script>
```
Or:
```javascript
import SharedComponents from '../shared-components/dist/shared-components.bundle.js';
const { Button, NavBar } = SharedComponents;
```
2. **Publish to NPM** (even if it’s a private registry):
If you set up an npm package in the `shared-components` directory, you can publish this package and then install it in both `app-one` and `app-two` like any other npm package.
3. **Use Yarn Workspaces or Lerna**:
This is ideal for handling dependencies in a monorepo and can automatically link packages locally without publishing them.
### Step 5: Optimize and Maintain
As you expand your shared component library, consider:
- Adding more Webpack loaders and plugins as needed, e.g., for handling CSS, images.
- Continuously updating the shared components package and ensuring all consuming applications are compatible with the changes.
By following these steps, you can effectively manage and bundle shared React components in a monorepo, improving code reuse and maintainability across multiple applications.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment