Skip to content

Enable filtering by file extensions #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 1 commit 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ You can use this plugin to copy files in your Gatsby project. Specify absolute `
|`source` |Absolute path of the source (eg: `${__dirname}/path`)|
|`destination`|Relative path to destination within `public/` direcotry </br> You can add `/*/` inside destination path to wildcard destination lookup </br> *Refer to [graphical explanation](#graphical-explanation) for better understanding*
|`purge` |Overwrite destination. `false` by default|
|`extensions` |Filter files to copy from their extension `null` by default, Provide an array if you wish to filter extensions (eg: `['png', 'jpg']`)|

#### Important
This plugin should be used with `gatsby-source-filesystem`
Expand All @@ -36,6 +37,14 @@ This plugin should be used with `gatsby-source-filesystem`
destination: '/containers/*/images',
purge: false,
}
}, {
resolve: 'gatsby-plugin-copy-files-enhanced',
options: {
source: `${__dirname}/src/content` ,
destination: '/images',
purge: true,
extensions: ['png', 'jpg'],
}
}
```

Expand Down
5 changes: 4 additions & 1 deletion gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,14 @@ const getDirectories = source =>
const regex1 = RegExp('(.*)\\/\\*(.*)');

exports.onCreateNode = ({ node }, pluginOptions) => {
const { source, destination = '', purge = false } = pluginOptions;
const { source, destination = '', purge = false, extensions = null } = pluginOptions;
const sourceNormalized = path.normalize(source);
if (node.internal.type === 'File') {
const dir = path.normalize(node.dir);
if (dir.includes(sourceNormalized)) {
// if extensions are specified, do not copy this file if the extension is not in the provided list
if (extensions && !extensions.includes(node.extension)) return;

const relativeToDest = dir.replace(sourceNormalized, '');

// if regex enabled
Expand Down