Skip to content
This repository has been archived by the owner on Jun 15, 2023. It is now read-only.

[WIP] New version #4

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
root = true

[*]
indent_style = space
indent_size = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true
9 changes: 1 addition & 8 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,9 +1,2 @@
*~
*.bak
.DS_Store
npm-debug.log
node_modules/
lib/
public/
html-report/
lcov.info
npm-debug.log*
9 changes: 0 additions & 9 deletions .npmignore

This file was deleted.

10 changes: 1 addition & 9 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,3 @@
language: node_js
node_js:
- "0.10"
addons:
code_climate:
repo_token: 8338023ce688b10744c6385ce188e6aa677d9dec682354db836091e034a94d44
script:
- npm test
- npm run coverage
after_script:
- cat lcov.info | codeclimate
- lts
48 changes: 48 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
'use strict';
const BrunchLoader = require('./loader');
const {Environment} = require('nunjucks');

class NunjucksBrunch {
get brunchPlugin() {
return true;
}

constructor(config) {
const {
base = 'app',
dir = '$&.html',
pattern = /\.njk$/i,

options = {},
context = {},
rename = String,
} = config.plugins.nunjucks || {};

this.loader = {
base,
dir,
pattern,
};

Object.assign(this, {
pattern,
options,
context,
rename,
type: 'template',
});
}

compileStatic({data, path}) {
const loader = new BrunchLoader(this.loader);
const env = new Environment(loader, this.options);

return {
data: env.renderString(data, this.context, {path}),
path: this.rename(path),
dependencies: loader.deps,
};
}
}

module.exports = NunjucksBrunch;
33 changes: 33 additions & 0 deletions lib/loader.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
const {dirname, resolve} = require('path');
const fs = require('fs');

class BrunchLoader {
constructor(config) {
this.deps = [];

Object.assign(this, config);
}

isRelative(path) {
return !path.startsWith('/');
}

resolve(from, to) {
const base = /^\.{1,2}/.test(to) ? dirname(from) : this.base;
const path = resolve(base, to);

return fs.statSync(path).isDirectory()
? path.replace(/[^/]+$/, `$&/${this.dir}`)
: path;
}

getSource(path) {
this.deps.push(path);
const src = fs.readFileSync(path, 'utf-8');

return {path, src};
}
}

module.exports = BrunchLoader;
Loading