Skip to content

Commit 72296c9

Browse files
committed
bug #979 #936: Fix manifest key problem when using copy files (bobvandevijver)
This PR was squashed before being merged into the main branch. Discussion ---------- #936: Fix manifest key problem when using copy files So, the solution for #936 was not complex at all, but creating a failing test was. I needed to update the test scripts in the `package.json`, as webpack simply doesn't write it's cache until the mocha process ends. I did not find an alternative way to trigger the cache files to be written. So, I separated the persistent cache tests into a dedicated directory/file, which is ignored during the normal test run. I added commands to run the persistent cache test both with and without previous cache (using the `CLEAR_PERSISTENT_CACHE` environment variable), and updated the original test command to run the normal test once, and the persistent cache tests twice (so the second run is with a valid cache). I also needed to update the temp directory generation for the persistent cache, as it need to be deterministic over multiple cache runs. This is done with an extra parameter when creating the webpack configuration. This also makes sure that the webpack persistent cache is not shared over multiple tests (as long as you use unique keys obviously. Just let me know if something needs to be done different! Commits ------- c18ac58 #936: Fix manifest key problem when using copy files
2 parents 37c6b37 + c18ac58 commit 72296c9

File tree

6 files changed

+124
-25
lines changed

6 files changed

+124
-25
lines changed

lib/webpack-manifest-plugin/hooks.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -129,12 +129,16 @@ const normalModuleLoaderHook = ({ moduleAssets }, loaderContext, module) => {
129129
const { emitFile } = loaderContext;
130130

131131
// eslint-disable-next-line no-param-reassign
132-
loaderContext.emitFile = (file, content, sourceMap) => {
132+
loaderContext.emitFile = (file, content, sourceMap, assetInfo) => {
133+
const info = Object.assign({}, assetInfo);
134+
133135
if (module.userRequest && !moduleAssets[file]) {
134-
Object.assign(moduleAssets, { [file]: join(dirname(file), basename(module.userRequest)) });
136+
info.sourceFilename = join(dirname(file), basename(module.userRequest));
137+
138+
Object.assign(moduleAssets, { [file]: info.sourceFilename });
135139
}
136140

137-
return emitFile.call(module, file, content, sourceMap);
141+
return emitFile.call(module, file, content, sourceMap, info);
138142
};
139143
};
140144

package.json

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44
"description": "Webpack Encore is a simpler way to integrate Webpack into your application",
55
"main": "index.js",
66
"scripts": {
7-
"test": "mocha --reporter spec test --recursive",
7+
"test": "yarn run test:main && yarn run test:persistent-cache",
8+
"test:main": "mocha --reporter spec test --recursive --ignore test/persistent-cache/*",
9+
"test:persistent-cache": "node run-persistent-tests",
810
"lint": "eslint lib test index.js .eslintrc.js",
9-
"travis:lint": "npm run lint"
11+
"travis:lint": "yarn run lint"
1012
},
1113
"bin": {
1214
"encore": "bin/encore.js"

run-persistent-tests.js

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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 { execSync } = require('child_process');
13+
const { emptyTmpDir } = require('./test/helpers/setup');
14+
15+
emptyTmpDir();
16+
for (let i = 0; i < 2; i++) {
17+
execSync('mocha --reporter spec test/persistent-cache --recursive', { stdio: 'inherit' });
18+
}

test/functional.js

Lines changed: 0 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -753,23 +753,6 @@ describe('Functional tests using webpack', function() {
753753
});
754754
});
755755

756-
it('Persistent caching does not cause problems', (done) => {
757-
const config = createWebpackConfig('www/build', 'dev');
758-
config.setPublicPath('/build');
759-
config.addEntry('main', './js/code_splitting');
760-
config.enableBuildCache({ config: [__filename] });
761-
762-
testSetup.runWebpack(config, (webpackAssert) => {
763-
// sanity check
764-
webpackAssert.assertManifestPath(
765-
'build/main.js',
766-
'/build/main.js'
767-
);
768-
769-
done();
770-
});
771-
});
772-
773756
describe('addCacheGroup()', () => {
774757
it('addCacheGroup() to extract a vendor into its own chunk', (done) => {
775758
const config = createWebpackConfig('www/build', 'dev');
@@ -1295,7 +1278,6 @@ module.exports = {
12951278
});
12961279
});
12971280

1298-
12991281
it('When enabled, react JSX is transformed!', (done) => {
13001282
const config = createWebpackConfig('www/build', 'dev');
13011283
config.setPublicPath('/build');

test/helpers/setup.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,8 +25,8 @@ const testFixturesDir = path.join(__dirname, '../', '../', 'fixtures');
2525

2626
let servers = [];
2727

28-
function createTestAppDir(rootDir = tmpDir) {
29-
const testAppDir = path.join(rootDir, Math.random().toString(36).substring(7));
28+
function createTestAppDir(rootDir = null, subDir = null) {
29+
const testAppDir = path.join(rootDir ? rootDir : tmpDir, subDir ? subDir : Math.random().toString(36).substring(7));
3030

3131
// copy the fixtures into this new directory
3232
fs.copySync(testFixturesDir, testAppDir);

test/persistent-cache/functional.js

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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 chai = require('chai');
13+
chai.use(require('chai-fs'));
14+
chai.use(require('chai-subset'));
15+
const path = require('path');
16+
const testSetup = require('../helpers/setup');
17+
18+
function createWebpackConfig(outputDirName = '', testName, command, argv = {}) {
19+
// We need a static named test dir for the cache to work
20+
let testAppDir = testSetup.createTestAppDir(null, testName + '/test');
21+
const webpackConfig = testSetup.createWebpackConfig(
22+
testAppDir,
23+
outputDirName,
24+
command,
25+
argv,
26+
);
27+
28+
webpackConfig.enableSingleRuntimeChunk();
29+
webpackConfig.enableBuildCache({ config: [__filename] }, (cache) => {
30+
cache.cacheDirectory = path.resolve(testAppDir, '..', '.webpack-cache');
31+
});
32+
33+
return webpackConfig;
34+
}
35+
36+
describe('Functional persistent cache tests using webpack', function() {
37+
// being functional tests, these can take quite long
38+
this.timeout(10000);
39+
40+
describe('Basic scenarios.', () => {
41+
it('Persistent caching does not cause problems', (done) => {
42+
const config = createWebpackConfig('www/build', 'basic_cache', 'dev');
43+
config.setPublicPath('/build');
44+
config.addEntry('main', './js/code_splitting');
45+
46+
testSetup.runWebpack(config, (webpackAssert) => {
47+
// sanity check
48+
webpackAssert.assertManifestPath(
49+
'build/main.js',
50+
'/build/main.js',
51+
);
52+
53+
done();
54+
});
55+
});
56+
});
57+
58+
describe('copyFiles() allows to copy files and folders', () => {
59+
it('Persistent caching does not cause problems', (done) => {
60+
const config = createWebpackConfig('www/build', 'copy_files_cache', 'production');
61+
config.addEntry('main', './js/no_require');
62+
config.setPublicPath('/build');
63+
config.enableVersioning(true);
64+
config.copyFiles([{
65+
from: './images',
66+
includeSubdirectories: false,
67+
}]);
68+
69+
testSetup.runWebpack(config, (webpackAssert) => {
70+
webpackAssert.assertDirectoryContents([
71+
'entrypoints.json',
72+
'runtime.[hash:8].js',
73+
'main.[hash:8].js',
74+
'manifest.json',
75+
'symfony_logo.[hash:8].png',
76+
'symfony_logo_alt.[hash:8].png',
77+
]);
78+
79+
webpackAssert.assertManifestPath(
80+
'build/symfony_logo.png',
81+
'/build/symfony_logo.91beba37.png',
82+
);
83+
84+
webpackAssert.assertManifestPath(
85+
'build/symfony_logo_alt.png',
86+
'/build/symfony_logo_alt.f880ba14.png',
87+
);
88+
89+
done();
90+
});
91+
});
92+
});
93+
});

0 commit comments

Comments
 (0)