Add CSS and JavaScript Files to Phabricator Extensions

By  on  

Every open source framework has its own methods of extending functionality; some make extending incredibly easy and others...not so much.  Most of the time it comes down to how well the framework is documented.  Phabricator did an awesome job of making necessary classes extendable but it's fair to say it would be great if the framework had a bit better documentation (PRs welcome, I bet!).

In creating my own extension, I needed to add JavaScript and CSS files to the page.  Unfortunately the Adding New CSS and JS documentation states "This document is intended for Phabricator developers and contributors. This process will not work correctly for third-party code, plugins, or extensions."  I found that to not be true.  The following is how I added JavaScript and CSS assets for my extension.

Place Asset Files in Your Extension Directory

Each extension I create is placed in its own directory with all dependencies within that directory:

  • extensions
    • my-extension
      • my-extension.php
      • my-extension.css
      • my-extension.js

You could further place CSS and JS files into their own directories if you'd like.  Each CSS and JS file should have a @provides comment by which you'll refer to it when it's time to require it:

/**
 * @provides my-extension-js
 */

I choose to add -js and -css to the reference name for ease in maintenance.

Use require_celerity_resource to Require the File

In the function which represents the view you'd like the assets injected into, you'll use require_celerity_resource with the @provides asset name:

protected function renderLoginForm(AphrontRequest $request, $mode) {
 // Add CSS and JS resources to the page
  require_celerity_resource('bmo-auth-css');
  require_celerity_resource('bmo-auth-js');

  // ....
}

During Build, Move Asset Locations

Phabricator expects all JavaScript and CSS files to be in its /webroot/rsrc/js/ and /webroot/rsrc/css/ directories so you'll need to move your extension assets during the build process.  I added the following to my Dockerfile to do just that:

# Move static resources to phabricator, add files to celerity map array
COPY auth/my-extension.css /app/phabricator/webroot/rsrc/css/my-extension.css
COPY auth/my-extension.js /app/phabricator/webroot/rsrc/js/my-extension.js

With your JavaScript and CSS files in this location, the require_celerity_resource function can find your assets by name.

Updated the Celerity Map

To bust cache and index your files, you'll need to run the following command:

phabricator/ $ ./bin/celerity map

That's the simple but not excellently documented process for adding your own CSS and JavaScript assets to Phabricator!

Recent Features

  • By
    5 More HTML5 APIs You Didn’t Know Existed

    The HTML5 revolution has provided us some awesome JavaScript and HTML APIs.  Some are APIs we knew we've needed for years, others are cutting edge mobile and desktop helpers.  Regardless of API strength or purpose, anything to help us better do our job is a...

  • By
    Creating Scrolling Parallax Effects with CSS

    Introduction For quite a long time now websites with the so called "parallax" effect have been really popular. In case you have not heard of this effect, it basically includes different layers of images that are moving in different directions or with different speed. This leads to a...

Incredible Demos

  • By
    Create a Twitter AJAX Button with MooTools, jQuery, or Dojo

    There's nothing like a subtle, slick website widget that effectively uses CSS and JavaScript to enhance the user experience.  Of course widgets like that take many hours to perfect, but it doesn't take long for that effort to be rewarded with above-average user retention and...

  • By
    Create a Download Package Using MooTools Moousture

    Zohaib Sibt-e-Hassan recently released a great mouse gestures library for MooTools called Moousture. Moousture allows you to trigger functionality by moving your mouse in specified custom patterns. Too illustrate Moousture's value, I've created an image download builder using Mooustures and PHP. The XHTML We provide...

Discussion

    Wrap your code in <pre class="{language}"></pre> tags, link to a GitHub gist, JSFiddle fiddle, or CodePen pen to embed!