Quick Start - lewie9021/node-compiler GitHub Wiki

To get started, install the compiler via NPM. If NodeJS is installed, you will already have NPM. Open your terminal, switch to the directory you wish to save the compiler, and enter the following command:

npm install node-compiler

Once the compiler has installed, you will have a node_modules folder within the selected directory. Next, create a simple script that executes the compiler.

// Include the required modules.
var Compiler = require("node-compiler");
var Path = require("path");

// Instantiate the compiler with a configuration object and mode.
var compiler = new Compiler(Path.join(__dirname, "config.json"), "dev");

// Attach an event emitter to notify us when a compile is complete.
compiler.on("compiled", function() {
    console.log("Compiled");
});

// Begin the initial compile.
compiler.compile();

You may have noticed that the within the script we reference a configuration file found within the same directory as the script, so let's go ahead and create it.

{
    "name": "My Project",
    "directory": "C:\\Development\\MyProject",
    "modes": [
        {
            "id": "dev",
            "name": "Development",
            "profiles": ["js"]
        }
    ],
    "profiles": [
        {
            "id": "js",
            "name": "Application Logic",
            "output": "public\\app.js",
            "targets": [
                {
                    "directory": "src\\js",
                    "watch": true,
                    "plugin": {
                        "name": "JS",
                        "options": {
                            "minify": true,
                            "paths": true
                        }
                    }
                }
            ]
        }
    ]
}

The configuration object above declares a project called 'My Project' located at 'C:\Development\MyProject'. It has only one compile mode named 'dev' that references the 'js' profile. Within this profile, I want the compiler to watch the 'src\js' directory and render the contents to the output directory 'public\app.js'. The additional options set for the target mean the output will be minified and rendered with the original path in a comment above each file. Since this output directory is a file, the contents will be automatically concatenated.