Skip to content

Commit a9ae08d

Browse files
committed
Use versioned filenames during copying if the "to" option isn't set
1 parent 7878f4e commit a9ae08d

File tree

5 files changed

+64
-8
lines changed

5 files changed

+64
-8
lines changed

index.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -437,9 +437,6 @@ class Encore {
437437
* Notes:
438438
* * No transformation is applied to the copied files (for instance
439439
* copying a CSS file won't minify it)
440-
* * By default files won't be versioned even if enableVersioning()
441-
* has been called. In order to add a hash to your filenames you
442-
* must use the "[hash]" placeholder in the "to" option (see below)
443440
*
444441
* Supported options:
445442
* * {string} from (mandatory)

lib/WebpackConfig.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -378,7 +378,7 @@ class WebpackConfig {
378378
const defaultConfig = {
379379
from: null,
380380
pattern: /.*/,
381-
to: '[path][name].[ext]',
381+
to: null,
382382
includeSubdirectories: true
383383
};
384384

lib/config-generator.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,12 @@ class ConfigGenerator {
151151
entry.from
152152
);
153153

154-
const requireContextParam = `!${require.resolve('file-loader')}?context=${copyFrom}&name=${entry.to}!${copyFrom}`;
154+
let copyTo = entry.to;
155+
if (copyTo === null) {
156+
copyTo = this.webpackConfig.useVersioning ? '[path][name].[hash:8].[ext]' : '[path][name].[ext]';
157+
}
158+
159+
const requireContextParam = `!${require.resolve('file-loader')}?context=${copyFrom}&name=${copyTo}!${copyFrom}`;
155160

156161
return buffer + `
157162
const context_${index} = require.context(

test/WebpackConfig.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ describe('WebpackConfig object', () => {
360360
});
361361

362362
describe('copyFiles', () => {
363-
it('Calling it add files to be copied', () => {
363+
it('Calling it adds files to be copied', () => {
364364
const config = createConfig();
365365

366366
// With multiple config objects
@@ -375,7 +375,7 @@ describe('WebpackConfig object', () => {
375375
expect(config.copyFilesConfigs).to.deep.equal([{
376376
from: './foo',
377377
pattern: /.*/,
378-
to: '[path][name].[ext]',
378+
to: null,
379379
includeSubdirectories: true
380380
}, {
381381
from: './bar',
@@ -385,7 +385,7 @@ describe('WebpackConfig object', () => {
385385
}, {
386386
from: './baz',
387387
pattern: /.*/,
388-
to: '[path][name].[ext]',
388+
to: null,
389389
includeSubdirectories: true
390390
}]);
391391
});

test/functional.js

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1545,6 +1545,60 @@ module.exports = {
15451545
done();
15461546
});
15471547
});
1548+
1549+
it('Copy with versioning enabled', (done) => {
1550+
const config = createWebpackConfig('www/build', 'production');
1551+
config.addEntry('main', './js/no_require');
1552+
config.setPublicPath('/build');
1553+
config.enableVersioning(true);
1554+
config.copyFiles([{
1555+
from: './images',
1556+
includeSubdirectories: false
1557+
}, {
1558+
from: './fonts',
1559+
to: 'assets/[path][name].[ext]',
1560+
includeSubdirectories: false
1561+
}]);
1562+
1563+
testSetup.runWebpack(config, (webpackAssert) => {
1564+
expect(config.outputPath).to.be.a.directory()
1565+
.with.files([
1566+
'entrypoints.json',
1567+
'runtime.d41d8cd9.js',
1568+
'main.1172d977.js',
1569+
'manifest.json',
1570+
'symfony_logo.ea1ca6f7.png',
1571+
'symfony_logo_alt.f27119c2.png',
1572+
]);
1573+
1574+
expect(path.join(config.outputPath, 'assets')).to.be.a.directory()
1575+
.with.files([
1576+
'Roboto.woff2',
1577+
]);
1578+
1579+
webpackAssert.assertManifestPath(
1580+
'build/main.js',
1581+
'/build/main.1172d977.js'
1582+
);
1583+
1584+
webpackAssert.assertManifestPath(
1585+
'build/symfony_logo.png',
1586+
'/build/symfony_logo.ea1ca6f7.png'
1587+
);
1588+
1589+
webpackAssert.assertManifestPath(
1590+
'build/symfony_logo_alt.png',
1591+
'/build/symfony_logo_alt.f27119c2.png'
1592+
);
1593+
1594+
webpackAssert.assertManifestPath(
1595+
'build/assets/Roboto.woff2',
1596+
'/build/assets/Roboto.woff2'
1597+
);
1598+
1599+
done();
1600+
});
1601+
});
15481602
});
15491603

15501604
describe('entrypoints.json', () => {

0 commit comments

Comments
 (0)