forked from ietf-tools/datatracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprecompile-lodash-templates.js
More file actions
28 lines (24 loc) · 961 Bytes
/
precompile-lodash-templates.js
File metadata and controls
28 lines (24 loc) · 961 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import { createFilter } from '@rollup/pluginutils'
import template from 'lodash/template'
import transform from 'lodash/transform'
import fs from 'fs/promises'
export default function precompileLodashTemplates(options = {}) {
const filter = createFilter(options.include, options.exclude)
return {
name: 'precompile-lodash-templates',
enforce: 'pre',
async transform(code, id) {
if (!filter(id)) { return }
const jsonPath = `${id}on`
const urls = JSON.parse(await fs.readFile(jsonPath, { encoding: 'utf8' }))
const interpolate = /{([\s\S]+?)}/g
const compiledUrls = transform(urls, (result, value, key) => {
result.push(`"${key}": ${template(value.replaceAll('{', '{data.'), { interpolate, variable: 'data' }).source.replace('function(obj)', '(obj) =>')}`)
}, [])
return {
code: code.replace('/* __COMPILED_URLS__ */', compiledUrls.join(',\n')),
map: null
}
}
}
}