Skip to content

Returning empty string from either assertPath or publicUrl allows to skip that asset from download #2

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ If `assetPath` is a function, it will be invoked for each asset detected with tw
- `entry` (Object): The entry in which the asset was detected
- `asset` (Object): The normalized asset object

Its return value will be used as the path for the asset.
Its return value will be used as the path for the asset. If its return empty string then the asset is skipped from donwload.

### `publicUrl`

Expand All @@ -55,7 +55,7 @@ If `publicUrl` is a function, it will be invoked for each asset detected with tw
- `asset` (Object): The normalized asset object
- `assetPath` (String): The local path where the asset has been saved

Its return value will replace the original URL of the asset in any objects.
Its return value will replace the original URL of the asset in any objects. If its return empty string then the asset is skipped from donwload.

### 👀 Example configuration

Expand Down
12 changes: 9 additions & 3 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,11 @@ module.exports.transform = async ({ data, debug, log, options }) => {
? options.publicUrl(null, entry, assetPath)
: options.publicUrl;

result.set(regExp, { assetPath, publicUrl });

if (assetPath && publicUrl) {
debug("Found asset %s, %s", assetPath, publicUrl);
result.set(regExp, { assetPath, publicUrl });
}

return result;
}, new Map());
Expand All @@ -62,8 +66,10 @@ module.exports.transform = async ({ data, debug, log, options }) => {
? options.publicUrl(entry, value, assetPath)
: options.publicUrl;

filesToDownload[value.url] = path.join(process.cwd(), assetPath);
replacedFields[fieldName] = publicUrl;
if (assetPath && publicUrl) {
filesToDownload[value.url] = path.join(process.cwd(), assetPath);
replacedFields[fieldName] = publicUrl;
}
} else if (typeof entry[fieldName] === "string") {
assets.forEach(({ assetPath, publicUrl }, expression) => {
let isMatch = false;
Expand Down