Skip to content

Commit

Permalink
feat(dev): add loglevel to configure console logging (#351)
Browse files Browse the repository at this point in the history
  • Loading branch information
zetlen authored and tleunen committed Feb 11, 2019
1 parent 9e6dcf1 commit d4a596c
Show file tree
Hide file tree
Showing 4 changed files with 48 additions and 3 deletions.
17 changes: 17 additions & 0 deletions DOCS.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
6 changes: 6 additions & 0 deletions src/normalizeOptions.js
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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;
Expand All @@ -149,6 +154,7 @@ export default createSelector(
cwd,
root,
alias,
loglevel,
transformFunctions,
extensions,
stripExtensions,
Expand Down
6 changes: 3 additions & 3 deletions src/resolvePath.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}.`);
}
}
Expand Down Expand Up @@ -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;
Expand Down
22 changes: 22 additions & 0 deletions test/index.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down

0 comments on commit d4a596c

Please sign in to comment.