Skip to content

Commit

Permalink
refactor: use logger to avoid pulling logs in output (#1805)
Browse files Browse the repository at this point in the history
  • Loading branch information
alexander-akait authored Jun 19, 2023
1 parent 57c5a4e commit 6bf471c
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 6 deletions.
15 changes: 10 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,9 @@ const path = require('path');
const { CachedChildCompilation } = require('./lib/cached-child-compiler');

const { createHtmlTagObject, htmlTagObjectToString, HtmlTagArray } = require('./lib/html-tags');

const prettyError = require('./lib/errors.js');
const chunkSorter = require('./lib/chunksorter.js');
const getHtmlWebpackPluginHooks = require('./lib/hooks.js').getHtmlWebpackPluginHooks;
const { assert } = require('console');

/** @typedef {import("./typings").HtmlTagObject} HtmlTagObject */
/** @typedef {import("./typings").Options} HtmlWebpackOptions */
Expand All @@ -35,6 +33,8 @@ class HtmlWebpackPlugin {
}

apply (compiler) {
this.logger = compiler.getInfrastructureLogger('HtmlWebpackPlugin');

// Wait for configuration preset plugions to apply all configure webpack defaults
compiler.hooks.initialize.tap('HtmlWebpackPlugin', () => {
const userOptions = this.userOptions;
Expand Down Expand Up @@ -69,8 +69,13 @@ class HtmlWebpackPlugin {
this.options = options;

// Assert correct option spelling
assert(options.scriptLoading === 'defer' || options.scriptLoading === 'blocking' || options.scriptLoading === 'module', 'scriptLoading needs to be set to "defer", "blocking" or "module"');
assert(options.inject === true || options.inject === false || options.inject === 'head' || options.inject === 'body', 'inject needs to be set to true, false, "head" or "body');
if (options.scriptLoading !== 'defer' && options.scriptLoading !== 'blocking' && options.scriptLoading !== 'module') {
this.logger.error('The "scriptLoading" option need to be set to "defer", "blocking" or "module"');
}

if (options.inject !== true && options.inject !== false && options.inject !== 'head' && options.inject !== 'body') {
this.logger.error('The `inject` option needs to be set to true, false, "head" or "body');
}

// Default metaOptions if no template is provided
if (!userOptions.template && options.templateContent === false && options.meta) {
Expand Down Expand Up @@ -419,7 +424,7 @@ function hookIntoCompiler (compiler, options, plugin) {
outputName: finalOutputName,
plugin: plugin
}).catch(err => {
console.error(err);
plugin.logger.error(err);
return null;
}).then(() => null));

Expand Down
2 changes: 1 addition & 1 deletion lib/cached-child-compiler.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
});
* ```
*/
'use strict';

// Import types
/** @typedef {import("webpack/lib/Compiler.js")} WebpackCompiler */
Expand All @@ -35,7 +36,6 @@
dependencies: FileDependencies,
error: Error
}} ChildCompilationResult */
'use strict';

const { HtmlWebpackChildCompiler } = require('./child-compiler');
const fileWatcherApi = require('./file-watcher-api');
Expand Down

0 comments on commit 6bf471c

Please sign in to comment.