Coder Social home page Coder Social logo

madecomfy / gatsby-plugin-asset-path Goto Github PK

View Code? Open in Web Editor NEW
15.0 3.0 24.0 321 KB

Move all of your JS and CSS build files, as well as the static folder into a subdirectory of your choice

Home Page: https://madecomfy.com.au

License: MIT License

JavaScript 100.00%
react javascript gatsby gatsby-plugin

gatsby-plugin-asset-path's Introduction

gatsby-plugin-asset-path

Copy all of your JS and CSS build files, as well as the static folder into a subdirectory of your choice.

Breaking change in v3

No longer moves files due to gatsby's internal cache management. Copying files instead!

Breaking change in v2

  • 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 paths option, see below for more information on the option. To get the same behavior as v1, use the following options:

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

Also note that sitemap.xml and the page-data folder were copied to assets folder before, now they are moved just as all other files this plugin handles.

Breaking change in v1

Use assetPrefix instead of pathPrefix

Our use case

Gatsby by default will generate all of the assets and put them directly at the root level:

public
│   index.html
│   component1.js
|   component1.js.map
|   component1.css
|   component2.js
|   compoennt2.js.map
|   component3.css
└───path1
│   │   index.html
│   │   other1.html
│───path2
│   │   index.html
│   │   other2.html
|___static
|   |   data.json
|   |   image.jpg

However here at MadeComfy, we host our site on AWS Cloudfront/S3. One issue that we faced was that somehow, two different builds would have some JS/CSS files with the same file names even though their content are different.

That means during deployment on S3 and object invalidation on Cloudfront, someone that is currently browsing the site, would see the experience broken whilst moving onto other pages as the loaded JS would still have asset references of the previous build.

Hence our need to make sure that each build is kept intact on Cloudfront, except the HTML that are loaded on the browser at each hard reload. That way we make sure that our site has no down time at any point of time. We've configured our caching configs this way.

Using this plugin, our file struture is now as followed:

public
│   index.html
|___assets
|   |___1534761288
│   |   |   component1.js
│   |   |   component1.js.map
│   |   |   component1.css
│   |   |   component2.js
│   |   |   compoennt2.js.map
│   |   |   component3.css
│   |   |___static
│   |   |   |   data.json
│   |   |   |   image.jpg
└───path1
│   │   index.html
│   │   other1.html
│───path2
│   │   index.html
│   │   other2.html

Our new assets folder would contain assets of every build once on S3.

Install

npm install --save-dev gatsby-plugin-asset-path
yarn install -D gatsby-plugin-asset-path

How to use

// Your gatsby-config.js
{
    assetPrefix: "custom_asset_folder",
    plugins: [
        {
            resolve: "gatsby-plugin-asset-path"
        }
    ]
}

In our use case above, we have assetPrefix set as followed:

{
  assetPrefix: `/assets/${Date.now().toString()}`,
}

Options

removeMapFiles

Default: false

Stops Webpack from generating the .js.map files

// Your gatsby-config.js
{
  plugins: [
    {
      resolve: "gatsby-plugin-asset-path",
      options: {
        removeMapFiles: true,
      },
    },
  ];
}

paths

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

The paths of files/folders to be copied to the asset directory. Do not add icons since these are copied and duplicated across /public/icons/ and /public/${assetPrefix}/.

// Your gatsby-config.js
{
  plugins: [
    {
      resolve: "gatsby-plugin-asset-path",
      options: {
        paths: ["static"],
      },
    },
  ];
}

fileTypes

Default: ["js", "css"]

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

// Your gatsby-config.js
{
  plugins: [
    {
      resolve: "gatsby-plugin-asset-path",
      options: {
        fileTypes: ["js", "map", "css"],
      },
    },
  ];
}

DEPLOY

Update version in package.json then release via github releases with same tag #!

gatsby-plugin-asset-path's People

Contributors

chungryan avatar dependabot[bot] avatar dulcetti avatar ehannes avatar raurir avatar stephenreid avatar tjmcewan avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

gatsby-plugin-asset-path's Issues

cannot find module 'babel-runtime/regenerator'

I'm getting this error while building in docker with image mhart/alpine-node:12.18.0, building in local works fine with node v12.18.3

Error in "/usr/src/app/node_modules/gatsby-plugin-asset-path/gatsby-node.js":
Cannot find module 'babel-runtime/regenerator'
Require stack:
- /usr/src/app/node_modules/gatsby-plugin-asset-path/gatsby-node.js
- /usr/src/app/node_modules/gatsby/dist/bootstrap/resolve-module-exports.js
- /usr/src/app/node_modules/gatsby/dist/bootstrap/load-plugins/validate.js
- /usr/src/app/node_modules/gatsby/dist/bootstrap/load-plugins/load.js
- /usr/src/app/node_modules/gatsby/dist/bootstrap/load-plugins/index.js
- /usr/src/app/node_modules/gatsby/dist/services/initialize.js
- /usr/src/app/node_modules/gatsby/dist/services/index.js
- /usr/src/app/node_modules/gatsby/dist/bootstrap/index.js
- /usr/src/app/node_modules/gatsby/dist/commands/build.js
- /usr/src/app/node_modules/gatsby-cli/lib/create-cli.js
- /usr/src/app/node_modules/gatsby-cli/lib/index.js
- /usr/src/app/node_modules/gatsby/dist/bin/gatsby.js
- /usr/src/app/node_modules/gatsby/cli.js

Looking in local node_modules, the file gatsby-node.js is importing the package babel-runtime/regenerator which throws the error since the package doesnt exist.

Tried installing babel-runtime/regenerator but got permissions error maybe due to it being private package or something.

Here's my package.json file.

{
   "name": "shop",
   "version": "0.1.0",
   "scripts": {
      "build": "gatsby build --prefix-paths",
      "develop": "gatsby develop",
      "serve": "gatsby serve --prefix-paths",
      "lint:graphql": "eslint src/graphql/**/*.js"
   },
   "dependencies": {
      "@apollo/react-hooks": "^3.1.5",
      "@emotion/core": "^10.0.28",
      "@emotion/react": "^11.1.4",
      "@emotion/styled": "^10.0.27",
      "@react-keycloak/web": "^2.1.1",
      "@stripe/react-stripe-js": "^1.1.2",
      "@stripe/stripe-js": "^1.11.0",
      "apollo-cache-inmemory": "^1.6.6",
      "apollo-client": "^2.6.10",
      "apollo-link": "^1.2.14",
      "apollo-link-context": "^1.0.20",
      "apollo-link-http": "^1.5.17",
      "apollo-link-ws": "^1.0.20",
      "apollo-utilities": "^1.3.4",
      "axios": "^0.21.1",
      "core-js": "2.6.5",
      "gatsby": "^2.30.1",
      "gatsby-plugin-emotion": "^4.3.3",
      "gatsby-plugin-portal": "^1.0.7",
      "gatsby-plugin-postcss": "^2.3.3",
      "gatsby-plugin-react-helmet": "^3.8.0",
      "graphql": "^15.4.0",
      "graphql-tag": "^2.11.0",
      "isomorphic-fetch": "^2.2.1",
      "jwt-decode": "^3.1.2",
      "keycloak-js": "^10.0.2",
      "lodash": "^4.17.20",
      "moment": "^2.29.1",
      "postcss-import": "^12.0.1",
      "prop-types": "^15.7.2",
      "react": "^16.13.1",
      "react-dom": "^16.13.1",
      "react-google-places-autocomplete": "^2.1.0",
      "react-helmet": "^6.1.0",
      "react-html-parser": "^2.0.2",
      "react-toast-notifications": "^2.4.0",
      "rrule": "^2.6.6",
      "subscriptions-transport-ws": "^0.9.18",
      "ws": "^7.4.2"
   },
   "devDependencies": {
      "autoprefixer": "^10.2.0",
      "babel-preset-gatsby": "^0.10.0",
      "eslint-plugin-graphql": "^4.0.0",
      "gatsby-plugin-asset-path": "^3.0.2",
      "postcss": "^8.2.3",
      "postcss-preset-env": "^6.7.0",
      "tailwindcss": "^2.0.2",
      "twin.macro": "^2.0.8"
   },
   "repository": {
      "type": "git",
      "url": "https://github.com/dailykit/subscription-shop"
   },
   "bugs": {
      "url": "https://github.com/dailykit/subscription-shop/issues"
   }
}

Error: Source and destination must not be the same.

Hi, I'm trying to use this plugin but no matter what I configure in pathPrefix I get this error:

Error: Source and destination must not be the same.

I even tried copying the exact same config on the readme here but it didn't work.

Full error log:

error Plugin gatsby-plugin-asset-path returned an error


  Error: Source and destination must not be the same.
  
  - copy.js:237 checkStats
    [aurum]/[gatsby-plugin-asset-path]/[fs-extra]/lib/copy/copy.js:237:17
  
  - copy.js:227 fs.stat
    [aurum]/[gatsby-plugin-asset-path]/[fs-extra]/lib/copy/copy.js:227:14
  
  - polyfills.js:285 
    [aurum]/[graceful-fs]/polyfills.js:285:20

Versions:
gatsby": "^2.0.106
gatsby-plugin-asset-path": "^0.2.1

Not working with Gatsby v1

This plugin is exactly what I need but I cannot get it to work. I have followed the README and this is my gatsby-config.js

module.exports = {
  siteMetadata: {
    title: 'My page',
  },
  prefixPath: 'custom_asset_folder',
  plugins: [
    'gatsby-plugin-react-helmet',
    'gatsby-plugin-material-ui',
    'gatsby-plugin-emotion',
    {
      resolve: 'gatsby-plugin-asset-path',
    },
  ],
}

When I run gatsby build I get a config validation error.

screen shot 2018-09-05 at 15 32 26

I guessed that this may have been down to a typo as the Gatsby docs has pathPrefix rather than prefixPath https://next.gatsbyjs.org/docs/gatsby-config/#pathPrefix

I tried changing that key but still got an error.

  Error: ENOENT: no such file or directory, stat 'public/sitemap.xml'

Browser location changes after page load

I've managed to integrate this but the browser location changes after the page finishes loading.
The page is visible, but if I hit reload it attempts to reload the nested page and 404s.

e.g. with pathPrefix set to custom_asset_path, my page /page will load fine with all styles, but once the JS kicks in the browser's displayed url will be /custom_asset_path/page. Hence, reloading fails.

It seems like the plugin is meant to disable the standard behaviour of pathPrefix but it's not? Any ideas what I could be doing wrong?

Add option to copy files instead of move them

Hi!

I know there's been a lot back and forth about the move/copy strategy in this package. But we have some sites that has a lot of images. These sites more or less doubles in size when copying the assets instead of moving them, which gives us different problems.

Would you accept a pull request adding the option to copy instead of moving, and that we default to copying?

Plugin disables Gatsby cache

After using the plugin, Gatsby cache stops working and every time page is reloaded, js and other files are refetched.

Expected: should load js and other files from disk/memory cache.

Plugin version: v3.0.4

Config:

  assetPrefix: `/assets/${Date.now().toString()}`,
  plugins: [
    {
      resolve: 'gatsby-plugin-asset-path',
      options: {
        paths: ['static', 'page-data'],
      },
    },
    ...
  ]

Error when trying to use this plugin

Error: EnsureResources was not able to find resources for path: "/"
This typically means that an issue occurred building components for that path.
Run `gatsby clean` to remove any cached elements

I've run gatsby clean a few times and it doesn't make a difference. Using standard options and have my assetPrefix set to "gatsby".

Missing folders

Hello there, it seems that the plugin misses some assets files and folders. I'm thinking about:

  • assets
  • fonts
  • icons
  • *.json
  • *.map

Would you consider a PR to fix it?

Sub directories in static folder are not moved

Sub directories in the static folder are not moved to the asset directory.

This content in the static folder:

// static
- example.txt
- bundles
  | - bundle1.js
  | - bundle2.js

Gives this build:

// public
- assets
  | - example.txt
- bundles
  | - bundle1.js
  | - bundle2.js

The problem here is that the bundles directory is not moved to the assets directory.
I think it is reasonable to assume that you want to do a recursive move, not just a shallow one.

I'll start working with a PR for this.

Throws an error when there's no sitemap.xml file

I'm getting this error whenever I try to run gatsby build --prefix-paths with the gatsby-plugin-asset-path config:

 ERROR #11321  PLUGIN

"gatsby-plugin-asset-path" threw an error while running the onPostBuild lifecycle:

ENOENT: no such file or directory, stat 'public/sitemap.xml'

It works smoothly if I place a dummy sitemap.xml file in the public folder

My gatsby info is this:

  System:
    OS: macOS 10.15.3
    CPU: (8) x64 Intel(R) Core(TM) i7-7700HQ CPU @ 2.80GHz
    Shell: 5.7.1 - /bin/zsh
  Binaries:
    Node: 13.5.0 - /usr/local/bin/node
    Yarn: 1.21.1 - /usr/local/bin/yarn
    npm: 6.13.4 - /usr/local/bin/npm
  Languages:
    Python: 2.7.16 - /usr/bin/python
  Browsers:
    Chrome: 79.0.3945.130
    Firefox: 72.0.2
    Safari: 13.0.5
  npmPackages:
    gatsby: ^2.19.7 => 2.19.21
    gatsby-image: ^2.2.39 => 2.2.41
    gatsby-plugin-asset-path: ^1.1.0 => 1.1.0
    gatsby-plugin-manifest: ^2.2.39 => 2.2.42
    gatsby-plugin-offline: ^3.0.32 => 3.0.35
    gatsby-plugin-react-helmet: ^3.1.21 => 3.1.22
    gatsby-plugin-sass: ^2.1.28 => 2.1.29
    gatsby-plugin-sharp: ^2.4.3 => 2.4.5
    gatsby-source-filesystem: ^2.1.46 => 2.1.48
    gatsby-source-graphql: ^2.1.33 => 2.1.33
    gatsby-transformer-sharp: ^2.3.13 => 2.3.14
  npmGlobalPackages:
    gatsby-cli: 2.9.0

Doesn't work when adding domain prefix

I might have missed a step somewhere, but it seems that this works:

assetPrefix: `/assets`,

But this does not

assetPrefix: `https://foobar.netlify.com/assets`,

It seems trivial to detect // and work out the desired folder structure that way

Edit: I've submitted a pull request to fix the issue

Incremental builds: Delete doesn't work anymore

When using this plugin, you cannot delete pages with incremental builds anymore. I guess it's because the copy doesn't remove files that are missing in the source folder, but only copies new pages or overwrites existing ones.

Some notes about this plugin
Due to some migration of routing rules, we had a need for this plugin at the beginning of our project. Now that the migration phase is over we removed it. After we removed the plugin, we figured out that the plugin had a very negative impact on incremental build and deployment times. Those are now reduced by over 50%! This is not an issue with smaller projects, but if you have a lot of pages (>10k pages) I would not recommend to use it.

manifest.webmanifest

I cannot get this plugin to work together with gatsby-plugin-manifest. The <link> element created by that plugin assumes that manifest.webmanifest is located at the project root, but this plugin moves the manifest to whatever I set the assetPrefix to. I find no option to give gatsby-plugin-manifest a path to where the file is situated, nor an option to tell this plugin to exclude the manifest. How have you other folks solved this? I guess there has to be a solution out there since I see that you explicitly target the manifest file in gatsby-node.js.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.