UNPKG

@anikin/parcel-transformer-handlebars-precompile

Version:

Precompile handlebars templates into javascript allowing them to be imported as a regular javascript source

21 lines (18 loc) 664 B
'use strict' const Handlebars = require('handlebars'); const { Transformer } = require('@parcel/plugin'); const transformer = new Transformer({ async transform({asset}) { const source = await asset.getCode(); const precompiled = Handlebars.precompile(source); const js = ` import Handlebars from 'handlebars/dist/handlebars.runtime'; const templateFunction = Handlebars.template(${precompiled}); export default templateFunction; `; asset.type = 'js'; asset.setCode(js); return [asset]; } }); module.exports = transformer;