Skip to content

Commit

Permalink
initialPatterns renamed to cleanOnceBeforeBuildPatterns, customPatter…
Browse files Browse the repository at this point in the history
…ns renamed to cleanAfterEveryBuildPatterns
  • Loading branch information
chrisblossom committed Mar 4, 2019
1 parent 5e5a519 commit 6d6b341
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 38 deletions.
8 changes: 3 additions & 5 deletions README.md
Expand Up @@ -87,20 +87,18 @@ new CleanWebpackPlugin({
*
* default: ['**']
*/
initialPatterns: ['**', '!static-files*'],
initialPatterns: [], // disables initialPatterns
cleanOnceBeforeBuildPatterns: ['**', '!static-files*'],
cleanOnceBeforeBuildPatterns: [], // disables cleanOnceBeforeBuildPatterns

/**
* Custom pattern matching
*
* Removes files after every build (including watch mode) that match this pattern.
* Used for files that are not created directly by Webpack.
*
* Use !negative patterns to exclude files
*
* default: disabled
*/
customPatterns: ['static*.*', '!static1.js'],
cleanAfterEveryBuildPatterns: ['static*.*', '!static1.js'],

/**
* Allow clean patterns outside of process.cwd()
Expand Down
34 changes: 17 additions & 17 deletions src/clean-webpack-plugin.test.ts
Expand Up @@ -269,13 +269,13 @@ test('removes map files', async () => {
]);
});

describe('initialPatterns option', () => {
describe('cleanOnceBeforeBuildPatterns option', () => {
test('does nothing when nothing changes or files added but not removed', async () => {
createSrcBundle(1);
createStaticFiles();

const cleanWebpackPlugin = new CleanWebpackPlugin({
initialPatterns: [],
cleanOnceBeforeBuildPatterns: [],
});

const removeFilesSpy = jest.spyOn(cleanWebpackPlugin, 'removeFiles');
Expand Down Expand Up @@ -327,7 +327,7 @@ describe('initialPatterns option', () => {
expect(removeFilesSpy).not.toHaveBeenCalled();
});

test('only calls once initialPatterns once', async () => {
test('only calls once cleanOnceBeforeBuildPatterns once', async () => {
createSrcBundle(1);
createStaticFiles();

Expand All @@ -339,7 +339,7 @@ describe('initialPatterns option', () => {
]);

const cleanWebpackPlugin = new CleanWebpackPlugin({
initialPatterns: ['**'],
cleanOnceBeforeBuildPatterns: ['**'],
});

const compiler = webpack({
Expand Down Expand Up @@ -374,7 +374,7 @@ describe('initialPatterns option', () => {
]);
});

test('overrides default initialPatterns', async () => {
test('overrides default cleanOnceBeforeBuildPatterns', async () => {
createSrcBundle(1);
createStaticFiles();

Expand All @@ -386,7 +386,7 @@ describe('initialPatterns option', () => {
]);

const cleanWebpackPlugin = new CleanWebpackPlugin({
initialPatterns: ['static2.txt'],
cleanOnceBeforeBuildPatterns: ['static2.txt'],
});

const compiler = webpack({
Expand Down Expand Up @@ -437,7 +437,7 @@ describe('initialPatterns option', () => {
]);

const cleanWebpackPlugin = new CleanWebpackPlugin({
initialPatterns: ['**', '!static2.*'],
cleanOnceBeforeBuildPatterns: ['**', '!static2.*'],
});

const compiler = webpack({
Expand Down Expand Up @@ -475,7 +475,7 @@ describe('initialPatterns option', () => {
]);
});

test('handles the initialPatterns outside of build directory', async () => {
test('handles the cleanOnceBeforeBuildPatterns outside of build directory', async () => {
createSrcBundle(1);

const outsideDistPath = 'build';
Expand All @@ -485,7 +485,7 @@ describe('initialPatterns option', () => {
expect(initialOutsideFiles).toEqual(['outside-file.js']);

const cleanWebpackPlugin = new CleanWebpackPlugin({
initialPatterns: [path.join(sandbox.dir, 'build/**')],
cleanOnceBeforeBuildPatterns: [path.join(sandbox.dir, 'build/**')],
});

const compiler = webpack({
Expand All @@ -504,13 +504,13 @@ describe('initialPatterns option', () => {
});
});

describe('customPatterns option', () => {
test('removes with customPatterns', async () => {
describe('cleanAfterEveryBuildPatterns option', () => {
test('removes with cleanAfterEveryBuildPatterns', async () => {
createSrcBundle(2);
createStaticFiles();

const cleanWebpackPlugin = new CleanWebpackPlugin({
customPatterns: ['static*.*', '!static1.js'],
cleanAfterEveryBuildPatterns: ['static*.*', '!static1.js'],
});

const compiler = webpack({
Expand Down Expand Up @@ -551,7 +551,7 @@ describe('customPatterns option', () => {
]);
});

test('handles the customPatterns outside of webpack output directory', async () => {
test('handles the cleanAfterEveryBuildPatterns outside of webpack output directory', async () => {
createSrcBundle(1);

const outsideDistPath = 'build';
Expand All @@ -561,7 +561,7 @@ describe('customPatterns option', () => {
expect(initialOutsideFiles).toEqual(['outside-file.js']);

const cleanWebpackPlugin = new CleanWebpackPlugin({
customPatterns: [path.join(sandbox.dir, 'build/**')],
cleanAfterEveryBuildPatterns: [path.join(sandbox.dir, 'build/**')],
});

const compiler = webpack({
Expand Down Expand Up @@ -592,8 +592,8 @@ describe('dangerouslyAllowCleanPatternsOutsideProject option', () => {
expect(initialOutsideFiles).toEqual(['outside-file.js']);

const cleanWebpackPlugin = new CleanWebpackPlugin({
// Use initialPatterns because webpack 2/3 doesn't handle errors in done lifecycle correctly
initialPatterns: [path.join(sandbox.dir, 'build/**')],
// Use cleanOnceBeforeBuildPatterns because webpack 2/3 doesn't handle errors in done lifecycle correctly
cleanOnceBeforeBuildPatterns: [path.join(sandbox.dir, 'build/**')],
});

const compiler = webpack({
Expand Down Expand Up @@ -624,7 +624,7 @@ describe('dangerouslyAllowCleanPatternsOutsideProject option', () => {
const cleanWebpackPlugin = new CleanWebpackPlugin({
dangerouslyAllowCleanPatternsOutsideProject: true,
dry: false,
customPatterns: [path.join(sandbox.dir, 'build/**')],
cleanAfterEveryBuildPatterns: [path.join(sandbox.dir, 'build/**')],
});

expect(consoleSpy.mock.calls).toEqual([]);
Expand Down
37 changes: 21 additions & 16 deletions src/clean-webpack-plugin.ts
Expand Up @@ -19,16 +19,14 @@ interface Options {
verbose: boolean;

/**
* Custom pattern matching
*
* Removes files after every build (including watch mode) that match this pattern.
* Used for files that are not created directly by Webpack.
*
* Use !negative patterns to exclude files
*
* default: disabled
*/
customPatterns: string[];
cleanAfterEveryBuildPatterns: string[];

/**
* Removes files once prior to Webpack compilation
Expand All @@ -38,7 +36,7 @@ interface Options {
*
* default: ['**']
*/
initialPatterns: string[];
cleanOnceBeforeBuildPatterns: string[];

/**
* Allow clean patterns outside of process.cwd()
Expand All @@ -53,8 +51,8 @@ interface Options {
class CleanWebpackPlugin {
private readonly dry: boolean;
private readonly verbose: boolean;
private readonly customPatterns: string[];
private readonly initialPatterns: string[];
private readonly cleanAfterEveryBuildPatterns: string[];
private readonly cleanOnceBeforeBuildPatterns: string[];
private readonly dangerouslyAllowCleanPatternsOutsideProject: boolean;
private currentAssets: string[];
private initialClean: boolean;
Expand Down Expand Up @@ -96,12 +94,16 @@ class CleanWebpackPlugin {

this.verbose = this.dry === true || options.verbose === true || false;

this.customPatterns = Array.isArray(options.customPatterns)
? options.customPatterns
this.cleanAfterEveryBuildPatterns = Array.isArray(
options.cleanAfterEveryBuildPatterns,
)
? options.cleanAfterEveryBuildPatterns
: [];

this.initialPatterns = Array.isArray(options.initialPatterns)
? options.initialPatterns
this.cleanOnceBeforeBuildPatterns = Array.isArray(
options.cleanOnceBeforeBuildPatterns,
)
? options.cleanOnceBeforeBuildPatterns
: ['**'];

/**
Expand All @@ -110,7 +112,7 @@ class CleanWebpackPlugin {
this.currentAssets = [];

/**
* Only used with initialPatterns
* Only used with cleanOnceBeforeBuildPatterns
*/
this.initialClean = false;

Expand Down Expand Up @@ -141,7 +143,7 @@ class CleanWebpackPlugin {
*/
const hooks = compiler.hooks;

if (this.initialPatterns.length !== 0) {
if (this.cleanOnceBeforeBuildPatterns.length !== 0) {
if (hooks) {
hooks.compile.tap('clean-webpack-plugin', () => {
this.handleInitial();
Expand Down Expand Up @@ -178,7 +180,7 @@ class CleanWebpackPlugin {

this.initialClean = true;

this.removeFiles(this.initialPatterns);
this.removeFiles(this.cleanOnceBeforeBuildPatterns);
}

handleDone(stats: Stats) {
Expand Down Expand Up @@ -220,16 +222,19 @@ class CleanWebpackPlugin {
this.currentAssets = assets.sort();

/**
* Do nothing if there aren't any files to delete and customPatterns is not defined
* Do nothing if there aren't any files to delete and cleanAfterEveryBuildPatterns is not defined
*/
if (staleFiles.length === 0 && this.customPatterns.length === 0) {
if (
staleFiles.length === 0 &&
this.cleanAfterEveryBuildPatterns.length === 0
) {
return;
}

/**
* Merge customPatters with stale files.
*/
this.removeFiles([...staleFiles, ...this.customPatterns]);
this.removeFiles([...staleFiles, ...this.cleanAfterEveryBuildPatterns]);
}

removeFiles(patterns: string[]) {
Expand Down

0 comments on commit 6d6b341

Please sign in to comment.