A faster route to deploying static content in Magento 2? Dev to Live etc?

前端 未结 8 667
名媛妹妹
名媛妹妹 2021-01-30 07:02

This is my environment. Please note this is also set in the relevant development modes and production modes.

Dev:
https://ar.dev.loc/
https://en.dev.loc/

Live:
         


        
8条回答
  •  被撕碎了的回忆
    2021-01-30 07:51

    I haven't touched setup:static-content:deploy in awhile. After getting fed up with the slow deployment process in Magento 2, I devised my own that allows me to make a change in a CSS or JS file and have it reflected almost immediately on the site. Here's the magic:

    cd /path/to/docroot/app/design/frontend/$VENDOR/$THEME
    find * -type f \( -name "*.css" -o -name "*.js" \) -cmin -10 | xargs -I {} bash -c 'dest=/path/to/docroot/pub/static/frontend/$VENDOR/$THEME/*/$(echo {} | sed -E "s/(web\/|\/[^/]+$)//g"); echo Deploying {} to $dest ...; mkdir -p $dest && cp {} $_'
    cd - 1> /dev/null
    

    Let's break it down...

    1. cd /path/to/docroot/app/design/frontend/$VENDOR/$THEME - change to the active theme directory (replace $VENDOR and $THEME with your own values)
    2. find * -type f \( -name "*.css" -o -name "*.js" \) -cmin -10 - find all CSS and JS files that were changed in any way within the last 10 minutes inside the theme directory (* gets rid of the leading ./ in the results); feel free to change any of the parameters to fit your needs
    3. xargs -I {} bash -c - for each changed file, execute the commands in #4-6 (the relative path to the changed file is stored in {})
    4. dest=/path/to/docroot/pub/static/frontend/$VENDOR/$THEME/*/$(echo {} | sed -E "s/(web\/|\/[^/]+$)//g") - set the deployment destination path
      • the * glob takes care of matching whatever locale you’re in
      • $(...) spawns a subshell to extract only the part of the source path that we need to append to the destination path (web directory level doesn't exist under pub)
    5. echo Deploying {} to $dest ... - log the activity to the console so you know which files are being deployed
    6. mkdir -p $dest && cp {} $_ - create the destination directory structure; if and only if the directory structure is successfully created, copy the changed file to its deployment destination under pub ($_ points to the last argument of the previous command, mkdir, which is $dest)
    7. cd - 1> /dev/null - change to the previous directory so you can continue where you left off

    You can throw all this into an alias to reduce it to one command, but remember to escape the single quotes:

    alias update='cd /path/to/docroot/app/design/frontend/$VENDOR/$THEME; find * -type f \( -name "*.css" -o -name "*.js" \) -cmin -10 | xargs -I {} bash -c '"'"'dest=/path/to/docroot/pub/static/frontend/$VENDOR/$THEME/*/$(echo {} | sed -E "s/(web\/|\/[^/]+$)//g"); echo Deploying {} to $dest ...; mkdir -p $dest && cp {} $_'"'"'; cd - 1> /dev/null'
    

    If you use varnish, then you should add a command to restart varnish at the end of the alias (e.g., sudo service varnish restart) or you'll likely still be hitting cached static assets.

    If you're too lazy to type update every time you make a change, then you can put it in a cron or integrate it into a grunt monitor task or equivalent.

提交回复
热议问题