Skip to content

Commit

Permalink
[RELEASE] The last stable release 0.7.2
Browse files Browse the repository at this point in the history
- add new option `enable` to enable / disable the plugin, e.g. by development
- add support of `RegExp` for option `extensions`
- remove deprecated option `silent`, use `verbose` to show process information (no braking change)
- add GitHub workflow + codecov
- color verbose output
- update packages
- update readme
  • Loading branch information
webdiscus committed Dec 13, 2021
1 parent 35e77b1 commit 9d6cbcb
Show file tree
Hide file tree
Showing 113 changed files with 1,718 additions and 12,734 deletions.
10 changes: 10 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
root = true

[*]
end_of_line = lf
insert_final_newline = true

[test/cases/**.html]
end_of_line = lf
# disable final new line for expected html files
insert_final_newline = false
12 changes: 12 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: biodiscus
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
otechie: # Replace with a single Otechie username
custom: # Replace with up to 4 custom sponsorship URLs e.g., ['link1', 'link2']
27 changes: 27 additions & 0 deletions .github/ISSUE_TEMPLATE/bug_report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
name: Bug report
about: Create a report to help us improve
title: "[BUG]"
labels: ''
assignees: ''

---

**Describe the bug**
A clear and concise description of what the bug is.

**To Reproduce**
If issue is small you can add the code block to commentary.
Preferably, create a bug repository with reproducible problems.


**Expected behavior**
A clear and concise description of what you expected to happen.

**Dev Environment (please complete the following information):**
- OS: [e.g. macOS / Windows 10 / Ubuntu 18]
- Node [e.g. 12 LTS, 14, 16.04]


**Additional context**
Add any other context about the problem here.
34 changes: 34 additions & 0 deletions .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Running Code Coverage

on: [ push, pull_request ]

jobs:
build:

runs-on: ubuntu-latest

strategy:
matrix:
node-version: [ 12.x, 14.x, 16.x ]

steps:
- name: Checkout repository
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Set up Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v1
with:
node-version: ${{ matrix.node-version }}

- name: Install dependencies
run: npm install

- name: Run the tests
run: npm run test:coverage

- name: Submit coverage data to codecov
uses: codecov/codecov-action@v1
with:
token: ${{ secrets.CODECOV_TOKEN }}
10 changes: 8 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
.DS_Store
.idea
build
_ignored
package-lock.json
coverage
coverage.lcov
node_modules
test/outputs
public
dist
test/output/
1 change: 1 addition & 0 deletions .npmignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
/test
19 changes: 18 additions & 1 deletion .prettierrc
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
{
"trailingComma": "es5"
"printWidth": 120,
"tabWidth": 2,
"useTabs": false,
"singleQuote": true,
"trailingComma": "es5",
"semi": true,
"endOfLine": "lf",
"bracketSpacing": true,
"arrowParens": "always",
"bracketSameLine": true,
"overrides": [
{
"files": ["*.html"],
"options": {
"tabWidth": 4
}
}
]
}
11 changes: 10 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
## 0.7.2 (Dec 13, 2021) The last stable version
- add new option `enable` to enable / disable the plugin, e.g. by development
- add support of `RegExp` for option `extensions`
- remove deprecated option `silent`, use `verbose` to show process information (no braking change)
- add GitHub workflow + codecov
- color verbose output
- update packages
- update readme

## 0.7.1 (Jan 14, 2021)
### Bugfixes
- The issue infinite recursion by collect of resources from dependency modules by usage in react app some big components with many thousands dependencies.
Expand Down Expand Up @@ -64,4 +73,4 @@ Being overly careful here, this version is not breaking for almost all the exist
- added LICENSE.txt ([220e20](https://github.com/fqborges/webpack-fix-style-only-entries/commit/220e20eeb9bc95652a942812a424aadd92ec7d1f))

## 0.4.0 (Sep 8, 2019)
- feat: add support for module js files (PR [#21](https://github.com/fqborges/webpack-fix-style-only-entries/pull/21))
- feat: add support for module js files (PR [#21](https://github.com/fqborges/webpack-fix-style-only-entries/pull/21))
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
ISC License

Copyright (c) 2021, webdiscus
Copyright (c) 20218-2020, fqborges
Copyright (c) 2020, webdiscus

Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
Expand All @@ -13,4 +13,4 @@ MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
184 changes: 154 additions & 30 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,95 @@
[![npm version](https://badge.fury.io/js/webpack-remove-empty-scripts.svg)](https://www.npmjs.com/package/webpack-remove-empty-scripts)
[![npm version](https://badge.fury.io/js/webpack-remove-empty-scripts.svg?v/0.7.2)](https://www.npmjs.com/package/webpack-remove-empty-scripts/v/0.7.2)
[![node](https://img.shields.io/node/v/webpack-remove-empty-scripts)](https://nodejs.org)
[![node](https://img.shields.io/github/package-json/dependency-version/webdiscus/webpack-remove-empty-scripts/peer/webpack)](https://webpack.js.org/)
[![codecov](https://codecov.io/gh/webdiscus/webpack-remove-empty-scripts/branch/master/graph/badge.svg)](https://codecov.io/gh/webdiscus/webpack-remove-empty-scripts)
[![node](https://img.shields.io/npm/dm/webpack-remove-empty-scripts)](https://www.npmjs.com/package/webpack-remove-empty-scripts)

# [webpack-remove-empty-scripts](https://www.npmjs.com/package/webpack-remove-empty-scripts)


The plugin remove empty scripts generated by usage only a style (css/scss/sass/less/stylus) without a js script in entry.
The plugin removes empty `js` scripts generated when using only the styles like `css` `scss` `sass` `less` `stylus` in the webpack entry.

You can find more info by reading the following issues:
This is improved fork of original plugin [webpack-fix-style-only-entries](https://github.com/fqborges/webpack-fix-style-only-entries) ver. 0.6.0.\
This fork fixes some deprecation messages and some issues in React. See the details in [changelog](https://github.com/webdiscus/webpack-remove-empty-scripts/blob/master/CHANGELOG.md#061-oct-20-2020).

- https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/518
- https://github.com/webpack-contrib/mini-css-extract-plugin/issues/151
The plugin support only `Webpack 5`.
For `Webpack 4` use original [plugin](https://github.com/fqborges/webpack-fix-style-only-entries).

This is a fork of original plugin https://github.com/fqborges/webpack-fix-style-only-entries (ver. 0.6.0).
In this fork fixed some deprecation messages and integration tests for Webpack 5. See the details in [changelog](https://github.com/webdiscus/webpack-remove-empty-scripts/blob/master/CHANGELOG.md#061-oct-20-2020).
## Description of the problem

The plugin support only **Webpack 5** using new ChunkGraph and ModuleGraph APIs.
For **Webpack 4** use original [plugin](https://github.com/fqborges/webpack-fix-style-only-entries).
Webpack generates a js file for each resource defined in a webpack entry.
Some extract plugins use webpack entry to define non-js resources.
For example, in webpack entry might be defined resources like js, css, scss, html, pug, etc.
Each resource type needs its own extract plugin and loader. Such a extract plugin should take care of eliminating the phantom js files for non-js resources by self.
But the `mini-css-extract-plugin` not do it.
This plugin fixes this, finds and removes phantom js files for non-js resources.

View on: [GitHub](https://github.com/webdiscus/webpack-remove-empty-scripts), [npmjs](https://www.npmjs.com/package/webpack-remove-empty-scripts)
```js
module.exports = {
entry: {
main: './main.js', // the generated `main.js` is what we expect
styles: './main.scss', // will be generated the expected `styles.css` and the unexpected `styles.js`
},
// ...
}
```

You can find more info by the following issues:

- [extract-text-webpack-plugin issue](https://github.com/webpack-contrib/extract-text-webpack-plugin/issues/518)
- [mini-css-extract-plugin issue](https://github.com/webpack-contrib/mini-css-extract-plugin/issues/151)

## NEW
> The `experimental` version `^0.8.0` has **_new improved and fast algorithm_** to detect generated needless empty js files.\
> Please test your project before using it in production.\
> If you have a problem with the new version, please create a [new issue](https://github.com/webdiscus/webpack-remove-empty-scripts/issues).
> :warning: The last stable release version is `0.7.2` in the branch [`stable`](https://github.com/webdiscus/webpack-remove-empty-scripts/tree/stable).
## Propose
If you use the `mini-css-extract-plugin` only to extract `css` files from styles defined in webpack entry
like `scss` `sass` `less` `stylus` then try to use **new entry extract plugin** - [pug-plugin](https://github.com/webdiscus/pug-plugin).

The `pug-plugin`:

- extract HTML and CSS from `pug` `html` `scss` resources defined in `webpack entry`
- doesn't need any fix plugins like `webpack-remove-empty-scripts`, because it doesn't generate empty `js` files
- is very flexible and fast

Improve performance with `pug-plugin`. Using the `pug-plugin` for `pug` `html` `scss` etc in the `webpack entry` no longer requires additional plugins such as:
- [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin)
- [mini-css-extract-plugin](https://github.com/webpack-contrib/mini-css-extract-plugin)
- [webpack-remove-empty-scripts](https://github.com/webdiscus/webpack-remove-empty-scripts) (bug fix plugins for `mini-css-extract-plugin`)
- [pug-loader](https://github.com/webdiscus/pug-loader) (this loader is already included in the `pug-plugin`)

## How to use
Install `npm install -D webpack-remove-empty-scripts`.
For example, `webpack.config.js`
```js
const PugPlugin = require('pug-plugin');
module.exports = {
entry: {
'main': 'main.js',
'styles': 'styles.scss',
'index': 'index.html', // now is possible define HTML file in entry
'page01': 'page01.pug', // now is possible define PUG file in entry
// ...
},
plugins: [
new PugPlugin(), // supports zero config using default webpack output options
]
// ...
};
```
> The plugin can be used not only for `pug` but also for simply extracting `HTML` or `CSS` from `webpack entry`, independent of pug usage.
Require and add to webpack.config plugins.
For more examples see the [pug-plugin](https://github.com/webdiscus/pug-plugin).

## Install
```console
npm install webpack-remove-empty-scripts --save-dev
```

## Usage
The example of webpack.config.js:
```javascript
const MiniCssExtractPlugin = require('mini-css-extract-plugin');
const RemoveEmptyScriptsPlugin = require('webpack-remove-empty-scripts');
Expand Down Expand Up @@ -53,28 +120,85 @@ module.exports = {
```

## Options

| Name | Type | Default | Description |
|------------|------------------|-----------------------------------------|-------------|
| verbose | boolean | false | show logs to console |
| extensions | Array[string] | ['css', 'scss', 'sass', 'less', 'styl'] | file extensions for styles |
| ignore | string or RegExp or Array[string] or Array[RegExp] | | match resource path to be ignored |

### Example config:

### `enabled`
Type: `boolean` Default: `true`<br>
Enable / disable the plugin.
**Tip:** Use `disable` for development to improve performance.

### `extensions`
Type: `RegExp` Default: `/\.(css|scss|sass|less|styl)([?].*)?$/`
Note: the Regexp should have the query part at end `([?].*)?$` to match assets like `style.css?key=val` <br>
Type: `string[]` Default: `['css', 'scss', 'sass', 'less', 'styl']`. It is automatically converted to type `RegExp`.

### `ignore`
Type: `string | RegExp | string[] | RegExp[]` Default: `null`<br>
Match resource path to be ignored.

### `verbose`
Type: `boolean` Default: `false`<br>
Show process information.

## Recipes

### Show logs to console by development
```JavaScript
// show logs to console, use it for development
new RemoveEmptyScriptsPlugin({ verbose: true })
const isProduction = process.env.NODE_ENV === 'production';
new RemoveEmptyScriptsPlugin({ verbose: isProduction !== true })
```

### Disable plugin by development to improve performance
```JavaScript
// to identify only 'foo' and 'bar' extensions as styles
new RemoveEmptyScriptsPlugin({ extensions:['foo', 'bar'] })
const isProduction = process.env.NODE_ENV === 'production';
new RemoveEmptyScriptsPlugin({ enabled: isProduction === true })
```

## Recipes
### Identify only `.foo` and `.bar` extensions as styles

```JavaScript
new RemoveEmptyScriptsPlugin({ extensions: /\.(foo|bar)$/ })
```

### Usage a javascript entry to styles
Give an especial extension to your file, for example `.css.js`:
```JavaScript
new RemoveEmptyScriptsPlugin({ extensions: /\.(css.js)$/ })
```

### Recursive ignore all js files from directory, for example `my-workers/`
```JavaScript
new RemoveEmptyScriptsPlugin({
ignore: [
/my-workers\/.+\.js$/,
]
})
```

### Usage webpack-hot-middleware
```JavaScript
new RemoveEmptyScriptsPlugin({
ignore: [
'webpack-hot-middleware',
]
})
```
[See the test case](https://github.com/webdiscus/webpack-remove-empty-scripts/blob/master/test/cases/css-entry-with-ignored-hmr/webpack.config.js).

## Testing

`npm run test` will run the unit and integration tests.\
`npm run test:coverage` will run the tests with coverage.

## Also See

- more examples of usages see in [test cases](https://github.com/webdiscus/pug-plugin/tree/master/test/cases)
- [`pug-plugin`][pug-plugin]
- [`pug-loader`][pug-loader]

### I use a javascript entry to styles:
Give an especial extension to your file (`.css.js` for example) and configure `new RemoveEmptyScriptsPlugin({ extensions:['css.js'] })`.
## License
[ISC](https://github.com/webdiscus/webpack-remove-empty-scripts/blob/master/LICENSE)

### I use webpack-hot-middleware:
Configure this plugin as `new RemoveEmptyScriptsPlugin({ ignore: 'webpack-hot-middleware' })`. See: https://github.com/webdiscus/webpack-remove-empty-scripts/blob/master/test/cases/css-entry-with-ignored-hmr/webpack.config.js
<!-- prettier-ignore-start -->
[pug-plugin]: https://github.com/webdiscus/pug-plugin
[pug-loader]: https://github.com/webdiscus/pug-loader
<!-- prettier-ignore-end -->
Loading

0 comments on commit 9d6cbcb

Please sign in to comment.