Skip to content

Always add a hash to the name of the files created by the default assets loaders #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jul 27, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions fixtures/css/same_filename.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
h4 {
background: top left url('./../images/symfony_logo.png') no-repeat;
}

h5 {
background: top left url('./../images/same_filename/symfony_logo.png') no-repeat;
}

@font-face {
font-family: 'Roboto';
src: url('./../fonts/Roboto.woff2') format('woff2');
}

@font-face {
font-family: 'Roboto2';
src: url('./../fonts/same_filename/Roboto.woff2') format('woff2');
}
Binary file added fixtures/fonts/same_filename/Roboto.woff2
Binary file not shown.
Binary file added fixtures/images/same_filename/symfony_logo.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions lib/config-generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -127,15 +127,15 @@ class ConfigGenerator {
test: /\.(png|jpg|jpeg|gif|ico|svg)$/,
loader: 'file-loader',
options: {
name: `images/[name]${this.webpackConfig.useVersioning ? '.[hash]' : ''}.[ext]`,
name: 'images/[name].[hash:8].[ext]',
publicPath: this.webpackConfig.getRealPublicPath()
}
},
{
test: /\.(woff|woff2|ttf|eot|otf)$/,
loader: 'file-loader',
options: {
name: `fonts/[name]${this.webpackConfig.useVersioning ? '.[hash]' : ''}.[ext]`,
name: 'fonts/[name].[hash:8].[ext]',
publicPath: this.webpackConfig.getRealPublicPath()
}
},
Expand Down
84 changes: 66 additions & 18 deletions test/functional.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ describe('Functional tests using webpack', function() {
'main.js',
'font.css',
'bg.css',
'fonts/Roboto.woff2',
'images/symfony_logo.png',
'fonts/Roboto.9896f773.woff2',
'images/symfony_logo.ea1ca6f7.png',
'manifest.json'
]);

Expand All @@ -87,11 +87,11 @@ describe('Functional tests using webpack', function() {
);
webpackAssert.assertManifestPath(
'build/fonts/Roboto.woff2',
'/build/fonts/Roboto.woff2'
'/build/fonts/Roboto.9896f773.woff2'
);
webpackAssert.assertManifestPath(
'build/images/symfony_logo.png',
'/build/images/symfony_logo.png'
'/build/images/symfony_logo.ea1ca6f7.png'
);

done();
Expand Down Expand Up @@ -119,11 +119,11 @@ describe('Functional tests using webpack', function() {

webpackAssert.assertOutputFileContains(
'bg.css',
'http://localhost:8090/assets/images/symfony_logo.png'
'http://localhost:8090/assets/images/symfony_logo.ea1ca6f7.png'
);
webpackAssert.assertOutputFileContains(
'font.css',
'http://localhost:8090/assets/fonts/Roboto.woff2'
'http://localhost:8090/assets/fonts/Roboto.9896f773.woff2'
);
// manifest file has CDN in value
webpackAssert.assertManifestPath(
Expand Down Expand Up @@ -172,7 +172,7 @@ describe('Functional tests using webpack', function() {

webpackAssert.assertOutputFileContains(
'bg.css',
'http://localhost:8090/assets/images/symfony_logo.png'
'http://localhost:8090/assets/images/symfony_logo.ea1ca6f7.png'
);
// manifest file has CDN in value
webpackAssert.assertManifestPath(
Expand Down Expand Up @@ -285,20 +285,20 @@ describe('Functional tests using webpack', function() {
'0.d002be21e9bcf76057e9.js', // chunks are also versioned
'main.292c0347ed1240663cb1.js',
'h1.c84caea6dd12bba7955dee9fedd5fd03.css',
'bg.42ced8eae2254268bb3c65f1e65bd041.css',
'bg.483832e48e67e6a3b7f0ae064eadca51.css',
'manifest.json'
]
);

expect(path.join(config.outputPath, 'images')).to.be.a.directory()
.with.files([
'symfony_logo.ea1ca6f7f3719118f301a5cfcb1df3c0.png'
'symfony_logo.ea1ca6f7.png'
]
);

webpackAssert.assertOutputFileContains(
'bg.42ced8eae2254268bb3c65f1e65bd041.css',
'/build/images/symfony_logo.ea1ca6f7f3719118f301a5cfcb1df3c0.png'
'bg.483832e48e67e6a3b7f0ae064eadca51.css',
'/build/images/symfony_logo.ea1ca6f7.png'
);

done();
Expand All @@ -323,28 +323,76 @@ describe('Functional tests using webpack', function() {

expect(path.join(config.outputPath, 'images')).to.be.a.directory()
.with.files([
'symfony_logo.png'
'symfony_logo.ea1ca6f7.png'
]
);

expect(path.join(config.outputPath, 'fonts')).to.be.a.directory()
.with.files([
'Roboto.woff2'
'Roboto.9896f773.woff2'
]
);

webpackAssert.assertOutputFileContains(
'bg.css',
'/build/images/symfony_logo.png'
'/build/images/symfony_logo.ea1ca6f7.png'
);

webpackAssert.assertOutputFileContains(
'font.css',
'/build/fonts/Roboto.woff2'
'/build/fonts/Roboto.9896f773.woff2'
);

done();
});
});

it('two fonts or images with the same filename should not output a single file', (done) => {
const config = createWebpackConfig('www/build', 'dev');
config.setPublicPath('/build');
config.addStyleEntry('styles', './css/same_filename.css');
config.enableSassLoader();

testSetup.runWebpack(config, (webpackAssert) => {
expect(config.outputPath).to.be.a.directory()
.with.files([
'styles.css',
'manifest.json'
]
);

expect(path.join(config.outputPath, 'images')).to.be.a.directory()
.with.files([
'symfony_logo.ea1ca6f7.png',
'symfony_logo.f27119c2.png'
]
);

expect(path.join(config.outputPath, 'fonts')).to.be.a.directory()
.with.files([
'Roboto.9896f773.woff2',
'Roboto.3c37aa69.woff2'
]
);

webpackAssert.assertOutputFileContains(
'font.css',
'/build/fonts/Roboto.woff2'
'styles.css',
'/build/images/symfony_logo.ea1ca6f7.png'
);

webpackAssert.assertOutputFileContains(
'styles.css',
'/build/images/symfony_logo.f27119c2.png'
);

webpackAssert.assertOutputFileContains(
'styles.css',
'/build/fonts/Roboto.9896f773.woff2'
);

webpackAssert.assertOutputFileContains(
'styles.css',
'/build/fonts/Roboto.3c37aa69.woff2'
);

done();
Expand Down Expand Up @@ -687,7 +735,7 @@ module.exports = {
expect(config.outputPath).to.be.a.directory().with.deep.files([
'main.js',
'main.css',
'images/logo.png',
'images/logo.82b9c7a5.png',
'manifest.json'
]);

Expand Down