https://github.com/kudashevs/webpack-remove-code-blocks
A webpack plugin that removes blocks of code by specific patterns from any code to be processed with webpack.
https://github.com/kudashevs/webpack-remove-code-blocks
Last synced: about 2 months ago
JSON representation
A webpack plugin that removes blocks of code by specific patterns from any code to be processed with webpack.
- Host: GitHub
- URL: https://github.com/kudashevs/webpack-remove-code-blocks
- Owner: kudashevs
- License: mit
- Created: 2022-01-08T16:42:56.000Z (over 3 years ago)
- Default Branch: master
- Last Pushed: 2024-10-19T06:48:39.000Z (8 months ago)
- Last Synced: 2024-10-20T09:17:43.569Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 143 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
Webpack Remove Code Blocks 
==========================The `webpack-remove-code-blocks` removes blocks of code marked with special labels.
The loader can be incorporated into the build process to remove the code that you don't want to see in production. It supports
the usage of multiple blocks. The loader was originated from [Webpack Remove Block](https://github.com/ee01/webpack-remove-blocks).## Install
```bash
# NPM
npm install --save-dev webpack-remove-code-blocks
# Yarn
yarn add --dev webpack-remove-code-blocks
```## Usage example
Let's start with a simple usage example. For example, we want to remove some code from a bundle while we build a project.
To do that, we need to take a few simple steps.Firstly, we need to add the loader and some additional settings to our webpack configuration:
```javascript
module.exports = {
module: {
rules: [
{
test: /\.js$/, // files we want to procces
exclude: /(node_modules|bower_components|\.spec\.js)/, // files we want to exclude
use: [
{
loader: 'webpack-remove-code-blocks', // use the loader
},
],
},
],
},
};
```Then, we can mark unwanted blocks of code in our `.js` files using comments with the special syntax `devblock:start` and `devblock:end`:
```javascript
/* devblock:start */
console.log('something not for production');
/* devblock:end */
```After the bundling process, the marked blocks will be removed (the comments will be removed too).
## Advanced usage example
Let's suppose, that we have a more sophisticated task. We want to use different labels (we might want to keep some code
in staging, but not in the production environment) and process different file extensions. That's not a problem.The only thing we need to do is to provide some additional settings to our webpack configuration:
```javascript
module.exports = {
module: {
rules: [
{
test: /\.js|\.ts|\.tsx$/, // files we want to procces
exclude: /(node_modules|bower_components|\.spec\.js)/, // files we want to exclude
use: [
{
loader: 'webpack-remove-code-blocks', // use the loader
options: {
blocks: [ // define three different blocks
'debug',
'devblock',
{
start: 'devblock_start',
end: 'devblock_end',
prefix: '/*',
suffix: '*/',
},
],
},
},
],
},
],
},
};
```Let's now build our project with this code inside:
```javascript
/* debug:start */
console.log('debug');
/* debug:end */
var makeFoo = function(bar, baz) {
// The following code will be removed with the loader
/* devblock:start */
if (bar instanceof Bar !== true) {
throw new Error('makeFoo: bar param is required and must be instance of Bar');
}
/* devblock:end */
/* devblock_start */
if (baz instanceof Baz !== true) {
throw new Error('makeFoo: baz param is required and must be instance of Baz');
}
/* devblock_end */// This code will remain
return new Foo(bar, baz);
}
```After the bundling process, the result will be as follows:
```javascript
var makeFoo = function(bar, baz) {
// The following code will be removed with the loader// This code will remain
return new Foo(bar, baz);
}
```## Options
If you want to define different comment blocks, use the `options.blocks` array. Each element of the array describes a unique
block of comments to be removed. The block can be described by an object with the following properties:
```
start: 'dev_start', # a string value that defines the beginning of a block to remove
end: 'dev_end', # a string value that defines the end of a block to remove
prefix: '/*', # a string value that defines the beginning of a comment
suffix: '*/', # a string value that defines the end of a comment
```Or, if you don't want to clutter your configuration, a block can be described by just a simple string. The string `debug`
will represent a block of comments with the following properties:
```
start: 'debug:start',
end: 'debug:end',
prefix: '/*',
suffix: '*/',
```## License
The MIT License (MIT). Please see [License File](LICENSE.md) for more information.