[Coldbox 6] Accessing Static Assets When Building and Testing Modules

When building a ColdBox module, it is common practice to use a /test-harness/ folder in the project root to set up a dummy application to test the module. The problem is that if your module needs static assets like CSS, JS, or image files, it’s difficult to load them in the test-harness because the module’s files exist outside of the web root!

Example:
c:\repositories\myModule\ ← your awesome module
c:\repositories\myModule\test-harness\ <— dummy app for testing the module

Normally you would add static assets to your module’s layouts or views like this:
<link href="#event.getModuleRoot()#/includes/css/app.css" rel="stylesheet">
The above code gets the path of the current module’s root and then links to a css file. However, if you are viewing the module through the test-harness, the css file doesn’t exist there, so you will get a 404 error.

How do you fix this? If you’re using Commandbox, the answer is creating an alias in your test-harness’s server.json file:

{
    "web":{
        "aliases" : {
            "/moduleroot/myModule/" : "../"
        }
    }
}

The above configuration creates a special mapping at the web server which points to the directory above the test-harness.

It should be noted that Brad proposed a cool idea to handle this type of thing auto-magically without the need for setting up aliases, here: [COLDBOX-1180] - Welcome

I saw this come on during my holidays, I am investigating it and putting a plan together for it.

2 Likes