Skip to content

Commit

Permalink
fix(tests): Upgrade webpack-recompilation-simulator
Browse files Browse the repository at this point in the history
  • Loading branch information
jantimon committed Jul 11, 2018
1 parent 302e39e commit dfe1d10
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 24 deletions.
20 changes: 9 additions & 11 deletions lib/loader.js
Expand Up @@ -5,26 +5,24 @@ const _ = require('lodash');
const loaderUtils = require('loader-utils');

module.exports = function (source) {
if (this.cacheable) {
this.cacheable();
}
// Get templating options
const options = this.query !== '' ? loaderUtils.getOptions(this) : {};
const force = options.force || false;

const allLoadersButThisOne = this.loaders.filter(function (loader) {
// Loader API changed from `loader.module` to `loader.normal` in Webpack 2.
return (loader.module || loader.normal) !== module.exports;
return loader.normal !== module.exports;
});
// This loader shouldn't kick in if there is any other loader
if (allLoadersButThisOne.length > 0) {
// This loader shouldn't kick in if there is any other loader (unless it's explicitly enforced)
if (allLoadersButThisOne.length > 0 && !force) {
return source;
}
// Skip .js files
if (/\.js$/.test(this.resourcePath)) {
// Skip .js files (unless it's explicitly enforced)
if (/\.js$/.test(this.resourcePath) && !force) {
return source;
}

// The following part renders the template with lodash as aminimalistic loader
//
// Get templating options
const options = this.query !== '' ? loaderUtils.getOptions(this) : {};
const template = _.template(source, _.defaults(options, { interpolate: /<%=([\s\S]+?)%>/g, variable: 'data' }));
// Require !!lodash - using !! will disable all loaders (e.g. babel)
return 'var _ = require(' + loaderUtils.stringifyRequest(this, '!!' + require.resolve('lodash')) + ');' +
Expand Down
4 changes: 2 additions & 2 deletions package.json
Expand Up @@ -44,9 +44,9 @@
"typescript": "2.9.1",
"underscore-template-loader": "^0.7.3",
"url-loader": "^0.5.7",
"webpack": "4.8.3",
"webpack": "4.1.0",
"webpack-cli": "2.0.12",
"webpack-recompilation-simulator": "^1.3.0"
"webpack-recompilation-simulator": "^3.0.0"
},
"dependencies": {
"@types/tapable": "1.0.2",
Expand Down
1 change: 1 addition & 0 deletions spec/basic.spec.js
Expand Up @@ -33,6 +33,7 @@ var OUTPUT_DIR = path.resolve(__dirname, '../dist/basic-spec');

jest.setTimeout(30000);
process.on('unhandledRejection', r => console.log(r));
process.traceDeprecation = true;

function testHtmlPlugin (webpackConfig, expectedResults, outputFile, done, expectErrors, expectWarnings) {
outputFile = outputFile || 'index.html';
Expand Down
72 changes: 61 additions & 11 deletions spec/caching.spec.js
Expand Up @@ -10,36 +10,74 @@ var webpack = require('webpack');
var rimraf = require('rimraf');
var WebpackRecompilationSimulator = require('webpack-recompilation-simulator');
var HtmlWebpackPlugin = require('../index.js');
var webpackMajorVersion = require('webpack/package.json').version.split('.')[0];

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

jest.setTimeout(30000);
process.on('unhandledRejection', r => console.log(r));
process.traceDeprecation = true;

function setUpCompiler (htmlWebpackPlugin) {
jest.spyOn(htmlWebpackPlugin, 'evaluateCompilationResult');
var webpackConfig = {
stats: {all: true},
// Caching works only in development
mode: 'development',
entry: path.join(__dirname, 'fixtures/index.js'),
module: {
rules: [
{
test: /\.html$/,
loader: require.resolve('../lib/loader.js'),
options: {
force: true
}
}
]
},
output: {
path: OUTPUT_DIR,
filename: 'index_bundle.js'
},
plugins: [htmlWebpackPlugin]
};
if (Number(webpackMajorVersion) >= 4) {
webpackConfig.mode = 'development';
}
var compiler = new WebpackRecompilationSimulator(webpack(webpackConfig));
return compiler;
}

function getCompiledModuleCount (statsJson) {
return statsJson.modules.filter(function (webpackModule) {
function getCompiledModules (statsJson) {
const builtModules = statsJson.modules.filter(function (webpackModule) {
return webpackModule.built;
}).length + statsJson.children.reduce(function (sum, childCompilationStats) {
return sum + getCompiledModuleCount(childCompilationStats);
}, 0);
}).map((webpackModule) => {
return module.userRequest;
});
statsJson.children.forEach((childCompilationStats) => {
const builtChildModules = getCompiledModules(childCompilationStats);
Array.prototype.push.apply(builtModules, builtChildModules);
});
return builtModules;
}

function getCompiledModuleCount (statsJson) {
return getCompiledModules(statsJson).length;
}

function expectNoErrors (stats) {
const errors = {
main: stats.compilation.errors,
childCompilation: []
};
stats.compilation.children.forEach((child) => {
Array.prototype.push.apply(errors.childCompilation, child.errors);
});
if (errors.main.length) {
errors.main.forEach((error) => {
console.log('Error => ', error);
});
console.dir(stats.toJson({errorDetails: true, moduleTrace: true}), { depth: 5 });
}
expect(errors.main).toEqual([]);
expect(errors.childCompilation).toEqual([]);
}

describe('HtmlWebpackPluginCaching', function () {
Expand All @@ -54,16 +92,19 @@ describe('HtmlWebpackPluginCaching', function () {
});
var childCompilerHash;
var compiler = setUpCompiler(htmlWebpackPlugin);
compiler.addTestFile(path.join(__dirname, 'fixtures/index.js'));
compiler.run()
// Change the template file and compile again
.then(function () {
childCompilerHash = htmlWebpackPlugin.childCompilerHash;
return compiler.run();
})
.then(function (stats) {
// Expect no errors:
expectNoErrors(stats);
// Verify that no file was built
expect(getCompiledModuleCount(stats.toJson()))
.toBe(0);
expect(getCompiledModules(stats.toJson()))
.toEqual([]);
// Verify that the html was processed only during the inital build
expect(htmlWebpackPlugin.evaluateCompilationResult.mock.calls.length)
.toBe(1);
Expand All @@ -78,6 +119,7 @@ describe('HtmlWebpackPluginCaching', function () {
var htmlWebpackPlugin = new HtmlWebpackPlugin();
var compiler = setUpCompiler(htmlWebpackPlugin);
var childCompilerHash;
compiler.addTestFile(path.join(__dirname, 'fixtures/index.js'));
compiler.run()
// Change a js file and compile again
.then(function () {
Expand All @@ -86,6 +128,8 @@ describe('HtmlWebpackPluginCaching', function () {
return compiler.run();
})
.then(function (stats) {
// Expect no errors:
expectNoErrors(stats);
// Verify that only one file was built
expect(getCompiledModuleCount(stats.toJson()))
.toBe(1);
Expand All @@ -105,6 +149,7 @@ describe('HtmlWebpackPluginCaching', function () {
});
var childCompilerHash;
var compiler = setUpCompiler(htmlWebpackPlugin);
compiler.addTestFile(path.join(__dirname, 'fixtures/index.js'));
compiler.run()
// Change a js file and compile again
.then(function () {
Expand All @@ -113,6 +158,8 @@ describe('HtmlWebpackPluginCaching', function () {
return compiler.run();
})
.then(function (stats) {
// Expect no errors:
expectNoErrors(stats);
// Verify that only one file was built
expect(getCompiledModuleCount(stats.toJson()))
.toBe(1);
Expand All @@ -133,6 +180,7 @@ describe('HtmlWebpackPluginCaching', function () {
});
var childCompilerHash;
var compiler = setUpCompiler(htmlWebpackPlugin);
compiler.addTestFile(template);
compiler.run()
// Change the template file and compile again
.then(function () {
Expand All @@ -141,6 +189,8 @@ describe('HtmlWebpackPluginCaching', function () {
return compiler.run();
})
.then(function (stats) {
// Expect no errors:
expectNoErrors(stats);
// Verify that only one file was built
expect(getCompiledModuleCount(stats.toJson()))
.toBe(1);
Expand Down
1 change: 1 addition & 0 deletions spec/example.spec.js
Expand Up @@ -15,6 +15,7 @@ var webpackMajorVersion = require('webpack/package.json').version.split('.')[0];
var OUTPUT_DIR = path.resolve(__dirname, '../dist');

jest.setTimeout(30000);
process.traceDeprecation = true;

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

0 comments on commit dfe1d10

Please sign in to comment.