From 7045b413c5c3c957159c4550c473b7d9d4b2d570 Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sat, 26 Oct 2024 20:37:51 -0700 Subject: [PATCH 1/5] chore: set onlyFiles glob option --- src/index.js | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/src/index.js b/src/index.js index de80f1d..812295d 100644 --- a/src/index.js +++ b/src/index.js @@ -340,9 +340,11 @@ class CopyPlugin { /** @type {GlobbyOptions & { objectMode: true }} */ const globOptions = { - ...{ followSymbolicLinks: true }, + followSymbolicLinks: true, ...(pattern.globOptions || {}), - ...{ cwd: pattern.context, objectMode: true }, + cwd: pattern.context, + objectMode: true, + onlyFiles: true, }; // @ts-ignore @@ -412,7 +414,9 @@ class CopyPlugin { let globEntries; try { - globEntries = await globby(glob, globOptions); + globEntries = globOptions.onlyDirectories + ? [] + : await globby(glob, globOptions); } catch (error) { compilation.errors.push(/** @type {WebpackError} */ (error)); @@ -448,11 +452,6 @@ class CopyPlugin { * @returns {Promise} */ async (globEntry) => { - // Exclude directories - if (!globEntry.dirent.isFile()) { - return; - } - if (pattern.filter) { let isFiltered; From f8adcc43ba9b71f8591a08714ebeff19c0a0953f Mon Sep 17 00:00:00 2001 From: Ben McCann <322311+benmccann@users.noreply.github.com> Date: Sat, 26 Oct 2024 21:20:28 -0700 Subject: [PATCH 2/5] chore: disable glob objectMode --- src/index.js | 15 +++++++-------- types/index.d.ts | 3 --- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/src/index.js b/src/index.js index 812295d..d369bc1 100644 --- a/src/index.js +++ b/src/index.js @@ -43,7 +43,6 @@ const getGlobby = memoize(async () => { /** @typedef {import("webpack").WebpackError} WebpackError */ /** @typedef {import("webpack").Asset} Asset */ /** @typedef {import("globby").Options} GlobbyOptions */ -/** @typedef {import("globby").GlobEntry} GlobEntry */ /** @typedef {ReturnType} WebpackLogger */ /** @typedef {ReturnType} CacheFacade */ /** @typedef {ReturnType["getLazyHashedEtag"]>} Etag */ @@ -338,12 +337,12 @@ class CopyPlugin { logger.debug(`determined '${absoluteFrom}' is a glob`); } - /** @type {GlobbyOptions & { objectMode: true }} */ + /** @type {GlobbyOptions} */ const globOptions = { followSymbolicLinks: true, ...(pattern.globOptions || {}), cwd: pattern.context, - objectMode: true, + objectMode: false, onlyFiles: true, }; @@ -409,7 +408,7 @@ class CopyPlugin { logger.log(`begin globbing '${glob}'...`); /** - * @type {GlobEntry[]} + * @type {string[]} */ let globEntries; @@ -448,7 +447,7 @@ class CopyPlugin { copiedResult = await Promise.all( globEntries.map( /** - * @param {GlobEntry} globEntry + * @param {string} globEntry * @returns {Promise} */ async (globEntry) => { @@ -456,7 +455,7 @@ class CopyPlugin { let isFiltered; try { - isFiltered = await pattern.filter(globEntry.path); + isFiltered = await pattern.filter(globEntry); } catch (error) { compilation.errors.push(/** @type {WebpackError} */ (error)); @@ -464,13 +463,13 @@ class CopyPlugin { } if (!isFiltered) { - logger.log(`skip '${globEntry.path}', because it was filtered`); + logger.log(`skip '${globEntry}', because it was filtered`); return; } } - const from = globEntry.path; + const from = globEntry; logger.debug(`found '${from}'`); diff --git a/types/index.d.ts b/types/index.d.ts index 2d8a973..97267cc 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -5,7 +5,6 @@ export = CopyPlugin; /** @typedef {import("webpack").WebpackError} WebpackError */ /** @typedef {import("webpack").Asset} Asset */ /** @typedef {import("globby").Options} GlobbyOptions */ -/** @typedef {import("globby").GlobEntry} GlobEntry */ /** @typedef {ReturnType} WebpackLogger */ /** @typedef {ReturnType} CacheFacade */ /** @typedef {ReturnType["getLazyHashedEtag"]>} Etag */ @@ -165,7 +164,6 @@ declare namespace CopyPlugin { WebpackError, Asset, GlobbyOptions, - GlobEntry, WebpackLogger, CacheFacade, Etag, @@ -198,7 +196,6 @@ type Compilation = import("webpack").Compilation; type WebpackError = import("webpack").WebpackError; type Asset = import("webpack").Asset; type GlobbyOptions = import("globby").Options; -type GlobEntry = import("globby").GlobEntry; type WebpackLogger = ReturnType; type CacheFacade = ReturnType; type Etag = ReturnType< From 789eb7214ae9131aab3929ebb3ef171f1da23c05 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Wed, 30 Oct 2024 16:42:43 +0300 Subject: [PATCH 3/5] refactor: code --- README.md | 4 ++++ src/index.js | 4 +--- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c49d4c2..7d4709c 100644 --- a/README.md +++ b/README.md @@ -340,6 +340,10 @@ The use of `context` is illustrated by these [`examples`](#examples). #### `globOptions` +> [!WARNING] +> +> The *onlyDirectories* does not work because the plugin is designed to copy files. + Type: ```ts diff --git a/src/index.js b/src/index.js index d369bc1..250933d 100644 --- a/src/index.js +++ b/src/index.js @@ -413,9 +413,7 @@ class CopyPlugin { let globEntries; try { - globEntries = globOptions.onlyDirectories - ? [] - : await globby(glob, globOptions); + globEntries = await globby(glob, globOptions); } catch (error) { compilation.errors.push(/** @type {WebpackError} */ (error)); From b17e345e8d163b39329bcf4a65f77b5d0cce3662 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Wed, 30 Oct 2024 16:44:41 +0300 Subject: [PATCH 4/5] style: fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 7d4709c..fb9f9bd 100644 --- a/README.md +++ b/README.md @@ -342,7 +342,7 @@ The use of `context` is illustrated by these [`examples`](#examples). > [!WARNING] > -> The *onlyDirectories* does not work because the plugin is designed to copy files. +> The _onlyDirectories_ does not work because the plugin is designed to copy files. Type: From 6fde5923c59af8c4499f21615ab0aa85b37a7d98 Mon Sep 17 00:00:00 2001 From: "alexander.akait" Date: Wed, 30 Oct 2024 16:46:54 +0300 Subject: [PATCH 5/5] test: fix --- test/globOptions-option.test.js | 17 ----------------- 1 file changed, 17 deletions(-) diff --git a/test/globOptions-option.test.js b/test/globOptions-option.test.js index 0db6709..eda0fa7 100644 --- a/test/globOptions-option.test.js +++ b/test/globOptions-option.test.js @@ -400,23 +400,6 @@ describe("globOptions option", () => { .catch(done); }); - it('should work with the "onlyDirectories" option', (done) => { - runEmit({ - expectedAssetKeys: [], - patterns: [ - { - noErrorOnMissing: true, - from: "directory", - globOptions: { - onlyDirectories: true, - }, - }, - ], - }) - .then(done) - .catch(done); - }); - it("should emit error when not found assets for copy", (done) => { expect.assertions(1);