Skip to content

Commit

Permalink
feat(chunks): add chunks plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
andrepolischuk committed Dec 18, 2023
1 parent 0e76aa7 commit 54f3562
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/chunks/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Chunks Plugin

Support for split chunks

## Install

```
npm install -D @rambler-tech/razzle-chunks
```

or

```
yarn add -D @rambler-tech/razzle-chunks
```

## Usage

Add the plugin to `razzle.config.js`

```js
const path = require('path')
const ChunksPlugin = require('@rambler-tech/razzle-chunks')

module.exports = {
plugins: [
ChunksPlugin({
vendors: /[\\/]node_modules[\\/]*[\\/]/
})
],
modifyWebpackConfig({webpackConfig}) {
// ...
return webpackConfig
}
}
```
31 changes: 31 additions & 0 deletions packages/chunks/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
module.exports = (groups) => ({
modifyWebpackConfig({env: {target}, webpackConfig}) {
const isBrowser = target === 'web'

if (isBrowser) {
const entries = Object.keys(webpackConfig.entry)

webpackConfig.optimization = {
splitChunks: {
cacheGroups: {}
}
}

for (const entry of entries) {
for (const group in groups) {
webpackConfig.optimization.splitChunks.cacheGroups[
`${entry}.${group}`
] = {
test: groups[group],
name: `${entry}.${group}`,
chunks: (chunk) => chunk.name === entry,
enforce: true,
priority: 1
}
}
}
}

return webpackConfig
}
})
14 changes: 14 additions & 0 deletions packages/chunks/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"name": "@rambler-tech/razzle-chunks",
"version": "0.0.0",
"main": "index.js",
"license": "MIT",
"sideEffects": false,
"publishConfig": {
"access": "public"
},
"peerDependencies": {
"razzle": ">=4",
"webpack": ">=5"
}
}

0 comments on commit 54f3562

Please sign in to comment.