Laravel Elixir Remove CSS Comments

Have you ever wanted to strip those block comments out of your production CSS? In the past, I’ve run into issues where the CSS is not processed correctly because everything gets bumped onto the same line as Bootstrap’s block comment and it somehow things it is also commented out. No worries though if you are using Laravel Elixir (or really just the cssnano plugin):

Here is an example of how you can configure cssnano to strip all comments:

If you are just using cssnano in a non-elixir build, you can still pass the discardComments: { removeAll: true }  option to achieve the same effect.

Javascript Wait Until jQuery Loads

Ok, if you are like me, you’ve probably had the need to put a small snippet of Javascript between some <script> tags in a view. But, if the rest of your javascript is loaded somewhere in the footer of your layout, then at the point you run that embedded snippet, none of your other libraries will have been loaded.

Allow me to introduce you to a little technique that can be an interesting workaround. I can’t claim to have found this trick as I ran across it on an awesome StackOverflow answer.

Allow me to explain what is going on here.  You will notice the function is wrapped with parenthesis followed by another set of parenthesis. This is called an immediately invoked function expression (IIFE), which is a fancy way of saying that it will run automatically when the code is loaded. When it runs, it checks to see if the window.jQuery object exists and if not, it sets a timeout for 50ms and tries again. This will continuously loop until it detects that the jQuery object exists in the global namespace and will then run your code.

This doesn’t only apply to jQuery. For my use case, I had a javascript object that I needed to set a counter based on values loaded in from a form with dynamic sections. This is  how I made that work:

You will notice the {{ … }} syntax in there. That is the blade templating language in Laravel. For my use case, I needed to generate 0 to N instances of a template and keep track in javascript how many I had so that the next instance generated client side had the appropriate numbers applied to input field names. Probably not the best way to do things, but definitely functional.

 

GoLang, Qt, and Robert Frost

One of my most favorite poems is The Road Not Taken by Robert Frost. Something I was working on tonight sparked my memory of a line from that poem.

Yet knowing how way leads on to way,
I doubted if I should ever come back.

It all started with my decision to start dabbling with Go, a newish language created by the folks at Google. I’m exploring it because it is lightening fast and can be a great solution for some of the intensive data manipulation stuff I am working on, where PHP really struggles to perform. As someone who spends a lot of time in scripting languages, I had to remember the old days when I used to dabble with C/C++ and get back to understanding compilation. Lucky for us, Go handles memory management for the most part, which really makes it much more enjoyable to code with!

Although I love my trusty Sublime Text 3, I felt compelled to find an IDE that would help out more with syntax completion and make it more difficult for me to make stupid mistakes. Alas, I came across an open source project called LiteIDE. Sweet! So I installed it on my Macbook without a hitch and started using it. As I started perusing the options and settings, I couldn’t help but notice there were some misspellings and incorrect grammar. This isn’t a big deal — I am acutely where that I do not live in an English speaking bubble and some of the best developers come from all around the world. So, I thought, hey why not give back and send them a PR with a few updates? I cloned the project and made some changes. Although they are just translation changes, I figured I should try to build the project and make sure I didn’t break anything.

Looking through the code, I find a build folder with a convenient bash script setup to build on OSX. Give it a go and quickly find out I need Qt. Ok, although I’ve heard of it, I’ve never used it. So, off I go to download it and install it. After some path configuration so it can find qmake, I try the build again — no dice. Lots of errors like unknown type nume QIODevice and things about not matching a ColorStyleScheme declaration. Oy! I’m way out of my element here.

I will probably still submit the PR since it is only translation changes, but the point of the post is something I often find myself doing when experimenting or learning new things. There is so much to learn and when I venture down an unfamiliar road, I often drift from subject to subject learning just enough to contribute to the original goal. I’ve found this experience to a rewarding way to learn and has attributed to my achievements as a developer. I guess, like Robert Frost, you could say I took the road less traveled, and that has made all the difference.

Laravel 5 Elixir Gulp LiveReload

If you are anything like me, you are having trouble getting LiveReload to work in the Elixir overlay for Gulp. Good news is, I’ve finally got it figured out and am here to share. It turns out to be really simple, you just need to create an extension for Elixir. Add this to your gulpfile.js:

Then in your HTML, include this line:

One thing you will notice is in the livereload.changed()  method, I’m passing the word localhost. If you do not pass something there (for whatever reason), you will get an error like this:

It seems to work fine with an asterisk passed there as well. Maybe somebody can comment further what should go there.

PDO Query Nuance: UPDATE WHERE IN Only Updates One Row

I prefer to always use parameterized queries in PDO when possible. Just now, I encountered a situation in which the complications of using them would have not even been worth it. Take a look at this query:

Given that the post_id‘s exist, this will set a deleted flag on all of them right? Ok, let’s try to define that query using PDO and prepared statements…

As it turns out, that will ONLY update the first row. Turns out, the IN clause is a little more complicated than I thought with prepared statements. There is a proposed solution here, but in my case, the data is not user input and can thus sanitize it myself.