Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dev): add loglevel to configure console logging #351

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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