Skip to content

Commit effe965

Browse files
committed
Allow to use the [N] placeholder in copyFiles()
1 parent 5640271 commit effe965

File tree

2 files changed

+53
-1
lines changed

2 files changed

+53
-1
lines changed

lib/webpack/copy-files-loader.js

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ module.exports.default = function loader(source) {
5656
// The "source" part of the regexp is base64 encoded
5757
// in case it contains characters that don't work with
5858
// the "inline loader" syntax
59-
const pattern = new RegExp(
59+
const pattern = options.regExp || new RegExp(
6060
Buffer.from(options.patternSource, 'base64').toString(),
6161
options.patternFlags
6262
);
@@ -69,6 +69,23 @@ module.exports.default = function loader(source) {
6969
return 'module.exports = "";';
7070
}
7171

72+
// Add the "regExp" option (needed to use the [N] placeholder
73+
// see: https://github.com/webpack-contrib/file-loader#n)
74+
options.regExp = pattern;
75+
76+
// Remove copy-files-loader's custom options (in case the
77+
// file-loader starts looking for thing it doesn't expect)
78+
delete options.patternSource;
79+
delete options.patternFlags;
80+
81+
// Update loader's options.
82+
this.loaders.forEach(loader => {
83+
if (__filename === loader.path) {
84+
loader.options = options;
85+
delete loader.query;
86+
}
87+
});
88+
7289
// If everything is OK, let the file-loader do the copy
7390
return fileLoader.bind(this)(source);
7491
};

test/functional.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2206,6 +2206,41 @@ module.exports = {
22062206
done();
22072207
});
22082208
});
2209+
2210+
it('Can use the "[N]" placeholder', (done) => {
2211+
const config = createWebpackConfig('www/build', 'production');
2212+
config.addEntry('main', './js/no_require');
2213+
config.setPublicPath('/build');
2214+
config.copyFiles({
2215+
from: './images',
2216+
pattern: /(symfony)_(logo)\.png/,
2217+
to: '[path][2]_[1].[ext]',
2218+
includeSubdirectories: false
2219+
});
2220+
2221+
testSetup.runWebpack(config, (webpackAssert) => {
2222+
expect(config.outputPath).to.be.a.directory()
2223+
.with.files([
2224+
'entrypoints.json',
2225+
'runtime.js',
2226+
'main.js',
2227+
'manifest.json',
2228+
'logo_symfony.png'
2229+
]);
2230+
2231+
webpackAssert.assertManifestPath(
2232+
'build/main.js',
2233+
'/build/main.js'
2234+
);
2235+
2236+
webpackAssert.assertManifestPath(
2237+
'build/symfony_logo.png',
2238+
'/build/logo_symfony.png'
2239+
);
2240+
2241+
done();
2242+
});
2243+
});
22092244
});
22102245

22112246
describe('entrypoints.json & splitChunks()', () => {

0 commit comments

Comments
 (0)