Skip to content

Commit

Permalink
Default folder and filetypes (#25)
Browse files Browse the repository at this point in the history
* create branch

* Allow configuring of default folder and filetypes (#22)

Co-authored-by: aarc <4nv1lm355>

* update readme and remove unused copy function

* update pathPrefix and additionPaths check

Co-authored-by: Aaron Clark <[email protected]>
  • Loading branch information
raurir and aarc committed Mar 9, 2020
1 parent 5495d49 commit e0e1a9c
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 24 deletions.
32 changes: 26 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ Use `assetPrefix` instead of `pathPrefix`
- A sitemap is no longer required
- A webmanifest is no longer required

The above two files were hard coded into this plugin in earlier versions. If you still want to move these files to the assets folder, use the new `additionalPaths` option, see below for more information on the option. To get the same behavior as v1, use the following options:
The above two files were hard coded into this plugin in earlier versions. If you still want to move these files to the assets folder, use the new `paths` option, see below for more information on the option. To get the same behavior as v1, use the following options:

```javascript
options: {
additionalPaths: ["manifest.webmanifest", "sitemap.xml"],
paths: ["manifest.webmanifest", "sitemap.xml"],
},
```

Expand Down Expand Up @@ -131,11 +131,11 @@ Stops Webpack from generating the .js.map files
}
```

### additionalPaths
### paths

Default: `[]`
Default: `["static", "icons", "page-data"]`

Additional paths to files/folders that should be moved to the asset directory.
The paths of files/folders to be moved to the asset directory.

```javascript
// Your gatsby-config.js
Expand All @@ -144,7 +144,27 @@ Additional paths to files/folders that should be moved to the asset directory.
{
resolve: "gatsby-plugin-asset-path",
options: {
additionalPaths: ['example.txt', 'foo/example.txt', 'bar/'],
paths: ["static"],
},
},
];
}
```

### fileTypes

Default: `["js", "css"]`

The types of files in the root `publicFolder` to be moved to the asset directory.

```javascript
// Your gatsby-config.js
{
plugins: [
{
resolve: "gatsby-plugin-asset-path",
options: {
fileTypes: ["js", "map", "css"],
},
},
];
Expand Down
43 changes: 25 additions & 18 deletions src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ export const onCreateWebpackConfig = (
* Moves all js and css files into timestamp-named folder
* @see {@link https://next.gatsbyjs.org/docs/node-apis/#onPostBuild}
*/
export const onPostBuild = async ({ pathPrefix }, { additionalPaths = [] }) => {
export const onPostBuild = async (
{ pathPrefix }, // this is specified by the 'assetPrefix' parameter in gatsby-config (note 'asset' instead of 'path'!)
{
additionalPaths = [], // deprecated argument to prevent breaking change
paths = ["static", "icons", "page-data"],
fileTypes = ["js", "css"],
},
) => {
const publicFolder = "./public";
const assetFolder = path.join(publicFolder, `.${pathPrefix}`);

const copy = (fileOrFolder) => {
const currentPath = path.join(publicFolder, fileOrFolder);
const newPath = path.join(assetFolder, fileOrFolder);
try {
if (fs.existsSync(currentPath)) {
return fs.copy(currentPath, newPath);
}
} catch (err) {
console.error(err);
return Promise.resolve();
}
};
if (additionalPaths.length) {
console.warn(
`gatsby-plugin-asset-path argument 'additionalPaths' is deprecated, use 'paths'`,
);
}

if (pathPrefix === "") {
console.error(`gatsby-plugin-asset-path requires both:
- 'assetPrefix' set in gatsby-config
- 'gatsby build' to be run with the '--prefix-paths' flag`);
}

const move = (fileOrFolder) => {
const currentPath = path.join(publicFolder, fileOrFolder);
Expand All @@ -53,14 +59,15 @@ export const onPostBuild = async ({ pathPrefix }, { additionalPaths = [] }) => {
}
};

const filesExtensions = fileTypes.join("|");
const filesRegex = RegExp(`.*.(${filesExtensions})$`);
const filterFilesIn = (folder) =>
fs.readdirSync(folder).filter((file) => /.*\.(js|css)$/.test(file));
fs.readdirSync(folder).filter((file) => filesRegex.test(file));

const filesInPublicFolder = filterFilesIn(publicFolder);
const directories = ["static", "icons", "page-data"];
const thingsToMove = directories
.concat(filesInPublicFolder)
.concat(additionalPaths);
const thingsToMove = paths
.concat(additionalPaths)
.concat(filesInPublicFolder);

// Move files and directories
await Promise.all(thingsToMove.map(move));
Expand Down

0 comments on commit e0e1a9c

Please sign in to comment.