Skip to content

Commit 0088530

Browse files
refactor: code (#486)
1 parent 34a63c7 commit 0088530

File tree

1 file changed

+48
-43
lines changed

1 file changed

+48
-43
lines changed

src/index.js

Lines changed: 48 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,10 @@ class CopyPlugin {
3737
output: compiler.options.output.path,
3838
};
3939

40+
let assets;
41+
4042
try {
41-
const assets = await Promise.all(
43+
assets = await Promise.all(
4244
this.patterns.map((pattern) =>
4345
limit(async () => {
4446
const patternAfterPreProcess = await preProcessPattern(
@@ -69,62 +71,65 @@ class CopyPlugin {
6971
})
7072
)
7173
);
74+
} catch (error) {
75+
compilation.errors.push(error);
7276

73-
// Avoid writing assets inside `p-limit`, because it creates concurrency.
74-
// It could potentially lead to an error - "Multiple assets emit different content to the same filename"
75-
assets
76-
.reduce((acc, val) => acc.concat(val), [])
77-
.filter(Boolean)
78-
.forEach((asset) => {
79-
const {
80-
absoluteFrom,
81-
targetPath,
82-
webpackTo,
83-
source,
84-
force,
85-
} = asset;
86-
87-
// For old version webpack 4
88-
/* istanbul ignore if */
89-
if (typeof compilation.emitAsset !== 'function') {
90-
// eslint-disable-next-line no-param-reassign
91-
compilation.assets[targetPath] = source;
92-
93-
return;
94-
}
95-
96-
if (compilation.getAsset(targetPath)) {
97-
if (force) {
98-
logger.log(
99-
`force updating '${webpackTo}' to compilation assets from '${absoluteFrom}'`
100-
);
101-
102-
compilation.updateAsset(targetPath, source);
77+
callback();
10378

104-
return;
105-
}
79+
return;
80+
}
10681

82+
// Avoid writing assets inside `p-limit`, because it creates concurrency.
83+
// It could potentially lead to an error - "Multiple assets emit different content to the same filename"
84+
assets
85+
.reduce((acc, val) => acc.concat(val), [])
86+
.filter(Boolean)
87+
.forEach((asset) => {
88+
const {
89+
absoluteFrom,
90+
targetPath,
91+
webpackTo,
92+
source,
93+
force,
94+
} = asset;
95+
96+
// For old version webpack 4
97+
/* istanbul ignore if */
98+
if (typeof compilation.emitAsset !== 'function') {
99+
// eslint-disable-next-line no-param-reassign
100+
compilation.assets[targetPath] = source;
101+
102+
return;
103+
}
104+
105+
if (compilation.getAsset(targetPath)) {
106+
if (force) {
107107
logger.log(
108-
`skipping '${webpackTo}', because it already exists`
108+
`force updating '${webpackTo}' to compilation assets from '${absoluteFrom}'`
109109
);
110110

111+
compilation.updateAsset(targetPath, source);
112+
111113
return;
112114
}
113115

114116
logger.log(
115-
`writing '${webpackTo}' to compilation assets from '${absoluteFrom}'`
117+
`skipping '${webpackTo}', because it already exists`
116118
);
117119

118-
compilation.emitAsset(targetPath, source);
119-
});
120+
return;
121+
}
120122

121-
logger.debug('end to adding additional assets');
123+
logger.log(
124+
`writing '${webpackTo}' to compilation assets from '${absoluteFrom}'`
125+
);
122126

123-
callback();
124-
} catch (error) {
125-
compilation.errors.push(error);
126-
callback();
127-
}
127+
compilation.emitAsset(targetPath, source);
128+
});
129+
130+
logger.debug('end to adding additional assets');
131+
132+
callback();
128133
}
129134
);
130135
});

0 commit comments

Comments
 (0)