Skip to content

Commit

Permalink
Merge pull request #10 from jackypan1989/v2
Browse files Browse the repository at this point in the history
  • Loading branch information
jackypan1989 committed Oct 20, 2020
2 parents 80bc411 + 6c8c969 commit 3058cc5
Show file tree
Hide file tree
Showing 18 changed files with 6,112 additions and 14,060 deletions.
33 changes: 33 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
{
"env": {
"node": true,
"jest": true
},
"root": true,
"parser": "@typescript-eslint/parser",
"plugins": [
"@typescript-eslint"
],
"extends": [
"eslint:recommended",
"plugin:@typescript-eslint/recommended"
],
"rules": {
"array-bracket-spacing": [
"error",
"never"
],
"indent": [
"error",
2
],
"quotes": [
"error",
"single"
],
"semi": [
"error",
"never"
]
}
}
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
MIT License

Copyright (c) 2018 Guan Yu Pan (Jacky)
Copyright (c) 2020 Guan Yu Pan (Jacky)

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
63 changes: 24 additions & 39 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,21 @@
[![](https://img.shields.io/npm/dt/del-webpack-plugin.svg)](https://www.npmjs.com/package/del-webpack-plugin)
![](https://img.shields.io/github/license/jackypan1989/del-webpack-plugin.svg)
# Del-webpack-plugin
This webpack plugin clean old files after build
Just as the well-known plugin [clean-webpack-plugin](https://github.com/johnagan/clean-webpack-plugin), and more than it.

**v2 was released, support webpack v5 !**

A clean webpack plugin which can remove old files after bundling just as the well-known plugin [clean-webpack-plugin](https://github.com/johnagan/clean-webpack-plugin), and more than it.

## changelog
2020-10-20 (2.0.0): rewrite to ts, rollup, and support webpack v5
2018-04-25 (1.0.5): upgrade to webpack v4
2018-05-28 (1.0.6): fix and update lib
2018-08-30 (1.1.0): add keepGeneratedAssets option
2018-10-18 (1.2.0): add allowExternal option
2018-10-18 (1.2.0): add allowExternal option

## feature
- [x] webpack v5
- [x] typed source code
- [x] only delete after webpack compile
- [x] skip plugin if compile error
- [x] multiple entry / path support
Expand All @@ -21,19 +26,19 @@ Just as the well-known plugin [clean-webpack-plugin](https://github.com/johnagan
- [x] example with webpack
- [x] support cross platform

![](https://i.imgur.com/t65OjUv.png)
![](https://i.imgur.com/B1UWz2n.png)

## install
```
// use npm
npm install -D del-webpack-plugin
// use yarn
```shell
# use yarn
yarn add -D del-webpack-plugin

# use npm
npm i -D del-webpack-plugin
```

## usage (in your webpack config)
```
```js
const DelWebpackPlugin = require('del-webpack-plugin')

{
Expand All @@ -51,34 +56,14 @@ const DelWebpackPlugin = require('del-webpack-plugin')

## options

### options.info
console.log added files and deleted files
- type: Boolean
- default: true

### options.keepGeneratedAssets
keep webpack generated files
- type: Boolean
- default: true

### options.allowExternal
allows del-webpack-plugin to delete files outside of webpack root folder
- type: Boolean
- default: false

### options.include
a file list you wanna delete
it will delete all files and folders by default
- type: [String]
- default: ['**']
- example: ['trash.js', 'trash/*.js']

// only files not folders use ['**.*']
| field | desc | type | default |
|---|---|---|---|
| info | console.log added files and deleted files | ```boolean``` | ```true``` |
| keepGeneratedAssets | keep webpack generated files | ```boolean``` | ```true``` |
| allowExternal | allows del-webpack-plugin to delete files outside of webpack root folder | ```boolean``` | ```false``` |
| include | a file list you wanna delete it will delete all files and folders by default, example: ```['trash.js', 'trash/*.js']``` | ```string[]``` | ```['**']``` |
| exclude | a file list you dont wanna delete, example: ```['test.js', 'test/*.js']``` | ```string[]``` | ```[]``` |

### options.exclude
a file list you dont wanna delete
- type: [String]
- default: []
- example: ['test.js', 'test/*.js']
---

Welcome any issues and PRs submit :D
**Welcome any issues and PRs submit :D**
63 changes: 63 additions & 0 deletions dist/index.cjs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
'use strict';

var chalk2 = require('chalk');
var del2 = require('del');
var path2 = require('path');

function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'default' in e ? e : { 'default': e }; }

var chalk2__default = /*#__PURE__*/_interopDefaultLegacy(chalk2);
var del2__default = /*#__PURE__*/_interopDefaultLegacy(del2);
var path2__default = /*#__PURE__*/_interopDefaultLegacy(path2);

var __assign = Object.assign;
class DelWebpackPlugin {
constructor(options) {
const defaultOptions = {
info: true,
keepGeneratedAssets: true,
exclude: [],
include: ["**"],
allowExternal: false
};
this.options = __assign(__assign({}, defaultOptions), options);
}
apply(compiler) {
const outputPath = compiler.options.output.path;
const callback = (stats) => {
if (stats.hasErrors()) {
console.log();
console.log(`${chalk2__default['default'].red("Del Webpack Plugin stopped according to module failed.")}`);
return;
}
const allowExternal = this.options.allowExternal;
const assetNames = stats.toJson().assets.map((asset) => asset.name);
const assetPatterns = this.options.keepGeneratedAssets ? assetNames.map((name) => path2__default['default'].join(outputPath, name)) : [];
const includePatterns = this.options.include.map((name) => path2__default['default'].join(outputPath, name));
const excludePatterns = this.options.exclude.map((name) => path2__default['default'].join(outputPath, name));
const allExcludePatterns = [
outputPath,
...excludePatterns,
...assetPatterns
];
del2__default['default'](includePatterns, {
force: allowExternal,
ignore: allExcludePatterns
}).then((paths) => {
if (this.options.info) {
console.log();
console.log("===== Del Webpack Plugin ===");
console.log(`${chalk2__default['default'].green("Added files:")}`);
assetNames.map((name) => console.log(name));
console.log();
console.log(`${chalk2__default['default'].red("Deleted files:")}`);
paths.map((name) => console.log(path2__default['default'].basename(name)));
console.log("============================");
console.log();
}
});
};
compiler.hooks.done.tap("del-webpack-plugin", callback);
}
}
module.exports = DelWebpackPlugin;
55 changes: 55 additions & 0 deletions dist/index.esm.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import chalk2 from 'chalk';
import del2 from 'del';
import path2 from 'path';

var __assign = Object.assign;
class DelWebpackPlugin {
constructor(options) {
const defaultOptions = {
info: true,
keepGeneratedAssets: true,
exclude: [],
include: ["**"],
allowExternal: false
};
this.options = __assign(__assign({}, defaultOptions), options);
}
apply(compiler) {
const outputPath = compiler.options.output.path;
const callback = (stats) => {
if (stats.hasErrors()) {
console.log();
console.log(`${chalk2.red("Del Webpack Plugin stopped according to module failed.")}`);
return;
}
const allowExternal = this.options.allowExternal;
const assetNames = stats.toJson().assets.map((asset) => asset.name);
const assetPatterns = this.options.keepGeneratedAssets ? assetNames.map((name) => path2.join(outputPath, name)) : [];
const includePatterns = this.options.include.map((name) => path2.join(outputPath, name));
const excludePatterns = this.options.exclude.map((name) => path2.join(outputPath, name));
const allExcludePatterns = [
outputPath,
...excludePatterns,
...assetPatterns
];
del2(includePatterns, {
force: allowExternal,
ignore: allExcludePatterns
}).then((paths) => {
if (this.options.info) {
console.log();
console.log("===== Del Webpack Plugin ===");
console.log(`${chalk2.green("Added files:")}`);
assetNames.map((name) => console.log(name));
console.log();
console.log(`${chalk2.red("Deleted files:")}`);
paths.map((name) => console.log(path2.basename(name)));
console.log("============================");
console.log();
}
});
};
compiler.hooks.done.tap("del-webpack-plugin", callback);
}
}
module.exports = DelWebpackPlugin;
106 changes: 0 additions & 106 deletions dist/index.js

This file was deleted.

1 change: 0 additions & 1 deletion example/App.js

This file was deleted.

1 change: 0 additions & 1 deletion example/App2.js

This file was deleted.

15 changes: 15 additions & 0 deletions example/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"name": "example",
"version": "1.0.0",
"main": "index.js",
"license": "MIT",
"scripts": {
"build": "NODE_ENV=production webpack --config webpack.example.js --progress"
},
"devDependencies": {
"babel-loader": "^8.1.0",
"del-webpack-plugin": "file:..",
"webpack": "^5.1.3",
"webpack-cli": "^4.1.0"
}
}
2 changes: 2 additions & 0 deletions example/src/App.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const a = 1235
console.log(a)
2 changes: 2 additions & 0 deletions example/src/App2.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const a = 1235
console.log(a)
Loading

0 comments on commit 3058cc5

Please sign in to comment.