1.0.1 • Published 8 years ago

njs-webpack-plugin v1.0.1

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

Webpack Plugin for NJS


This plugin easyly adds NJS compilation to your build process.


Getting started

Install

npm install njs-webpack-plugin
npm install njs-runtime

Hello, world

Create file entry.js:
global.njs = require('njs-runtime');

var testRuntime = {% compileNjs %}function () {
    console.log('one');
    njs.sleep.yld(1000);
    console.log('two');
    njs.sleep.yld(1000);
    console.log('threes');
}{% endcompileNjs %};

testRuntime(); // new thread
Create webpack.config.js
var NjsPlugin = require('njs-webpack-plugin');

module.exports = {
    entry: './entry.js',
    output: {
        filename: 'build/bundle.js'
    },
    plugins: [new NjsPlugin()]
};
Compile bundle (run command in terminal):
webpack
Run compiled script (run command in terminal):
node build/bundle.js 
Output:
one
two
threes

API

new NjsPlugin(options)
optiontypedescription
localsdictionarylocals will be passed to Swig
template_dirstringPath to templates dir, where swig will search for template files. This can be useful if you use include swig tag: {% include "templates/addon.js" %}
runtimestringYou can override global variable name of NJS Runtime (default: 'njs')
tagNamestringYou can override swig Tag name (default: 'compileNjs')

locals is very useful if you want to use swig as text-preprocessor.

For example, if you set locals like this:

var options = {locals: {DEBUG: true, TITLE: 'MY PAGE'}}

Then you can preproccess your JavaScript files:

{% if DEBUG %}
console.log('DEBUG', '{{ TITLE }}');
{% else %}
console.log('PRODUCTION', '{{ TITLE }}');
{% endif %}

Documentation

Read more about NJS Project