Skip to content

Commit

Permalink
require dangerouslyAllowCleanPatternsOutsideProject to explicitly set…
Browse files Browse the repository at this point in the history
… dry to false
  • Loading branch information
chrisblossom committed Mar 4, 2019
1 parent 68893e8 commit d2ac516
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 9 deletions.
4 changes: 2 additions & 2 deletions README.md
Expand Up @@ -84,7 +84,6 @@ new CleanWebpackPlugin({
*/
initialPatterns: ['**', '!static-files*'],
initialPatterns: [], // disables initialPatterns
cleanBeforeBuildPatterns: [],

/**
* Custom pattern matching
Expand All @@ -97,11 +96,12 @@ new CleanWebpackPlugin({
* default: disabled
*/
customPatterns: ['static*.*', '!static1.js'],
cleanAfterBuildPatterns: [],

/**
* Allow clean patterns outside of process.cwd()
*
* requires dry option to be explicitly set
*
* default: false
*/
dangerouslyAllowCleanPatternsOutsideProject: true,
Expand Down
30 changes: 30 additions & 0 deletions src/clean-webpack-plugin.test.ts
Expand Up @@ -623,9 +623,12 @@ describe('dangerouslyAllowCleanPatternsOutsideProject option', () => {

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

expect(consoleSpy.mock.calls).toEqual([]);

const compiler = webpack({
entry: entryFileFull,
output: {
Expand All @@ -640,6 +643,33 @@ describe('dangerouslyAllowCleanPatternsOutsideProject option', () => {

expect(sandbox.getFileListSync(outsideDistPath)).toEqual([]);
});

test('dangerouslyAllowCleanPatternsOutsideProject: true require dry to be explicitly set', async () => {
const cleanWebpackPlugin = new CleanWebpackPlugin({
dangerouslyAllowCleanPatternsOutsideProject: true,
});

expect(cleanWebpackPlugin.dry).toEqual(true);
expect(cleanWebpackPlugin.verbose).toEqual(true);
expect(consoleSpy.mock.calls).toMatchInlineSnapshot(`
Array [
Array [
"clean-webpack-plugin: dangerouslyAllowCleanPatternsOutsideProject requires dry: false to be explicitly set. Enabling dry mode",
],
]
`);
});

test('dangerouslyAllowCleanPatternsOutsideProject: true dry: true', async () => {
const cleanWebpackPlugin = new CleanWebpackPlugin({
dangerouslyAllowCleanPatternsOutsideProject: true,
dry: true,
});

expect(cleanWebpackPlugin.dry).toEqual(true);
expect(cleanWebpackPlugin.verbose).toEqual(true);
expect(consoleSpy.mock.calls).toEqual([]);
});
});

describe('dry option', () => {
Expand Down
29 changes: 22 additions & 7 deletions src/clean-webpack-plugin.ts
Expand Up @@ -60,9 +60,28 @@ class CleanWebpackPlugin {
);
}

this.dry = options.dry === true || false;
this.verbose =
options.dry === true || options.verbose === true || false;
if (
options.dangerouslyAllowCleanPatternsOutsideProject === true &&
options.dry !== true &&
options.dry !== false
) {
// eslint-disable-next-line no-console
console.warn(
'clean-webpack-plugin: dangerouslyAllowCleanPatternsOutsideProject requires dry: false to be explicitly set. Enabling dry mode',
);
}

this.dangerouslyAllowCleanPatternsOutsideProject =
options.dangerouslyAllowCleanPatternsOutsideProject === true ||
false;

this.dry =
options.dry === true || options.dry === false
? options.dry
: this.dangerouslyAllowCleanPatternsOutsideProject === true ||
false;

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

this.customPatterns = Array.isArray(options.customPatterns)
? options.customPatterns
Expand All @@ -72,10 +91,6 @@ class CleanWebpackPlugin {
? options.initialPatterns
: ['**'];

this.dangerouslyAllowCleanPatternsOutsideProject =
options.dangerouslyAllowCleanPatternsOutsideProject === true ||
false;

/**
* Store webpack build assets
*/
Expand Down

0 comments on commit d2ac516

Please sign in to comment.