Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: simplify glob usage #789

Merged
merged 6 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[]}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

whats the typedef change here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

objectMode: true changes the return type from the default string[] to GlobEntry[]. Since we've removed objectMode: true we can go back to string[], which is simpler and has better performance

*/
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