From d4a596cab6429e2d100cf7ef6078fd43aec62b41 Mon Sep 17 00:00:00 2001 From: James Zetlen Date: Mon, 11 Feb 2019 07:29:54 -0600 Subject: [PATCH] feat(dev): add loglevel to configure console logging (#351) --- DOCS.md | 17 +++++++++++++++++ src/normalizeOptions.js | 6 ++++++ src/resolvePath.js | 6 +++--- test/index.test.js | 22 ++++++++++++++++++++++ 4 files changed, 48 insertions(+), 3 deletions(-) diff --git a/DOCS.md b/DOCS.md index 565c123..33b4eab 100644 --- a/DOCS.md +++ b/DOCS.md @@ -203,6 +203,23 @@ module.exports = { If you want to leave some paths as-is, then you can return `undefined` or any other falsy value from the function. +## loglevel + +One of the [NPM log level options](https://docs.npmjs.com/misc/config#loglevel) to configure console logging during build. Default is `"warn"`. Passing `"silent"` will disable all warnings for path resolution failures. + +```js +module.exports = { + plugins: [ + ["module-resolver", { + alias: { + "dependency-string": "module-that-does-not-exist" // warning will not log + }, + loglevel: 'silent' + }] + ] +} +``` + # Usage with create-react-app create-react-app by default don't use .babelrc, so in webpack.config.dev.js, add plugins property within js loader like below. Note that plugins recieve an array. diff --git a/src/normalizeOptions.js b/src/normalizeOptions.js index 56736b3..90f7a3f 100644 --- a/src/normalizeOptions.js +++ b/src/normalizeOptions.js @@ -132,6 +132,10 @@ function normalizeTransformedFunctions(optsTransformFunctions) { return [...defaultTransformedFunctions, ...optsTransformFunctions]; } +function normalizeLoglevel(optsLoglevel) { + return optsLoglevel || 'warn'; +} + export default createSelector( // The currentFile should have an extension; otherwise it's considered a special value currentFile => (currentFile.includes('.') ? path.dirname(currentFile) : currentFile), @@ -140,6 +144,7 @@ export default createSelector( const cwd = normalizeCwd(opts.cwd, currentFile); const root = normalizeRoot(opts.root, cwd); const alias = normalizeAlias(opts.alias); + const loglevel = normalizeLoglevel(opts.loglevel); const transformFunctions = normalizeTransformedFunctions(opts.transformFunctions); const extensions = opts.extensions || defaultExtensions; const stripExtensions = opts.stripExtensions || extensions; @@ -149,6 +154,7 @@ export default createSelector( cwd, root, alias, + loglevel, transformFunctions, extensions, stripExtensions, diff --git a/src/resolvePath.js b/src/resolvePath.js index 30c73f7..7778bf8 100644 --- a/src/resolvePath.js +++ b/src/resolvePath.js @@ -39,9 +39,9 @@ function resolvePathFromRootConfig(sourcePath, currentFile, opts) { return getRelativePath(sourcePath, currentFile, absFileInRoot, opts); } -function checkIfPackageExists(modulePath, currentFile, extensions) { +function checkIfPackageExists(modulePath, currentFile, extensions, loglevel) { const resolvedPath = nodeResolvePath(modulePath, currentFile, extensions); - if (resolvedPath === null) { + if (resolvedPath === null && loglevel !== 'silent') { warn(`Could not resolve "${modulePath}" in file ${currentFile}.`); } } @@ -71,7 +71,7 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) { } if (process.env.NODE_ENV !== 'production') { - checkIfPackageExists(aliasedSourceFile, currentFile, opts.extensions); + checkIfPackageExists(aliasedSourceFile, currentFile, opts.extensions, opts.loglevel); } return aliasedSourceFile; diff --git a/test/index.test.js b/test/index.test.js index 7061d03..c26e4ec 100644 --- a/test/index.test.js +++ b/test/index.test.js @@ -685,6 +685,28 @@ describe('module-resolver', () => { expect(mockWarn.mock.calls.length).toBe(0); }); }); + + const silentLoggingOpts = { + babelrc: false, + plugins: [ + [pluginWithMock, { + alias: { + legacy: 'npm:legacy', + 'non-existing': 'this-package-does-not-exist', + }, + loglevel: 'silent', + }], + ], + }; + + it('should respect opt loglevel:silent', () => { + testWithImport( + 'legacy/lib', + 'npm:legacy/lib', + silentLoggingOpts, + ); + expect(mockWarn.mock.calls.length).toBe(0); + }); }); describe('multiple alias application', () => {