Skip to content

Commit d4a596c

Browse files
zetlentleunen
authored andcommitted
feat(dev): add loglevel to configure console logging (#351)
1 parent 9e6dcf1 commit d4a596c

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
lines changed

DOCS.md

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -203,6 +203,23 @@ module.exports = {
203203

204204
If you want to leave some paths as-is, then you can return `undefined` or any other falsy value from the function.
205205

206+
## loglevel
207+
208+
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.
209+
210+
```js
211+
module.exports = {
212+
plugins: [
213+
["module-resolver", {
214+
alias: {
215+
"dependency-string": "module-that-does-not-exist" // warning will not log
216+
},
217+
loglevel: 'silent'
218+
}]
219+
]
220+
}
221+
```
222+
206223
# Usage with create-react-app
207224

208225
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.

src/normalizeOptions.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,10 @@ function normalizeTransformedFunctions(optsTransformFunctions) {
132132
return [...defaultTransformedFunctions, ...optsTransformFunctions];
133133
}
134134

135+
function normalizeLoglevel(optsLoglevel) {
136+
return optsLoglevel || 'warn';
137+
}
138+
135139
export default createSelector(
136140
// The currentFile should have an extension; otherwise it's considered a special value
137141
currentFile => (currentFile.includes('.') ? path.dirname(currentFile) : currentFile),
@@ -140,6 +144,7 @@ export default createSelector(
140144
const cwd = normalizeCwd(opts.cwd, currentFile);
141145
const root = normalizeRoot(opts.root, cwd);
142146
const alias = normalizeAlias(opts.alias);
147+
const loglevel = normalizeLoglevel(opts.loglevel);
143148
const transformFunctions = normalizeTransformedFunctions(opts.transformFunctions);
144149
const extensions = opts.extensions || defaultExtensions;
145150
const stripExtensions = opts.stripExtensions || extensions;
@@ -149,6 +154,7 @@ export default createSelector(
149154
cwd,
150155
root,
151156
alias,
157+
loglevel,
152158
transformFunctions,
153159
extensions,
154160
stripExtensions,

src/resolvePath.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,9 +39,9 @@ function resolvePathFromRootConfig(sourcePath, currentFile, opts) {
3939
return getRelativePath(sourcePath, currentFile, absFileInRoot, opts);
4040
}
4141

42-
function checkIfPackageExists(modulePath, currentFile, extensions) {
42+
function checkIfPackageExists(modulePath, currentFile, extensions, loglevel) {
4343
const resolvedPath = nodeResolvePath(modulePath, currentFile, extensions);
44-
if (resolvedPath === null) {
44+
if (resolvedPath === null && loglevel !== 'silent') {
4545
warn(`Could not resolve "${modulePath}" in file ${currentFile}.`);
4646
}
4747
}
@@ -71,7 +71,7 @@ function resolvePathFromAliasConfig(sourcePath, currentFile, opts) {
7171
}
7272

7373
if (process.env.NODE_ENV !== 'production') {
74-
checkIfPackageExists(aliasedSourceFile, currentFile, opts.extensions);
74+
checkIfPackageExists(aliasedSourceFile, currentFile, opts.extensions, opts.loglevel);
7575
}
7676

7777
return aliasedSourceFile;

test/index.test.js

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,28 @@ describe('module-resolver', () => {
685685
expect(mockWarn.mock.calls.length).toBe(0);
686686
});
687687
});
688+
689+
const silentLoggingOpts = {
690+
babelrc: false,
691+
plugins: [
692+
[pluginWithMock, {
693+
alias: {
694+
legacy: 'npm:legacy',
695+
'non-existing': 'this-package-does-not-exist',
696+
},
697+
loglevel: 'silent',
698+
}],
699+
],
700+
};
701+
702+
it('should respect opt loglevel:silent', () => {
703+
testWithImport(
704+
'legacy/lib',
705+
'npm:legacy/lib',
706+
silentLoggingOpts,
707+
);
708+
expect(mockWarn.mock.calls.length).toBe(0);
709+
});
688710
});
689711

690712
describe('multiple alias application', () => {

0 commit comments

Comments
 (0)