Skip to content

Commit a146720

Browse files
authored
refactor: simplify glob usage (#789)
1 parent 9048948 commit a146720

File tree

4 files changed

+14
-34
lines changed

4 files changed

+14
-34
lines changed

README.md

+4
Original file line numberDiff line numberDiff line change
@@ -340,6 +340,10 @@ The use of `context` is illustrated by these [`examples`](#examples).
340340

341341
#### `globOptions`
342342

343+
> [!WARNING]
344+
>
345+
> The _onlyDirectories_ does not work because the plugin is designed to copy files.
346+
343347
Type:
344348

345349
```ts

src/index.js

+10-14
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,6 @@ const getGlobby = memoize(async () => {
4343
/** @typedef {import("webpack").WebpackError} WebpackError */
4444
/** @typedef {import("webpack").Asset} Asset */
4545
/** @typedef {import("globby").Options} GlobbyOptions */
46-
/** @typedef {import("globby").GlobEntry} GlobEntry */
4746
/** @typedef {ReturnType<Compilation["getLogger"]>} WebpackLogger */
4847
/** @typedef {ReturnType<Compilation["getCache"]>} CacheFacade */
4948
/** @typedef {ReturnType<ReturnType<Compilation["getCache"]>["getLazyHashedEtag"]>} Etag */
@@ -338,11 +337,13 @@ class CopyPlugin {
338337
logger.debug(`determined '${absoluteFrom}' is a glob`);
339338
}
340339

341-
/** @type {GlobbyOptions & { objectMode: true }} */
340+
/** @type {GlobbyOptions} */
342341
const globOptions = {
343-
...{ followSymbolicLinks: true },
342+
followSymbolicLinks: true,
344343
...(pattern.globOptions || {}),
345-
...{ cwd: pattern.context, objectMode: true },
344+
cwd: pattern.context,
345+
objectMode: false,
346+
onlyFiles: true,
346347
};
347348

348349
// @ts-ignore
@@ -407,7 +408,7 @@ class CopyPlugin {
407408
logger.log(`begin globbing '${glob}'...`);
408409

409410
/**
410-
* @type {GlobEntry[]}
411+
* @type {string[]}
411412
*/
412413
let globEntries;
413414

@@ -444,34 +445,29 @@ class CopyPlugin {
444445
copiedResult = await Promise.all(
445446
globEntries.map(
446447
/**
447-
* @param {GlobEntry} globEntry
448+
* @param {string} globEntry
448449
* @returns {Promise<CopiedResult | undefined>}
449450
*/
450451
async (globEntry) => {
451-
// Exclude directories
452-
if (!globEntry.dirent.isFile()) {
453-
return;
454-
}
455-
456452
if (pattern.filter) {
457453
let isFiltered;
458454

459455
try {
460-
isFiltered = await pattern.filter(globEntry.path);
456+
isFiltered = await pattern.filter(globEntry);
461457
} catch (error) {
462458
compilation.errors.push(/** @type {WebpackError} */ (error));
463459

464460
return;
465461
}
466462

467463
if (!isFiltered) {
468-
logger.log(`skip '${globEntry.path}', because it was filtered`);
464+
logger.log(`skip '${globEntry}', because it was filtered`);
469465

470466
return;
471467
}
472468
}
473469

474-
const from = globEntry.path;
470+
const from = globEntry;
475471

476472
logger.debug(`found '${from}'`);
477473

test/globOptions-option.test.js

-17
Original file line numberDiff line numberDiff line change
@@ -400,23 +400,6 @@ describe("globOptions option", () => {
400400
.catch(done);
401401
});
402402

403-
it('should work with the "onlyDirectories" option', (done) => {
404-
runEmit({
405-
expectedAssetKeys: [],
406-
patterns: [
407-
{
408-
noErrorOnMissing: true,
409-
from: "directory",
410-
globOptions: {
411-
onlyDirectories: true,
412-
},
413-
},
414-
],
415-
})
416-
.then(done)
417-
.catch(done);
418-
});
419-
420403
it("should emit error when not found assets for copy", (done) => {
421404
expect.assertions(1);
422405

types/index.d.ts

-3
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ export = CopyPlugin;
55
/** @typedef {import("webpack").WebpackError} WebpackError */
66
/** @typedef {import("webpack").Asset} Asset */
77
/** @typedef {import("globby").Options} GlobbyOptions */
8-
/** @typedef {import("globby").GlobEntry} GlobEntry */
98
/** @typedef {ReturnType<Compilation["getLogger"]>} WebpackLogger */
109
/** @typedef {ReturnType<Compilation["getCache"]>} CacheFacade */
1110
/** @typedef {ReturnType<ReturnType<Compilation["getCache"]>["getLazyHashedEtag"]>} Etag */
@@ -165,7 +164,6 @@ declare namespace CopyPlugin {
165164
WebpackError,
166165
Asset,
167166
GlobbyOptions,
168-
GlobEntry,
169167
WebpackLogger,
170168
CacheFacade,
171169
Etag,
@@ -198,7 +196,6 @@ type Compilation = import("webpack").Compilation;
198196
type WebpackError = import("webpack").WebpackError;
199197
type Asset = import("webpack").Asset;
200198
type GlobbyOptions = import("globby").Options;
201-
type GlobEntry = import("globby").GlobEntry;
202199
type WebpackLogger = ReturnType<Compilation["getLogger"]>;
203200
type CacheFacade = ReturnType<Compilation["getCache"]>;
204201
type Etag = ReturnType<

0 commit comments

Comments
 (0)