Skip to content

Commit dfe1d10

Browse files
committedJul 11, 2018
fix(tests): Upgrade webpack-recompilation-simulator
1 parent 302e39e commit dfe1d10

File tree

5 files changed

+74
-24
lines changed

5 files changed

+74
-24
lines changed
 

‎lib/loader.js

+9-11
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,24 @@ const _ = require('lodash');
55
const loaderUtils = require('loader-utils');
66

77
module.exports = function (source) {
8-
if (this.cacheable) {
9-
this.cacheable();
10-
}
8+
// Get templating options
9+
const options = this.query !== '' ? loaderUtils.getOptions(this) : {};
10+
const force = options.force || false;
11+
1112
const allLoadersButThisOne = this.loaders.filter(function (loader) {
12-
// Loader API changed from `loader.module` to `loader.normal` in Webpack 2.
13-
return (loader.module || loader.normal) !== module.exports;
13+
return loader.normal !== module.exports;
1414
});
15-
// This loader shouldn't kick in if there is any other loader
16-
if (allLoadersButThisOne.length > 0) {
15+
// This loader shouldn't kick in if there is any other loader (unless it's explicitly enforced)
16+
if (allLoadersButThisOne.length > 0 && !force) {
1717
return source;
1818
}
19-
// Skip .js files
20-
if (/\.js$/.test(this.resourcePath)) {
19+
// Skip .js files (unless it's explicitly enforced)
20+
if (/\.js$/.test(this.resourcePath) && !force) {
2121
return source;
2222
}
2323

2424
// The following part renders the template with lodash as aminimalistic loader
2525
//
26-
// Get templating options
27-
const options = this.query !== '' ? loaderUtils.getOptions(this) : {};
2826
const template = _.template(source, _.defaults(options, { interpolate: /<%=([\s\S]+?)%>/g, variable: 'data' }));
2927
// Require !!lodash - using !! will disable all loaders (e.g. babel)
3028
return 'var _ = require(' + loaderUtils.stringifyRequest(this, '!!' + require.resolve('lodash')) + ');' +

‎package.json

+2-2
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@
4444
"typescript": "2.9.1",
4545
"underscore-template-loader": "^0.7.3",
4646
"url-loader": "^0.5.7",
47-
"webpack": "4.8.3",
47+
"webpack": "4.1.0",
4848
"webpack-cli": "2.0.12",
49-
"webpack-recompilation-simulator": "^1.3.0"
49+
"webpack-recompilation-simulator": "^3.0.0"
5050
},
5151
"dependencies": {
5252
"@types/tapable": "1.0.2",

‎spec/basic.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ var OUTPUT_DIR = path.resolve(__dirname, '../dist/basic-spec');
3333

3434
jest.setTimeout(30000);
3535
process.on('unhandledRejection', r => console.log(r));
36+
process.traceDeprecation = true;
3637

3738
function testHtmlPlugin (webpackConfig, expectedResults, outputFile, done, expectErrors, expectWarnings) {
3839
outputFile = outputFile || 'index.html';

‎spec/caching.spec.js

+61-11
Original file line numberDiff line numberDiff line change
@@ -10,36 +10,74 @@ var webpack = require('webpack');
1010
var rimraf = require('rimraf');
1111
var WebpackRecompilationSimulator = require('webpack-recompilation-simulator');
1212
var HtmlWebpackPlugin = require('../index.js');
13-
var webpackMajorVersion = require('webpack/package.json').version.split('.')[0];
1413

1514
var OUTPUT_DIR = path.join(__dirname, '../dist/caching-spec');
1615

1716
jest.setTimeout(30000);
1817
process.on('unhandledRejection', r => console.log(r));
18+
process.traceDeprecation = true;
1919

2020
function setUpCompiler (htmlWebpackPlugin) {
2121
jest.spyOn(htmlWebpackPlugin, 'evaluateCompilationResult');
2222
var webpackConfig = {
23+
stats: {all: true},
24+
// Caching works only in development
25+
mode: 'development',
2326
entry: path.join(__dirname, 'fixtures/index.js'),
27+
module: {
28+
rules: [
29+
{
30+
test: /\.html$/,
31+
loader: require.resolve('../lib/loader.js'),
32+
options: {
33+
force: true
34+
}
35+
}
36+
]
37+
},
2438
output: {
2539
path: OUTPUT_DIR,
2640
filename: 'index_bundle.js'
2741
},
2842
plugins: [htmlWebpackPlugin]
2943
};
30-
if (Number(webpackMajorVersion) >= 4) {
31-
webpackConfig.mode = 'development';
32-
}
3344
var compiler = new WebpackRecompilationSimulator(webpack(webpackConfig));
3445
return compiler;
3546
}
3647

37-
function getCompiledModuleCount (statsJson) {
38-
return statsJson.modules.filter(function (webpackModule) {
48+
function getCompiledModules (statsJson) {
49+
const builtModules = statsJson.modules.filter(function (webpackModule) {
3950
return webpackModule.built;
40-
}).length + statsJson.children.reduce(function (sum, childCompilationStats) {
41-
return sum + getCompiledModuleCount(childCompilationStats);
42-
}, 0);
51+
}).map((webpackModule) => {
52+
return module.userRequest;
53+
});
54+
statsJson.children.forEach((childCompilationStats) => {
55+
const builtChildModules = getCompiledModules(childCompilationStats);
56+
Array.prototype.push.apply(builtModules, builtChildModules);
57+
});
58+
return builtModules;
59+
}
60+
61+
function getCompiledModuleCount (statsJson) {
62+
return getCompiledModules(statsJson).length;
63+
}
64+
65+
function expectNoErrors (stats) {
66+
const errors = {
67+
main: stats.compilation.errors,
68+
childCompilation: []
69+
};
70+
stats.compilation.children.forEach((child) => {
71+
Array.prototype.push.apply(errors.childCompilation, child.errors);
72+
});
73+
if (errors.main.length) {
74+
errors.main.forEach((error) => {
75+
console.log('Error => ', error);
76+
});
77+
console.dir(stats.toJson({errorDetails: true, moduleTrace: true}), { depth: 5 });
78+
}
79+
expect(errors.main).toEqual([]);
80+
expect(errors.childCompilation).toEqual([]);
4381
}
4482

4583
describe('HtmlWebpackPluginCaching', function () {
@@ -54,16 +92,19 @@ describe('HtmlWebpackPluginCaching', function () {
5492
});
5593
var childCompilerHash;
5694
var compiler = setUpCompiler(htmlWebpackPlugin);
95+
compiler.addTestFile(path.join(__dirname, 'fixtures/index.js'));
5796
compiler.run()
5897
// Change the template file and compile again
5998
.then(function () {
6099
childCompilerHash = htmlWebpackPlugin.childCompilerHash;
61100
return compiler.run();
62101
})
63102
.then(function (stats) {
103+
// Expect no errors:
104+
expectNoErrors(stats);
64105
// Verify that no file was built
65-
expect(getCompiledModuleCount(stats.toJson()))
66-
.toBe(0);
106+
expect(getCompiledModules(stats.toJson()))
107+
.toEqual([]);
67108
// Verify that the html was processed only during the inital build
68109
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
69110
.toBe(1);
@@ -78,6 +119,7 @@ describe('HtmlWebpackPluginCaching', function () {
78119
var htmlWebpackPlugin = new HtmlWebpackPlugin();
79120
var compiler = setUpCompiler(htmlWebpackPlugin);
80121
var childCompilerHash;
122+
compiler.addTestFile(path.join(__dirname, 'fixtures/index.js'));
81123
compiler.run()
82124
// Change a js file and compile again
83125
.then(function () {
@@ -86,6 +128,8 @@ describe('HtmlWebpackPluginCaching', function () {
86128
return compiler.run();
87129
})
88130
.then(function (stats) {
131+
// Expect no errors:
132+
expectNoErrors(stats);
89133
// Verify that only one file was built
90134
expect(getCompiledModuleCount(stats.toJson()))
91135
.toBe(1);
@@ -105,6 +149,7 @@ describe('HtmlWebpackPluginCaching', function () {
105149
});
106150
var childCompilerHash;
107151
var compiler = setUpCompiler(htmlWebpackPlugin);
152+
compiler.addTestFile(path.join(__dirname, 'fixtures/index.js'));
108153
compiler.run()
109154
// Change a js file and compile again
110155
.then(function () {
@@ -113,6 +158,8 @@ describe('HtmlWebpackPluginCaching', function () {
113158
return compiler.run();
114159
})
115160
.then(function (stats) {
161+
// Expect no errors:
162+
expectNoErrors(stats);
116163
// Verify that only one file was built
117164
expect(getCompiledModuleCount(stats.toJson()))
118165
.toBe(1);
@@ -133,6 +180,7 @@ describe('HtmlWebpackPluginCaching', function () {
133180
});
134181
var childCompilerHash;
135182
var compiler = setUpCompiler(htmlWebpackPlugin);
183+
compiler.addTestFile(template);
136184
compiler.run()
137185
// Change the template file and compile again
138186
.then(function () {
@@ -141,6 +189,8 @@ describe('HtmlWebpackPluginCaching', function () {
141189
return compiler.run();
142190
})
143191
.then(function (stats) {
192+
// Expect no errors:
193+
expectNoErrors(stats);
144194
// Verify that only one file was built
145195
expect(getCompiledModuleCount(stats.toJson()))
146196
.toBe(1);

‎spec/example.spec.js

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ var webpackMajorVersion = require('webpack/package.json').version.split('.')[0];
1515
var OUTPUT_DIR = path.resolve(__dirname, '../dist');
1616

1717
jest.setTimeout(30000);
18+
process.traceDeprecation = true;
1819

1920
function runExample (exampleName, done) {
2021
var examplePath = path.resolve(__dirname, '..', 'examples', exampleName);

0 commit comments

Comments
 (0)
Please sign in to comment.