Skip to content

Commit 4cea28b

Browse files
refactor: next
BREAKING CHANGE: yes
1 parent 6c11e21 commit 4cea28b

18 files changed

+1199
-2328
lines changed

.github/workflows/nodejs.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ jobs:
5656
matrix:
5757
os: [ubuntu-latest, windows-latest, macos-latest]
5858
node-version: [10.x, 12.x, 14.x]
59-
webpack-version: [4, latest]
59+
webpack-version: [latest]
6060

6161
runs-on: ${{ matrix.os }}
6262

README.md

Lines changed: 124 additions & 148 deletions
Large diffs are not rendered by default.

package-lock.json

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

package.json

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,20 +38,17 @@
3838
"dist"
3939
],
4040
"peerDependencies": {
41-
"webpack": "^4.37.0 || ^5.0.0"
41+
"webpack": "^5.1.0"
4242
},
4343
"dependencies": {
44-
"cacache": "^15.0.5",
4544
"fast-glob": "^3.2.4",
46-
"find-cache-dir": "^3.3.1",
4745
"glob-parent": "^5.1.1",
4846
"globby": "^11.0.1",
4947
"loader-utils": "^2.0.0",
5048
"normalize-path": "^3.0.0",
5149
"p-limit": "^3.0.2",
5250
"schema-utils": "^3.0.0",
53-
"serialize-javascript": "^5.0.1",
54-
"webpack-sources": "^1.4.3"
51+
"serialize-javascript": "^5.0.1"
5552
},
5653
"devDependencies": {
5754
"@babel/cli": "^7.12.1",
@@ -62,7 +59,6 @@
6259
"@webpack-contrib/defaults": "^6.3.0",
6360
"@webpack-contrib/eslint-config-webpack": "^3.0.0",
6461
"babel-jest": "^26.6.3",
65-
"chokidar": "^3.4.3",
6662
"cross-env": "^7.0.2",
6763
"del": "^6.0.0",
6864
"del-cli": "^3.0.1",
@@ -79,7 +75,7 @@
7975
"npm-run-all": "^4.1.5",
8076
"prettier": "^2.1.2",
8177
"standard-version": "^9.0.0",
82-
"webpack": "^5.4.0"
78+
"webpack": "^5.10.0"
8379
},
8480
"keywords": [
8581
"webpack",

src/index.js

Lines changed: 32 additions & 116 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,10 @@
11
import path from "path";
2-
import os from "os";
32
import crypto from "crypto";
43

5-
import webpack from "webpack";
64
import { validate } from "schema-utils";
75
import pLimit from "p-limit";
86
import globby from "globby";
9-
import findCacheDir from "find-cache-dir";
107
import serialize from "serialize-javascript";
11-
import cacache from "cacache";
128
import loaderUtils from "loader-utils";
139
import normalizePath from "normalize-path";
1410
import globParent from "glob-parent";
@@ -19,11 +15,6 @@ import { version } from "../package.json";
1915
import schema from "./options.json";
2016
import { readFile, stat } from "./utils/promisify";
2117

22-
// webpack 5 exposes the sources property to ensure the right version of webpack-sources is used
23-
const { RawSource } =
24-
// eslint-disable-next-line global-require
25-
webpack.sources || require("webpack-sources");
26-
2718
const template = /(\[ext\])|(\[name\])|(\[path\])|(\[folder\])|(\[emoji(?::(\d+))?\])|(\[(?:([^:\]]+):)?(?:hash|contenthash)(?::([a-z]+\d*))?(?::(\d+))?\])|(\[\d+\])/;
2819

2920
class CopyPlugin {
@@ -38,10 +29,6 @@ class CopyPlugin {
3829
}
3930

4031
static async createSnapshot(compilation, startTime, dependency) {
41-
if (!compilation.fileSystemInfo) {
42-
return;
43-
}
44-
4532
// eslint-disable-next-line consistent-return
4633
return new Promise((resolve, reject) => {
4734
compilation.fileSystemInfo.createSnapshot(
@@ -66,10 +53,6 @@ class CopyPlugin {
6653
}
6754

6855
static async checkSnapshotValid(compilation, snapshot) {
69-
if (!compilation.fileSystemInfo) {
70-
return;
71-
}
72-
7356
// eslint-disable-next-line consistent-return
7457
return new Promise((resolve, reject) => {
7558
compilation.fileSystemInfo.checkSnapshotValid(
@@ -95,6 +78,7 @@ class CopyPlugin {
9578
inputPattern,
9679
index
9780
) {
81+
const { RawSource } = compiler.webpack.sources;
9882
const pattern =
9983
typeof inputPattern === "string"
10084
? { from: inputPattern }
@@ -111,6 +95,13 @@ class CopyPlugin {
11195
: pattern.compilerContext
11296
);
11397

98+
pattern.transform =
99+
typeof pattern.transform !== "undefined"
100+
? typeof pattern.transform === "function"
101+
? { transformer: pattern.transform }
102+
: pattern.transform
103+
: {};
104+
114105
logger.log(
115106
`starting to process a pattern from '${pattern.from}' using '${pattern.context}' context`
116107
);
@@ -152,17 +143,7 @@ class CopyPlugin {
152143
...{ cwd: pattern.context, objectMode: true },
153144
};
154145

155-
// TODO remove after drop webpack@4
156-
if (
157-
inputFileSystem.lstat &&
158-
inputFileSystem.stat &&
159-
inputFileSystem.lstatSync &&
160-
inputFileSystem.statSync &&
161-
inputFileSystem.readdir &&
162-
inputFileSystem.readdirSync
163-
) {
164-
pattern.globOptions.fs = inputFileSystem;
165-
}
146+
pattern.globOptions.fs = inputFileSystem;
166147

167148
switch (pattern.fromType) {
168149
case "dir":
@@ -327,9 +308,7 @@ class CopyPlugin {
327308
`'to' option '${pattern.to}' determinated as '${pattern.toType}'`
328309
);
329310

330-
const relativeFrom = pattern.flatten
331-
? path.basename(absoluteFilename)
332-
: path.relative(pattern.context, absoluteFilename);
311+
const relativeFrom = path.relative(pattern.context, absoluteFilename);
333312
let filename =
334313
pattern.toType === "dir"
335314
? path.join(pattern.to, relativeFrom)
@@ -481,67 +460,41 @@ class CopyPlugin {
481460
}
482461
}
483462

484-
if (pattern.transform) {
463+
if (pattern.transform.transformer) {
485464
logger.log(`transforming content for '${absoluteFilename}'...`);
486465

487466
const buffer = result.source.source();
488467

489-
if (pattern.cacheTransform) {
468+
if (pattern.transform.cache) {
490469
const defaultCacheKeys = {
491470
version,
492471
sourceFilename,
493-
transform: pattern.transform,
472+
transform: pattern.transform.transformer,
494473
contentHash: crypto
495474
.createHash("md4")
496475
.update(buffer)
497476
.digest("hex"),
498477
index,
499478
};
500479
const cacheKeys = `transform|${serialize(
501-
typeof pattern.cacheTransform.keys === "function"
502-
? await pattern.cacheTransform.keys(
480+
typeof pattern.transform.cache.keys === "function"
481+
? await pattern.transform.cache.keys(
503482
defaultCacheKeys,
504483
absoluteFilename
505484
)
506-
: { ...defaultCacheKeys, ...pattern.cacheTransform.keys }
485+
: { ...defaultCacheKeys, ...pattern.transform.cache.keys }
507486
)}`;
508487

509-
let cacheItem;
510-
let cacheDirectory;
511-
512488
logger.debug(
513489
`getting transformation cache for '${absoluteFilename}'...`
514490
);
515491

516-
// webpack@5 API
517-
if (cache) {
518-
cacheItem = cache.getItemCache(
519-
cacheKeys,
520-
cache.getLazyHashedEtag(result.source)
521-
);
522-
523-
result.source = await cacheItem.getPromise();
524-
} else {
525-
cacheDirectory = pattern.cacheTransform.directory
526-
? pattern.cacheTransform.directory
527-
: typeof pattern.cacheTransform === "string"
528-
? pattern.cacheTransform
529-
: findCacheDir({ name: "copy-webpack-plugin" }) ||
530-
os.tmpdir();
531-
532-
let cached;
492+
const cacheItem = cache.getItemCache(
493+
cacheKeys,
494+
cache.getLazyHashedEtag(result.source)
495+
);
533496

534-
try {
535-
cached = await cacache.get(cacheDirectory, cacheKeys);
536-
} catch (error) {
537-
logger.debug(
538-
`no transformation cache for '${absoluteFilename}'...`
539-
);
540-
}
541-
542-
// eslint-disable-next-line no-undefined
543-
result.source = cached ? new RawSource(cached.data) : undefined;
544-
}
497+
result.source = await cacheItem.getPromise();
545498

546499
logger.debug(
547500
result.source
@@ -550,7 +503,7 @@ class CopyPlugin {
550503
);
551504

552505
if (!result.source) {
553-
const transformed = await pattern.transform(
506+
const transformed = await pattern.transform.transformer(
554507
buffer,
555508
absoluteFilename
556509
);
@@ -561,24 +514,13 @@ class CopyPlugin {
561514
`caching transformation for '${absoluteFilename}'...`
562515
);
563516

564-
// webpack@5 API
565-
if (cache) {
566-
await cacheItem.storePromise(result.source);
567-
} else {
568-
try {
569-
await cacache.put(cacheDirectory, cacheKeys, transformed);
570-
} catch (error) {
571-
compilation.errors.push(error);
572-
573-
return;
574-
}
575-
}
517+
await cacheItem.storePromise(result.source);
576518

577519
logger.debug(`cached transformation for '${absoluteFilename}'`);
578520
}
579521
} else {
580522
result.source = new RawSource(
581-
await pattern.transform(buffer, absoluteFilename)
523+
await pattern.transform.transformer(buffer, absoluteFilename)
582524
);
583525
}
584526
}
@@ -619,23 +561,6 @@ class CopyPlugin {
619561
);
620562
}
621563

622-
if (pattern.transformPath) {
623-
logger.log(
624-
`transforming '${result.filename}' for '${absoluteFilename}'...`
625-
);
626-
627-
// eslint-disable-next-line no-param-reassign
628-
result.immutable = false;
629-
// eslint-disable-next-line no-param-reassign
630-
result.filename = await pattern.transformPath(
631-
result.filename,
632-
absoluteFilename
633-
);
634-
logger.log(
635-
`transformed new '${result.filename}' for '${absoluteFilename}'...`
636-
);
637-
}
638-
639564
// eslint-disable-next-line no-param-reassign
640565
result.filename = normalizePath(result.filename);
641566

@@ -663,14 +588,14 @@ class CopyPlugin {
663588

664589
compiler.hooks.thisCompilation.tap(pluginName, (compilation) => {
665590
const logger = compilation.getLogger("copy-webpack-plugin");
666-
const cache = compilation.getCache
667-
? compilation.getCache("CopyWebpackPlugin")
668-
: // eslint-disable-next-line no-undefined
669-
undefined;
670-
671-
compilation.hooks.additionalAssets.tapAsync(
672-
"copy-webpack-plugin",
673-
async (callback) => {
591+
const cache = compilation.getCache("CopyWebpackPlugin");
592+
593+
compilation.hooks.processAssets.tapAsync(
594+
{
595+
name: "copy-webpack-plugin",
596+
stage: compiler.webpack.Compilation.PROCESS_ASSETS_STAGE_ADDITIONS,
597+
},
598+
async (unusedAssets, callback) => {
674599
logger.log("starting to add additional assets...");
675600

676601
let assets;
@@ -712,15 +637,6 @@ class CopyPlugin {
712637
force,
713638
} = asset;
714639

715-
// For old version webpack 4
716-
/* istanbul ignore if */
717-
if (typeof compilation.emitAsset !== "function") {
718-
// eslint-disable-next-line no-param-reassign
719-
compilation.assets[filename] = source;
720-
721-
return;
722-
}
723-
724640
const existingAsset = compilation.getAsset(filename);
725641

726642
if (existingAsset) {

src/options.json

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -43,36 +43,39 @@
4343
}
4444
]
4545
},
46-
"flatten": {
47-
"type": "boolean"
48-
},
4946
"transform": {
50-
"instanceof": "Function"
51-
},
52-
"cacheTransform": {
5347
"anyOf": [
5448
{
55-
"type": "boolean"
56-
},
57-
{
58-
"type": "string"
49+
"instanceof": "Function"
5950
},
6051
{
6152
"type": "object",
6253
"additionalProperties": false,
6354
"properties": {
64-
"directory": {
65-
"type": "string",
66-
"absolutePath": true
55+
"transformer": {
56+
"instanceof": "Function"
6757
},
68-
"keys": {
58+
"cache": {
6959
"anyOf": [
7060
{
71-
"type": "object",
72-
"additionalProperties": true
61+
"type": "boolean"
7362
},
7463
{
75-
"instanceof": "Function"
64+
"type": "object",
65+
"additionalProperties": false,
66+
"properties": {
67+
"keys": {
68+
"anyOf": [
69+
{
70+
"type": "object",
71+
"additionalProperties": true
72+
},
73+
{
74+
"instanceof": "Function"
75+
}
76+
]
77+
}
78+
}
7679
}
7780
]
7881
}

0 commit comments

Comments
 (0)