https://github.com/financial-times/disable-tree-shaking-for-chunk-plugin
🌲 A Webpack plugin to disable tree shaking for all JS modules in the specified chunks.
https://github.com/financial-times/disable-tree-shaking-for-chunk-plugin
customer-products platforms-customer-products
Last synced: 11 months ago
JSON representation
🌲 A Webpack plugin to disable tree shaking for all JS modules in the specified chunks.
- Host: GitHub
- URL: https://github.com/financial-times/disable-tree-shaking-for-chunk-plugin
- Owner: Financial-Times
- License: mit
- Created: 2019-11-30T10:28:38.000Z (over 6 years ago)
- Default Branch: main
- Last Pushed: 2024-09-03T12:12:47.000Z (almost 2 years ago)
- Last Synced: 2024-11-08T22:16:39.051Z (over 1 year ago)
- Topics: customer-products, platforms-customer-products
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/disable-tree-shaking-for-chunk-plugin
- Size: 33.2 KB
- Stars: 6
- Watchers: 31
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Codeowners: .github/CODEOWNERS
Awesome Lists containing this project
README
# Disable Tree Shaking For Chunk Plugin
[](https://github.com/Financial-Times/disable-tree-shaking-for-chunk-plugin/blob/main/LICENSE) [](https://app.circleci.com/pipelines/github/Financial-Times/disable-tree-shaking-for-chunk-plugin) [](https://www.npmjs.com/package/disable-tree-shaking-for-chunk-plugin)
This plugin for [Webpack 5] can disable tree shaking for all modules contained in specified chunks. It is intended to help improve long-term caching and code reuse between project installations and builds.
[Webpack 5]: https://webpack.js.org/
## Installation
This is a [Node.js] module available through the [npm] registry. Node 8 and Webpack 4.38 or higher are required.
Installation is done using the [npm install] command:
```sh
$ npm install --save-dev disable-tree-shaking-for-chunk-plugin
```
Once installed the plugin can be added to your [Webpack plugins configuration][plugins]:
```js
const DisableTreeShakingForChunk = require('disable-tree-shaking-for-chunk-plugin')
module.exports = {
//...
plugins: [
new DisableTreeShakingForChunk({
test: 'chunk-name'
})
]
}
```
[Node.js]: https://nodejs.org/
[npm]: http://npmjs.com/
[npm install]: https://docs.npmjs.com/getting-started/installing-npm-packages-locally
[plugins]: https://webpack.js.org/configuration/plugins/
[optimization]: https://webpack.js.org/configuration/optimization/#optimizationmoduleids
## Options
### `test` (string, RegExp, Function, Array, Set)
Matches the chunk name. It may be a string matched with strict equality, a regular expression for more complex string matching, a function which will receive the chunk name as an argument and should return a boolean, or an array or set of strings.
## Example
Below demonstrates part of a Webpack configuration file which sets up code splitting for a project. It has one cache group defined which will create a separate chunk for each package defined in the array.
```js
const DisableTreeShakingForChunk = require('disable-tree-shaking-for-chunk-plugin')
const commonLibraries = ['react', 'redux', 'regenerator-runtime']
module.exports = {
optimization: {
splitChunks: {
cacheGroups: {
commonLibraries: {
test(module) {
const packageName = getPackageName(module.context)
return packageName ? commonLibraries.includes(packageName) : false
},
name(module) {
return getPackageName(module.context)
}
}
}
}
},
plugins: [
new DisableTreeShakingForChunk({
test: commonLibraries
})
]
}
```
## Motivation
By default when running Webpack in production mode it will try to track the properties exported by JavaScript modules and flag when the module is imported and which of those properties is used. It's this clever tracking of "used exports" that enables [tree shaking](https://webpack.js.org/guides/tree-shaking/) by "pruning" any unused properties. Usually, this is a useful feature as it enables us to ship less code to our users but for cases where we'd like our compiled code to be cached for a long time or be reused by separate applications we need to disable it because how the module may be used over time and by different apps is unknown.
## Prior Art
This plugin is based upon Webpack's internal [`FlagInitialModulesAsUsedPlugin`][flag-plugin] by Tobias Koppers.
[flag-plugin]: https://github.com/webpack/webpack/blob/webpack-4/lib/FlagInitialModulesAsUsedPlugin.js
## Development
This project uses [Prettier] for automatic code formatting and is tested with [Jasmine].
[Prettier]: https://prettier.io/
[Jasmine]: http://jasmine.github.io/
## License
This package is [MIT] licensed.
[MIT]: https://opensource.org/licenses/MIT