Skip to content

Commit 1f511b8

Browse files
refactor: debug option (#342)
BREAKING CHANGE: `debug` option was renamed to `logLevel`, it only accepts string values: `trace`, `debug`, `info`, `warn`, `error` and `silent`
1 parent e808aa2 commit 1f511b8

File tree

8 files changed

+141
-113
lines changed

8 files changed

+141
-113
lines changed

README.md

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -373,18 +373,26 @@ module.exports = {
373373

374374
| Name | Type | Default | Description |
375375
| :---------------------------------: | :---------: | :------------------------: | :------------------------------------------------------------------------------------------------------------------------------------------------ |
376-
| [`debug`](#debug) | `{String}` | **`'warning'`** | [Debug Options](#debug) |
376+
| [`logLevel`](#logLevel) | `{String}` | **`'warning'`** | Level of messages that the module will log |
377377
| [`ignore`](#ignore) | `{Array}` | `[]` | Array of globs to ignore (applied to `from`) |
378378
| [`context`](#context) | `{String}` | `compiler.options.context` | A path that determines how to interpret the `from` path, shared for all patterns |
379379
| [`copyUnmodified`](#copyUnmodified) | `{Boolean}` | `false` | Copies files, regardless of modification when using watch or `webpack-dev-server`. All files are copied on first build, regardless of this option |
380380

381-
#### `debug`
381+
#### `logLevel`
382382

383-
| Name | Type | Default | Description |
384-
| :-------------: | :-----------------: | :-----: | :--------------------------- |
385-
| **`'info'`** | `{String\|Boolean}` | `false` | File location and read info |
386-
| **`'debug'`** | `{String}` | `false` | Very detailed debugging info |
387-
| **`'warning'`** | `{String}` | `true` | Only warnings |
383+
This property defines the level of messages that the module will log. Valid levels include:
384+
385+
- `trace`
386+
- `debug`
387+
- `info`
388+
- `warn`
389+
- `error`
390+
- `silent`
391+
392+
Setting a log level means that all other levels below it will be visible in the
393+
console. Setting `logLevel: 'silent'` will hide all console output. The module
394+
leverages [`webpack-log`](https://github.com/webpack-contrib/webpack-log#readme)
395+
for logging management, and more information can be found on its page.
388396

389397
##### `'info'`
390398

package-lock.json

Lines changed: 24 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,15 +40,16 @@
4040
"webpack": "^4.0.0"
4141
},
4242
"dependencies": {
43-
"globby": "^7.1.1",
4443
"cacache": "^11.3.1",
4544
"find-cache-dir": "^2.0.0",
46-
"serialize-javascript": "^1.4.0",
45+
"globby": "^7.1.1",
4746
"is-glob": "^4.0.0",
4847
"loader-utils": "^1.1.0",
4948
"minimatch": "^3.0.4",
49+
"normalize-path": "^3.0.0",
5050
"p-limit": "^2.1.0",
51-
"normalize-path": "^3.0.0"
51+
"serialize-javascript": "^1.4.0",
52+
"webpack-log": "^2.0.0"
5253
},
5354
"devDependencies": {
5455
"@babel/cli": "^7.1.5",

src/index.js

Lines changed: 18 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
import path from 'path';
22

3+
import log from 'webpack-log';
4+
35
import preProcessPattern from './preProcessPattern';
46
import processPattern from './processPattern';
57
import postProcessPattern from './postProcessPattern';
@@ -15,45 +17,6 @@ class CopyPlugin {
1517
}
1618

1719
apply(compiler) {
18-
// Defaults debug level to 'warning'
19-
// eslint-disable-next-line no-param-reassign
20-
this.options.debug = this.options.debug || 'warning';
21-
22-
// Defaults debugging to info if only true is specified
23-
if (this.options.debug === true) {
24-
// eslint-disable-next-line no-param-reassign
25-
this.options.debug = 'info';
26-
}
27-
28-
const debugLevels = ['warning', 'info', 'debug'];
29-
const debugLevelIndex = debugLevels.indexOf(this.options.debug);
30-
31-
function log(msg, level) {
32-
if (level === 0) {
33-
// eslint-disable-next-line no-param-reassign
34-
msg = `WARNING - ${msg}`;
35-
} else {
36-
// eslint-disable-next-line no-param-reassign
37-
level = level || 1;
38-
}
39-
40-
if (level <= debugLevelIndex) {
41-
console.log(`[copy-webpack-plugin] ${msg}`); // eslint-disable-line no-console
42-
}
43-
}
44-
45-
function warning(msg) {
46-
log(msg, 0);
47-
}
48-
49-
function info(msg) {
50-
log(msg, 1);
51-
}
52-
53-
function debug(msg) {
54-
log(msg, 2);
55-
}
56-
5720
const fileDependencies = new Set();
5821
const contextDependencies = new Set();
5922
const written = {};
@@ -68,15 +31,18 @@ class CopyPlugin {
6831
({ context } = this.options);
6932
}
7033

34+
const logger = log({
35+
name: 'copy-webpack-plugin',
36+
level: this.options.logLevel || 'warn',
37+
});
38+
7139
const plugin = { name: 'CopyPlugin' };
7240

7341
compiler.hooks.emit.tapAsync(plugin, (compilation, callback) => {
74-
debug('starting emit');
42+
logger.debug('starting emit');
7543

7644
const globalRef = {
77-
info,
78-
debug,
79-
warning,
45+
logger,
8046
compilation,
8147
written,
8248
fileDependencies,
@@ -124,22 +90,22 @@ class CopyPlugin {
12490
compilation.errors.push(error);
12591
})
12692
.then(() => {
127-
debug('finishing emit');
93+
logger.debug('finishing emit');
12894

12995
callback();
13096
});
13197
});
13298
compiler.hooks.afterEmit.tapAsync(plugin, (compilation, callback) => {
133-
debug('starting after-emit');
99+
logger.debug('starting after-emit');
134100

135101
// Add file dependencies if they're not already tracked
136102
for (const fileDependency of fileDependencies) {
137103
if (compilation.fileDependencies.has(fileDependency)) {
138-
debug(
139-
`not adding ${fileDependency} to change tracking, because it's already tracked`
104+
logger.debug(
105+
`not adding '${fileDependency}' to change tracking, because it's already tracked`
140106
);
141107
} else {
142-
debug(`adding ${fileDependency} to change tracking`);
108+
logger.debug(`adding '${fileDependency}' to change tracking`);
143109

144110
compilation.fileDependencies.add(fileDependency);
145111
}
@@ -148,17 +114,17 @@ class CopyPlugin {
148114
// Add context dependencies if they're not already tracked
149115
for (const contextDependency of contextDependencies) {
150116
if (compilation.contextDependencies.has(contextDependency)) {
151-
debug(
152-
`not adding ${contextDependency} to change tracking, because it's already tracked`
117+
logger.debug(
118+
`not adding '${contextDependency}' to change tracking, because it's already tracked`
153119
);
154120
} else {
155-
debug(`adding ${contextDependency} to change tracking`);
121+
logger.debug(`adding '${contextDependency}' to change tracking`);
156122

157123
compilation.contextDependencies.add(contextDependency);
158124
}
159125
}
160126

161-
debug('finishing after-emit');
127+
logger.debug('finishing after-emit');
162128

163129
callback();
164130
});

src/postProcessPattern.js

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,23 @@ import { stat, readFile } from './utils/promisify';
1616

1717
export default function postProcessPattern(globalRef, pattern, file) {
1818
const {
19-
info,
20-
debug,
19+
logger,
2120
compilation,
2221
fileDependencies,
2322
written,
2423
inputFileSystem,
2524
copyUnmodified,
2625
} = globalRef;
2726

27+
logger.debug(`getting stats for '${file.absoluteFrom}' to write to assets`);
28+
2829
return stat(inputFileSystem, file.absoluteFrom).then((stats) => {
2930
// We don't write empty directories
3031
if (stats.isDirectory()) {
32+
logger.debug(
33+
`skipping '${file.absoluteFrom}' because it is empty directory`
34+
);
35+
3136
return Promise.resolve();
3237
}
3338

@@ -36,11 +41,13 @@ export default function postProcessPattern(globalRef, pattern, file) {
3641
fileDependencies.add(file.absoluteFrom);
3742
}
3843

39-
info(`reading ${file.absoluteFrom} to write to assets`);
44+
logger.debug(`reading '${file.absoluteFrom}' to write to assets`);
4045

4146
return readFile(inputFileSystem, file.absoluteFrom)
4247
.then((content) => {
4348
if (pattern.transform) {
49+
logger.info(`transforming content for '${file.absoluteFrom}'`);
50+
4451
// eslint-disable-next-line no-shadow
4552
const transform = (content, absoluteFrom) =>
4653
pattern.transform(content, absoluteFrom);
@@ -65,16 +72,26 @@ export default function postProcessPattern(globalRef, pattern, file) {
6572
});
6673

6774
return cacache.get(globalRef.cacheDir, cacheKey).then(
68-
(result) => result.data,
75+
(result) => {
76+
logger.debug(
77+
`getting cached transformation for '${file.absoluteFrom}'`
78+
);
79+
80+
return result.data;
81+
},
6982
() =>
7083
Promise.resolve()
7184
.then(() => transform(content, file.absoluteFrom))
7285
// eslint-disable-next-line no-shadow
73-
.then((content) =>
74-
cacache
86+
.then((content) => {
87+
logger.debug(
88+
`caching transformation for '${file.absoluteFrom}'`
89+
);
90+
91+
return cacache
7592
.put(globalRef.cacheDir, cacheKey, content)
76-
.then(() => content)
77-
)
93+
.then(() => content);
94+
})
7895
);
7996
}
8097

@@ -85,7 +102,7 @@ export default function postProcessPattern(globalRef, pattern, file) {
85102
})
86103
.then((content) => {
87104
if (pattern.toType === 'template') {
88-
info(
105+
logger.info(
89106
`interpolating template '${file.webpackTo}' for '${
90107
file.relativeFrom
91108
}'`
@@ -115,6 +132,10 @@ export default function postProcessPattern(globalRef, pattern, file) {
115132
})
116133
.then((content) => {
117134
if (pattern.transformPath) {
135+
logger.info(
136+
`transforming path '${file.webpackTo}' for '${file.absoluteFrom}'`
137+
);
138+
118139
return Promise.resolve(
119140
pattern.transformPath(file.webpackTo, file.absoluteFrom)
120141
)
@@ -136,12 +157,14 @@ export default function postProcessPattern(globalRef, pattern, file) {
136157
written[file.webpackTo][file.absoluteFrom] &&
137158
written[file.webpackTo][file.absoluteFrom] === hash
138159
) {
139-
info(`skipping '${file.webpackTo}', because it hasn't changed`);
160+
logger.info(
161+
`skipping '${file.webpackTo}', because content hasn't changed`
162+
);
140163

141164
return;
142165
}
143166

144-
debug(`added ${hash} to written tracking for '${file.absoluteFrom}'`);
167+
logger.debug(`adding '${file.webpackTo}' for tracking content changes`);
145168

146169
if (!written[file.webpackTo]) {
147170
written[file.webpackTo] = {};
@@ -150,12 +173,14 @@ export default function postProcessPattern(globalRef, pattern, file) {
150173
written[file.webpackTo][file.absoluteFrom] = hash;
151174

152175
if (compilation.assets[file.webpackTo] && !file.force) {
153-
info(`skipping '${file.webpackTo}', because it already exists`);
176+
logger.info(
177+
`skipping '${file.webpackTo}', because it already exists`
178+
);
154179

155180
return;
156181
}
157182

158-
info(
183+
logger.info(
159184
`writing '${file.webpackTo}' to compilation assets from '${
160185
file.absoluteFrom
161186
}'`

0 commit comments

Comments
 (0)