Skip to content

Commit

Permalink
refactor: code
Browse files Browse the repository at this point in the history
  • Loading branch information
cap-Bernardito committed Mar 3, 2021
1 parent 244494c commit 88809c4
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 106 deletions.
101 changes: 23 additions & 78 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Expand Up @@ -47,7 +47,7 @@
"glob-parent": "^5.1.1",
"globby": "^11.0.2",
"normalize-path": "^3.0.0",
"p-queue": "^6.6.2",
"p-limit": "^3.1.0",
"schema-utils": "^3.0.0",
"serialize-javascript": "^5.0.1"
},
Expand Down
61 changes: 34 additions & 27 deletions src/index.js
Expand Up @@ -2,7 +2,7 @@ import path from "path";
import crypto from "crypto";

import { validate } from "schema-utils";
import PQueue from "p-queue";
import pLimit from "p-limit";
import globby from "globby";
import serialize from "serialize-javascript";
import normalizePath from "normalize-path";
Expand Down Expand Up @@ -70,6 +70,7 @@ class CopyPlugin {
}

static async runPattern(
assetMap,
compiler,
compilation,
logger,
Expand Down Expand Up @@ -313,7 +314,13 @@ class CopyPlugin {
path.relative(compiler.context, absoluteFilename)
);

return { absoluteFilename, sourceFilename, filename, toType };
return {
absoluteFilename,
sourceFilename,
filename,
toType,
priority: pattern.priority || 0,
};
})
);

Expand All @@ -322,7 +329,13 @@ class CopyPlugin {
try {
assets = await Promise.all(
files.map(async (file) => {
const { absoluteFilename, sourceFilename, filename, toType } = file;
const {
absoluteFilename,
sourceFilename,
filename,
toType,
priority,
} = file;
const info =
typeof pattern.info === "function"
? pattern.info(file) || {}
Expand Down Expand Up @@ -578,6 +591,12 @@ class CopyPlugin {
result.filename = normalizePath(result.filename);
}

if (!assetMap.has(priority)) {
assetMap.set(priority, []);
}

assetMap.get(priority).push(result);

// eslint-disable-next-line consistent-return
return result;
})
Expand All @@ -598,6 +617,7 @@ class CopyPlugin {

apply(compiler) {
const pluginName = this.constructor.name;
const limit = pLimit(this.options.concurrency || 100);

compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
const logger = compilation.getLogger("copy-webpack-plugin");
Expand All @@ -611,39 +631,24 @@ class CopyPlugin {
async (unusedAssets, callback) => {
logger.log("starting to add additional assets...");

const queue = new PQueue({
concurrency: this.options.concurrency || 100,
autoStart: false,
});
const tasks = [];
const assetMap = new Map();

this.patterns.forEach((item, index) => {
queue.add(
async () => {
tasks.push(
try {
await Promise.all(
this.patterns.map((item, index) =>
limit(async () =>
CopyPlugin.runPattern(
assetMap,
compiler,
compilation,
logger,
cache,
item,
index
)
);
},
{
priority:
typeof item.priority === "undefined" ? 0 : item.priority,
}
)
)
);
});

await queue.start().onIdle();

let assets;

try {
assets = await Promise.all(tasks);
} catch (error) {
compilation.errors.push(error);

Expand All @@ -652,10 +657,12 @@ class CopyPlugin {
return;
}

const assets = [...assetMap.entries()].sort((a, b) => b[0] - a[0]);

// Avoid writing assets inside `p-limit`, because it creates concurrency.
// It could potentially lead to an error - 'Multiple assets emit different content to the same filename'
assets
.reduce((acc, val) => acc.concat(val), [])
.reduce((acc, val) => acc.concat(val[1]), [])
.filter(Boolean)
.forEach((asset) => {
const {
Expand Down

0 comments on commit 88809c4

Please sign in to comment.