Skip to content

Commit

Permalink
feat: emit warning instead error if file doesn't exist (#338)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: plugin emit warning instead error if file doesn't exist
  • Loading branch information
evilebottnawi committed Feb 18, 2019
1 parent f551c0d commit a1c5372
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 22 deletions.
25 changes: 15 additions & 10 deletions src/preProcessPattern.js
Expand Up @@ -95,16 +95,21 @@ export default function preProcessPattern(globalRef, pattern) {
// when we already in watch mode and this directories are not in context dependencies
contextDependencies.add(pattern.context);
} else {
const msg = `unable to locate '${pattern.from}' at '${
pattern.absoluteFrom
}'`;
const warningMsg = `[copy-webpack-plugin] ${msg}`;

// only display the same message once
if (compilation.errors.indexOf(warningMsg) === -1) {
warning(msg);

compilation.errors.push(warningMsg);
const newWarning = new Error(
`[copy-webpack-plugin] unable to locate '${pattern.from}' at '${
pattern.absoluteFrom
}'`
);
const hasWarning = compilation.warnings.some(
// eslint-disable-next-line no-shadow
(warning) => warning.message === newWarning.message
);

// Only display the same message once
if (!hasWarning) {
warning(newWarning.message);

compilation.warnings.push(newWarning);
}

pattern.fromType = 'nonexistent';
Expand Down
39 changes: 27 additions & 12 deletions test/CopyPlugin.test.js
Expand Up @@ -108,6 +108,7 @@ describe('apply function', () => {
{
assets: {},
errors: [],
warnings: [],
fileDependencies: new Set(),
contextDependencies: new Set(),
},
Expand Down Expand Up @@ -142,6 +143,13 @@ describe('apply function', () => {
} else if (compilation.errors.length > 0) {
throw compilation.errors[0];
}

if (opts.expectedWarnings) {
expect(compilation.warnings).toEqual(opts.expectedWarnings);
} else if (compilation.warnings.length > 0) {
throw compilation.warnings[0];
}

resolve(compilation);
})
.catch(reject);
Expand Down Expand Up @@ -206,6 +214,7 @@ describe('apply function', () => {
const compilation = {
assets: {},
errors: [],
warnings: [],
fileDependencies: new Set(),
contextDependencies: new Set(),
};
Expand Down Expand Up @@ -756,10 +765,12 @@ describe('apply function', () => {
it('warns when file not found', (done) => {
runEmit({
expectedAssetKeys: [],
expectedErrors: [
`[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${
path.sep
}nonexistent.txt'`,
expectedWarnings: [
new Error(
`[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${
path.sep
}nonexistent.txt'`
),
],
patterns: [
{
Expand All @@ -775,10 +786,12 @@ describe('apply function', () => {
runEmit({
compiler: new MockCompilerNoStat(),
expectedAssetKeys: [],
expectedErrors: [
`[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${
path.sep
}nonexistent.txt'`,
expectedWarnings: [
new Error(
`[copy-webpack-plugin] unable to locate 'nonexistent.txt' at '${HELPER_DIR}${
path.sep
}nonexistent.txt'`
),
],
patterns: [
{
Expand Down Expand Up @@ -1414,10 +1427,12 @@ describe('apply function', () => {
it('warns when directory not found', (done) => {
runEmit({
expectedAssetKeys: [],
expectedErrors: [
`[copy-webpack-plugin] unable to locate 'nonexistent' at '${HELPER_DIR}${
path.sep
}nonexistent'`,
expectedWarnings: [
new Error(
`[copy-webpack-plugin] unable to locate 'nonexistent' at '${HELPER_DIR}${
path.sep
}nonexistent'`
),
],
patterns: [
{
Expand Down

0 comments on commit a1c5372

Please sign in to comment.