-
-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
e303828
commit 5f743c6
Showing
7 changed files
with
184 additions
and
257 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,44 @@ | ||
'use strict'; | ||
const through = require('through2'); | ||
const nunjucks = require('nunjucks'); | ||
const PluginError = require('plugin-error'); | ||
const is = require('@sindresorhus/is'); | ||
import {Buffer} from 'node:buffer'; | ||
import {promisify} from 'node:util'; | ||
import nunjucks from 'nunjucks'; | ||
import {gulpPlugin} from 'gulp-plugin-extras'; | ||
|
||
function compile(data, options = {}) { | ||
return through.obj(function (file, encoding, callback) { | ||
if (file.isNull()) { | ||
callback(null, file); | ||
return; | ||
} | ||
|
||
if (file.isStream()) { | ||
callback(new PluginError('gulp-nunjucks', 'Streaming not supported')); | ||
return; | ||
} | ||
export function nunjucksCompile(data, options = {}) { | ||
return gulpPlugin('gulp-nunjucks', async file => { | ||
const context = { | ||
...data, | ||
...file.data, | ||
}; | ||
|
||
const context = {...data, ...file.data}; | ||
const filePath = file.path; | ||
const env = options.env || new nunjucks.Environment(new nunjucks.FileSystemLoader(file.base), options); | ||
|
||
let isAsync = false; | ||
const env = options.env ?? new nunjucks.Environment(new nunjucks.FileSystemLoader(file.base), options); | ||
|
||
if (options.filters && !options.env) { | ||
for (const key of Object.keys(options.filters)) { | ||
const filter = options.filters[key]; | ||
if (is.asyncFunction(filter)) { | ||
isAsync = true; | ||
env.addFilter(key, async (...args) => { | ||
const cb = args.pop(); | ||
try { | ||
const result = await filter(...args); | ||
cb(null, result); | ||
} catch (error) { | ||
cb(error, null); | ||
} | ||
}, true); | ||
} else { | ||
env.addFilter(key, filter); | ||
} | ||
for (const [key, filter] of Object.entries(options.filters)) { | ||
env.addFilter(key, async (...arguments_) => { | ||
const cb = arguments_.pop(); | ||
try { | ||
const result = await filter(...arguments_); | ||
cb(undefined, result); | ||
} catch (error) { | ||
cb(error); | ||
} | ||
}, true); | ||
} | ||
} | ||
|
||
try { | ||
const writeResult = result => { | ||
file.contents = Buffer.from(result); | ||
file.extname = '.html'; | ||
this.push(file); | ||
}; | ||
file.contents = Buffer.from(await promisify(env.renderString.bind(env))(file.contents.toString(), context)); | ||
file.extname = '.html'; | ||
|
||
if (isAsync) { | ||
env.renderString(file.contents.toString(), context, (error, result) => { | ||
if (error) { | ||
this.emit('error', new PluginError('gulp-nunjucks', error, {fileName: filePath})); | ||
callback(); | ||
return; | ||
} | ||
|
||
writeResult(result); | ||
callback(); | ||
}); | ||
} else { | ||
writeResult(env.renderString(file.contents.toString(), context)); | ||
callback(); | ||
} | ||
} catch (error) { | ||
this.emit('error', new PluginError('gulp-nunjucks', error, {fileName: filePath})); | ||
callback(); | ||
} | ||
return file; | ||
}); | ||
} | ||
|
||
function precompile(options) { | ||
return through.obj(function (file, encoding, callback) { | ||
if (file.isNull()) { | ||
callback(null, file); | ||
return; | ||
} | ||
|
||
if (file.isStream()) { | ||
callback(new PluginError('gulp-nunjucks', 'Streaming not supported')); | ||
return; | ||
} | ||
|
||
export function nunjucksPrecompile(options) { | ||
return gulpPlugin('gulp-nunjucks', file => { | ||
const localOptions = {...options}; | ||
const filePath = file.path; | ||
|
||
try { | ||
localOptions.name = (typeof localOptions.name === 'function' && localOptions.name(file)) || file.relative; | ||
file.contents = Buffer.from(nunjucks.precompileString(file.contents.toString(), localOptions)); | ||
file.extname = '.js'; | ||
this.push(file); | ||
} catch (error) { | ||
this.emit('error', new PluginError('gulp-nunjucks', error, {fileName: filePath})); | ||
} | ||
|
||
callback(); | ||
localOptions.name = (typeof localOptions.name === 'function' && localOptions.name(file)) || file.relative; | ||
file.contents = Buffer.from(nunjucks.precompileString(file.contents.toString(), localOptions)); | ||
file.extname = '.js'; | ||
return file; | ||
}); | ||
} | ||
|
||
module.exports = precompile; | ||
module.exports.compile = compile; | ||
module.exports.precompile = precompile; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.