Skip to content

Commit d07965c

Browse files
committed
Add some checks to the 'pattern' option of Encore.copyFiles()
1 parent 853e0f9 commit d07965c

File tree

4 files changed

+32
-5
lines changed

4 files changed

+32
-5
lines changed

index.js

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -500,8 +500,9 @@ class Encore {
500500
* Supported options:
501501
* * {string} from (mandatory)
502502
* The path of the source directory (mandatory)
503-
* * {RegExp} pattern (default: all files)
504-
* A pattern that the filenames must match in order to be copied
503+
* * {RegExp|string} pattern (default: all files)
504+
* A regular expression (or a string containing one) that
505+
* the filenames must match in order to be copied
505506
* * {string} to (default: [path][name].[ext])
506507
* Where the files must be copied to. You can add all the
507508
* placeholders supported by the file-loader.

lib/WebpackConfig.js

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,20 @@ class WebpackConfig {
451451
}
452452
}
453453

454+
if (typeof config.pattern !== 'undefined' && !(config.pattern instanceof RegExp)) {
455+
let validPattern = false;
456+
if (typeof config.pattern === 'string') {
457+
const regexPattern = /^\/(.*)\/([a-z]*)?$/;
458+
if (regexPattern.test(config.pattern)) {
459+
validPattern = true;
460+
}
461+
}
462+
463+
if (!validPattern) {
464+
throw new Error(`Invalid pattern "${config.pattern}" passed to copyFiles(). Make sure it contains a valid regular expression.`);
465+
}
466+
}
467+
454468
this.copyFilesConfigs.push(
455469
Object.assign({}, defaultConfig, config)
456470
);

lib/config-generator.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,7 @@ class ConfigGenerator {
188188
return buffer + `
189189
const context_${index} = require.context(
190190
'${stringEscaper(requireContextParam)}',
191-
${entry.includeSubdirectories},
191+
${!!entry.includeSubdirectories},
192192
${entry.pattern}
193193
);
194194
context_${index}.keys().forEach(context_${index});

test/WebpackConfig.js

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@ describe('WebpackConfig object', () => {
384384
// With multiple config objects
385385
config.copyFiles([
386386
{ from: './foo', pattern: /.*/ },
387-
{ from: './bar', pattern: /abc/, to: 'bar', includeSubdirectories: false },
387+
{ from: './bar', pattern: '/abc/', to: 'bar', includeSubdirectories: false },
388388
]);
389389

390390
// With a single config object
@@ -397,7 +397,7 @@ describe('WebpackConfig object', () => {
397397
includeSubdirectories: true
398398
}, {
399399
from: './bar',
400-
pattern: /abc/,
400+
pattern: '/abc/',
401401
to: 'bar',
402402
includeSubdirectories: false
403403
}, {
@@ -439,6 +439,18 @@ describe('WebpackConfig object', () => {
439439
config.copyFiles({ from: 'images', foo: 'foo' });
440440
}).to.throw('Invalid config option "foo"');
441441
});
442+
443+
it('Calling it with an invalid "pattern" option', () => {
444+
const config = createConfig();
445+
446+
expect(() => {
447+
config.copyFiles({ from: 'images', pattern: true });
448+
}).to.throw('Invalid pattern "true"');
449+
450+
expect(() => {
451+
config.copyFiles({ from: 'images', pattern: 'foo' });
452+
}).to.throw('Invalid pattern "foo"');
453+
});
442454
});
443455

444456
describe('autoProvideVariables', () => {

0 commit comments

Comments
 (0)