Skip to content

Commit 9801b84

Browse files
committed
bug #619 Fix createSharedEntry(...) when using query string versioning strategy (Lyrkan)
This PR was merged into the master branch. Discussion ---------- Fix createSharedEntry(...) when using query string versioning strategy This PR fixes #618 by correctly filtering the JS files of the shared entry (in the `SharedEntryConcatPlugin`) when versioning is handled in a query string. Commits ------- 0f63369 Fix createSharedEntry(...) when using query string versioning strategy
2 parents b61cf71 + 0f63369 commit 9801b84

File tree

4 files changed

+92
-2
lines changed

4 files changed

+92
-2
lines changed

lib/utils/get-file-extension.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <fabien@symfony.com>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const path = require('path');
13+
const url = require('url');
14+
15+
module.exports = function(filename) {
16+
const parsedFilename = new url.URL(filename, 'http://foo');
17+
const extension = path.extname(parsedFilename.pathname);
18+
return extension ? extension.slice(1) : '';
19+
};

lib/webpack/shared-entry-concat-plugin.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
'use strict';
1111

1212
const sharedEntryTmpName = require('../utils/sharedEntryTmpName');
13+
const getFileExtension = require('../utils/get-file-extension');
1314
const RawSource = require('webpack-sources/lib/RawSource');
1415

1516
function SharedEntryConcatPlugin(sharedEntryName) {
@@ -26,7 +27,8 @@ function getChunkFilename(compilation, chunkName) {
2627
}
2728

2829
const jsFiles = chunk.files.filter(filename => {
29-
return /\.js$/.test(filename) && !additionalChunkAssets.includes(filename);
30+
const fileExtension = getFileExtension(filename);
31+
return /^js$/.test(fileExtension) && !additionalChunkAssets.includes(filename);
3032
});
3133

3234
if (jsFiles.length !== 1) {

test/functional.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -854,7 +854,7 @@ describe('Functional tests using webpack', function() {
854854
});
855855
});
856856

857-
it('createdSharedEntry() works with versioning', (done) => {
857+
it('createdSharedEntry() works with default versioning strategy', (done) => {
858858
const config = createWebpackConfig('www/build', 'dev');
859859
config.setPublicPath('/build');
860860
config.addEntry('main', ['./js/no_require', './js/code_splitting', './js/arrow_function', './js/print_to_app']);
@@ -878,6 +878,33 @@ describe('Functional tests using webpack', function() {
878878
});
879879
});
880880

881+
it('createdSharedEntry() works with query string versioning strategy', (done) => {
882+
const config = createWebpackConfig('www/build', 'dev');
883+
config.setPublicPath('/build');
884+
config.addEntry('main', ['./js/no_require', './js/code_splitting', './js/arrow_function', './js/print_to_app']);
885+
config.addEntry('other', ['./js/no_require', './css/h1_style.css']);
886+
config.createSharedEntry('shared', './js/shared_example');
887+
config.configureFilenames({
888+
js: '[name].js?[contenthash:8]',
889+
css: '[name].css?[contenthash:8]',
890+
});
891+
892+
testSetup.runWebpack(config, (webpackAssert) => {
893+
testSetup.requestTestPage(
894+
path.join(config.getContext(), 'www'),
895+
[
896+
convertToManifestPath('build/runtime.js', config),
897+
convertToManifestPath('build/shared.js', config),
898+
],
899+
(browser) => {
900+
// assert that the javascript brought into shared is executed
901+
browser.assert.text('#app', 'Welcome to Encore!');
902+
done();
903+
}
904+
);
905+
});
906+
});
907+
881908
it('createdSharedEntry() works with source maps enabled', (done) => {
882909
const config = createWebpackConfig('www/build', 'dev');
883910
config.setPublicPath('/build');

test/utils/get-file-extension.js

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
/*
2+
* This file is part of the Symfony Webpack Encore package.
3+
*
4+
* (c) Fabien Potencier <fabien@symfony.com>
5+
*
6+
* For the full copyright and license information, please view the LICENSE
7+
* file that was distributed with this source code.
8+
*/
9+
10+
'use strict';
11+
12+
const expect = require('chai').expect;
13+
const getFileExtension = require('../../lib/utils/get-file-extension');
14+
15+
describe('get-file-extension', () => {
16+
it('returns the extension of simple filenames', () => {
17+
expect(getFileExtension('foo.js')).to.equal('js');
18+
expect(getFileExtension('foo-bar.txt')).to.equal('txt');
19+
expect(getFileExtension('foo.bar.baz')).to.equal('baz');
20+
});
21+
22+
it('returns an empty string for files with no extension', () => {
23+
expect(getFileExtension('foo')).to.equal('');
24+
expect(getFileExtension('foo-bar')).to.equal('');
25+
});
26+
27+
it('returns the extension of a file from an absolute path', () => {
28+
expect(getFileExtension('/home/foo/bar.js')).to.equal('js');
29+
expect(getFileExtension('C:\\home\\foo\\bar.js')).to.equal('js');
30+
});
31+
32+
it('returns the extension from an URI', () => {
33+
expect(getFileExtension('http://localhost/foo.js')).to.equal('js');
34+
expect(getFileExtension('file://localhost/foo/bar.txt')).to.equal('txt');
35+
expect(getFileExtension('https://localhost:8080/foo.bar.baz')).to.equal('baz');
36+
});
37+
38+
it('works with query strings', () => {
39+
expect(getFileExtension('http://localhost/foo.js?abcd')).to.equal('js');
40+
expect(getFileExtension('foo.txt?bar=baz&baz=bar')).to.equal('txt');
41+
});
42+
});

0 commit comments

Comments
 (0)