From dfe1d10c4511b0da4354cacf79ca0d5ac7baf862 Mon Sep 17 00:00:00 2001 From: Jan Nicklas Date: Wed, 4 Jul 2018 10:54:16 +0200 Subject: [PATCH] fix(tests): Upgrade webpack-recompilation-simulator --- lib/loader.js | 20 ++++++------ package.json | 4 +-- spec/basic.spec.js | 1 + spec/caching.spec.js | 72 +++++++++++++++++++++++++++++++++++++------- spec/example.spec.js | 1 + 5 files changed, 74 insertions(+), 24 deletions(-) diff --git a/lib/loader.js b/lib/loader.js index 3fecc64b..01c52cd9 100644 --- a/lib/loader.js +++ b/lib/loader.js @@ -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')) + ');' + diff --git a/package.json b/package.json index cc72fc17..3472e7b1 100644 --- a/package.json +++ b/package.json @@ -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", diff --git a/spec/basic.spec.js b/spec/basic.spec.js index 2a9a0445..f8fc7e63 100644 --- a/spec/basic.spec.js +++ b/spec/basic.spec.js @@ -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'; diff --git a/spec/caching.spec.js b/spec/caching.spec.js index 94c4e04d..bab3d291 100644 --- a/spec/caching.spec.js +++ b/spec/caching.spec.js @@ -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 () { @@ -54,6 +92,7 @@ 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 () { @@ -61,9 +100,11 @@ describe('HtmlWebpackPluginCaching', function () { 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); @@ -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 () { @@ -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); @@ -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 () { @@ -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); @@ -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 () { @@ -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); diff --git a/spec/example.spec.js b/spec/example.spec.js index 230b3ba9..c470a98b 100644 --- a/spec/example.spec.js +++ b/spec/example.spec.js @@ -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);