Skip to content

Commit

Permalink
refactor: simplify glob usage (#789)
Browse files Browse the repository at this point in the history
  • Loading branch information
benmccann authored Oct 30, 2024
1 parent 9048948 commit a146720
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 34 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
24 changes: 10 additions & 14 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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<Compilation["getLogger"]>} WebpackLogger */
/** @typedef {ReturnType<Compilation["getCache"]>} CacheFacade */
/** @typedef {ReturnType<ReturnType<Compilation["getCache"]>["getLazyHashedEtag"]>} Etag */
Expand Down Expand Up @@ -338,11 +337,13 @@ class CopyPlugin {
logger.debug(`determined '${absoluteFrom}' is a glob`);
}

/** @type {GlobbyOptions & { objectMode: true }} */
/** @type {GlobbyOptions} */
const globOptions = {
...{ followSymbolicLinks: true },
followSymbolicLinks: true,
...(pattern.globOptions || {}),
...{ cwd: pattern.context, objectMode: true },
cwd: pattern.context,
objectMode: false,
onlyFiles: true,
};

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

/**
* @type {GlobEntry[]}
* @type {string[]}
*/
let globEntries;

Expand Down Expand Up @@ -444,34 +445,29 @@ class CopyPlugin {
copiedResult = await Promise.all(
globEntries.map(
/**
* @param {GlobEntry} globEntry
* @param {string} globEntry
* @returns {Promise<CopiedResult | undefined>}
*/
async (globEntry) => {
// Exclude directories
if (!globEntry.dirent.isFile()) {
return;
}

if (pattern.filter) {
let isFiltered;

try {
isFiltered = await pattern.filter(globEntry.path);
isFiltered = await pattern.filter(globEntry);
} catch (error) {
compilation.errors.push(/** @type {WebpackError} */ (error));

return;
}

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}'`);

Expand Down
17 changes: 0 additions & 17 deletions test/globOptions-option.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
3 changes: 0 additions & 3 deletions types/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Compilation["getLogger"]>} WebpackLogger */
/** @typedef {ReturnType<Compilation["getCache"]>} CacheFacade */
/** @typedef {ReturnType<ReturnType<Compilation["getCache"]>["getLazyHashedEtag"]>} Etag */
Expand Down Expand Up @@ -165,7 +164,6 @@ declare namespace CopyPlugin {
WebpackError,
Asset,
GlobbyOptions,
GlobEntry,
WebpackLogger,
CacheFacade,
Etag,
Expand Down Expand Up @@ -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<Compilation["getLogger"]>;
type CacheFacade = ReturnType<Compilation["getCache"]>;
type Etag = ReturnType<
Expand Down

0 comments on commit a146720

Please sign in to comment.