Skip to content

Commit 28086b1

Browse files
committed
bug #649 Allow to use the [N] placeholder in copyFiles() (Lyrkan)
This PR was merged into the master branch. Discussion ---------- Allow to use the [N] placeholder in copyFiles() This PR should fix #647 by passing the `regExp` option to the `file-loader` used by `Encore.copyFiles()`. It's a bit hacky since it changes the loader's options from the loader itself, but we should probably be fine for a while (I also tested it with `[email protected]` and the latest version of `[email protected]`). Commits ------- b8ac398 Allow to use the [N] placeholder in copyFiles()
2 parents 19e6c51 + b8ac398 commit 28086b1

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

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

0 commit comments

Comments
 (0)