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:
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...
cd /path/to/docroot/app/design/frontend/$VENDOR/$THEME
- change to the active theme directory (replace $VENDOR
and $THEME
with your own values)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 needsxargs -I {} bash -c
- for each changed file, execute the commands in #4-6 (the relative path to the changed file is stored in {}
)dest=/path/to/docroot/pub/static/frontend/$VENDOR/$THEME/*/$(echo {} | sed -E "s/(web\/|\/[^/]+$)//g")
- set the deployment destination path
*
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
)echo Deploying {} to $dest ...
- log the activity to the console so you know which files are being deployedmkdir -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
)cd - 1> /dev/null
- change to the previous directory so you can continue where you left offYou 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.