From e0e1a9caafeb7ea4111a9ad23ac694c410e312d8 Mon Sep 17 00:00:00 2001 From: Rauri Rochford Date: Tue, 10 Mar 2020 09:58:46 +1100 Subject: [PATCH] Default folder and filetypes (#25) * 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 --- README.md | 32 ++++++++++++++++++++++++++------ src/gatsby-node.js | 43 +++++++++++++++++++++++++------------------ 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/README.md b/README.md index 97a3d50..f79ec9b 100644 --- a/README.md +++ b/README.md @@ -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"], }, ``` @@ -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 @@ -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"], }, }, ]; diff --git a/src/gatsby-node.js b/src/gatsby-node.js index eaf5ce7..b1cf8c2 100644 --- a/src/gatsby-node.js +++ b/src/gatsby-node.js @@ -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); @@ -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));