Skip to content

Commit

Permalink
allowExternal renamed to dangerouslyAllowCleanPatternsOutsideProject
Browse files Browse the repository at this point in the history
  • Loading branch information
chrisblossom committed Mar 4, 2019
1 parent 8167328 commit 68893e8
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 11 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -104,7 +104,7 @@ new CleanWebpackPlugin({
*
* default: false
*/
allowExternal: true,
dangerouslyAllowCleanPatternsOutsideProject: true,
});
```

Expand Down
18 changes: 13 additions & 5 deletions src/clean-webpack-plugin.test.ts
Expand Up @@ -581,8 +581,8 @@ describe('customPatterns option', () => {
});
});

describe('allowExternal option', () => {
test('will not files outside of process.cwd(), allowExternal: false (by default)', async () => {
describe('dangerouslyAllowCleanPatternsOutsideProject option', () => {
test('will not files outside of process.cwd(), dangerouslyAllowCleanPatternsOutsideProject: false (by default)', async () => {
process.chdir(cwd);
createSrcBundle(1);

Expand All @@ -607,11 +607,11 @@ describe('allowExternal option', () => {
});

await expect(compiler.run()).rejects.toThrowErrorMatchingInlineSnapshot(
`"clean-webpack-plugin: Cannot delete files/folders outside the current working directory. Can be overridden with the \`allowExternal\` option."`,
`"clean-webpack-plugin: Cannot delete files/folders outside the current working directory. Can be overridden with the \`dangerouslyAllowCleanPatternsOutsideProject\` option."`,
);
});

test('removes files outside of process.cwd() with allowExternal: true', async () => {
test('removes files outside of process.cwd() with dangerouslyAllowCleanPatternsOutsideProject: true', async () => {
process.chdir(cwd);
createSrcBundle(1);

Expand All @@ -622,7 +622,7 @@ describe('allowExternal option', () => {
expect(initialOutsideFiles).toEqual(['outside-file.js']);

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

Expand Down Expand Up @@ -880,4 +880,12 @@ describe('detect old options', () => {
https://github.com/johnagan/clean-webpack-plugin#options-and-defaults-optional"
`);
});

test('allowExternal', () => {
expect(() =>
CleanWebpackPlugin({ allowExternal: true }),
).toThrowErrorMatchingInlineSnapshot(
`"clean-webpack-plugin: \`allowExternal\` option no longer supported. Use \`dangerouslyAllowCleanPatternsOutsideProject\`"`,
);
});
});
19 changes: 14 additions & 5 deletions src/clean-webpack-plugin.ts
Expand Up @@ -34,15 +34,15 @@ interface Options {
/**
* Allow clean patterns outside of process.cwd()
*/
allowExternal: boolean;
dangerouslyAllowCleanPatternsOutsideProject: boolean;
}

class CleanWebpackPlugin {
private readonly dry: boolean;
private readonly verbose: boolean;
private readonly customPatterns: string[];
private readonly initialPatterns: string[];
private readonly allowExternal: boolean;
private readonly dangerouslyAllowCleanPatternsOutsideProject: boolean;
private currentAssets: string[];
private initialClean: boolean;
private outputPath: string;
Expand All @@ -53,6 +53,13 @@ class CleanWebpackPlugin {
https://github.com/johnagan/clean-webpack-plugin#options-and-defaults-optional`);
}

// @ts-ignore
if (options.allowExternal) {
throw new Error(
'clean-webpack-plugin: `allowExternal` option no longer supported. Use `dangerouslyAllowCleanPatternsOutsideProject`',
);
}

this.dry = options.dry === true || false;
this.verbose =
options.dry === true || options.verbose === true || false;
Expand All @@ -65,7 +72,9 @@ class CleanWebpackPlugin {
? options.initialPatterns
: ['**'];

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

/**
* Store webpack build assets
Expand Down Expand Up @@ -198,7 +207,7 @@ class CleanWebpackPlugin {
removeFiles(patterns: string[]) {
try {
const deleted = del.sync(patterns, {
force: this.allowExternal,
force: this.dangerouslyAllowCleanPatternsOutsideProject,
// Change context to build directory
cwd: this.outputPath,
dryRun: this.dry,
Expand Down Expand Up @@ -232,7 +241,7 @@ class CleanWebpackPlugin {

if (needsForce) {
const message =
'clean-webpack-plugin: Cannot delete files/folders outside the current working directory. Can be overridden with the `allowExternal` option.';
'clean-webpack-plugin: Cannot delete files/folders outside the current working directory. Can be overridden with the `dangerouslyAllowCleanPatternsOutsideProject` option.';

throw new Error(message);
}
Expand Down

0 comments on commit 68893e8

Please sign in to comment.