Skip to content

Commit 487d6ef

Browse files
committed
Allow to set a custom context in copyFiles
1 parent f35841a commit 487d6ef

File tree

5 files changed

+74
-5
lines changed

5 files changed

+74
-5
lines changed

index.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -493,6 +493,13 @@ class Encore {
493493
* { from: './txt', pattern: /\.txt$/ },
494494
* ]);
495495
*
496+
* // Set the context path
497+
* Encore.copyFiles({
498+
* from: './assets/images',
499+
* to: '[path][name].[hash:8].[ext]',
500+
* context: './assets'
501+
* });
502+
*
496503
* Notes:
497504
* * No transformation is applied to the copied files (for instance
498505
* copying a CSS file won't minify it)
@@ -508,6 +515,8 @@ class Encore {
508515
* https://github.com/webpack-contrib/file-loader#placeholders
509516
* * {boolean} includeSubdirectories (default: true)
510517
* Whether or not the copy should include subdirectories.
518+
* * {string} context (default: path of the source directory)
519+
* The context to use as a root path when copying files.
511520
*
512521
* @param {object|Array} configs
513522
* @returns {Encore}

lib/WebpackConfig.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,8 @@ class WebpackConfig {
437437
from: null,
438438
pattern: /.*/,
439439
to: null,
440-
includeSubdirectories: true
440+
includeSubdirectories: true,
441+
context: null,
441442
};
442443

443444
for (const config of configs) {

lib/config-generator.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -183,7 +183,8 @@ class ConfigGenerator {
183183
copyTo = this.webpackConfig.useVersioning ? '[path][name].[hash:8].[ext]' : '[path][name].[ext]';
184184
}
185185

186-
const requireContextParam = `!${require.resolve('file-loader')}?context=${copyFrom}&name=${copyTo}!${copyFrom}`;
186+
const copyContext = entry.context ? path.resolve(this.webpackConfig.getContext(), entry.context) : copyFrom;
187+
const requireContextParam = `!${require.resolve('file-loader')}?context=${copyContext}&name=${copyTo}!${copyFrom}`;
187188

188189
return buffer + `
189190
const context_${index} = require.context(

test/WebpackConfig.js

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -394,17 +394,20 @@ describe('WebpackConfig object', () => {
394394
from: './foo',
395395
pattern: /.*/,
396396
to: null,
397-
includeSubdirectories: true
397+
includeSubdirectories: true,
398+
context: null,
398399
}, {
399400
from: './bar',
400401
pattern: /abc/,
401402
to: 'bar',
402-
includeSubdirectories: false
403+
includeSubdirectories: false,
404+
context: null,
403405
}, {
404406
from: './baz',
405407
pattern: /.*/,
406408
to: null,
407-
includeSubdirectories: true
409+
includeSubdirectories: true,
410+
context: null,
408411
}]);
409412
});
410413

test/functional.js

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,6 +1823,61 @@ module.exports = {
18231823
done();
18241824
});
18251825
});
1826+
1827+
it('Copy with a custom context', (done) => {
1828+
const config = createWebpackConfig('www/build', 'production');
1829+
config.addEntry('main', './js/no_require');
1830+
config.setPublicPath('/build');
1831+
config.copyFiles({
1832+
from: './images',
1833+
to: '[path][name].[hash:8].[ext]',
1834+
includeSubdirectories: true,
1835+
context: './',
1836+
});
1837+
1838+
testSetup.runWebpack(config, (webpackAssert) => {
1839+
expect(config.outputPath).to.be.a.directory()
1840+
.with.files([
1841+
'entrypoints.json',
1842+
'runtime.js',
1843+
'main.js',
1844+
'manifest.json',
1845+
]);
1846+
1847+
expect(path.join(config.outputPath, 'images')).to.be.a.directory()
1848+
.with.files([
1849+
'symfony_logo.ea1ca6f7.png',
1850+
'symfony_logo_alt.f27119c2.png',
1851+
]);
1852+
1853+
expect(path.join(config.outputPath, 'images', 'same_filename')).to.be.a.directory()
1854+
.with.files([
1855+
'symfony_logo.f27119c2.png',
1856+
]);
1857+
1858+
webpackAssert.assertManifestPath(
1859+
'build/main.js',
1860+
'/build/main.js'
1861+
);
1862+
1863+
webpackAssert.assertManifestPath(
1864+
'build/images/symfony_logo.png',
1865+
'/build/images/symfony_logo.ea1ca6f7.png'
1866+
);
1867+
1868+
webpackAssert.assertManifestPath(
1869+
'build/images/symfony_logo_alt.png',
1870+
'/build/images/symfony_logo_alt.f27119c2.png'
1871+
);
1872+
1873+
webpackAssert.assertManifestPath(
1874+
'build/images/same_filename/symfony_logo.png',
1875+
'/build/images/same_filename/symfony_logo.f27119c2.png'
1876+
);
1877+
1878+
done();
1879+
});
1880+
});
18261881
});
18271882

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

0 commit comments

Comments
 (0)