2020-05-29 09:51:16 +03:00
|
|
|
const uglify = require("uglify-js");
|
|
|
|
const fs = require('fs')
|
|
|
|
const path = require('path')
|
2020-08-25 10:56:36 +03:00
|
|
|
const Handlebars = require("handlebars");
|
2021-05-18 11:36:12 +03:00
|
|
|
const g = require("generatorics");
|
2020-05-29 09:51:16 +03:00
|
|
|
|
2022-09-29 14:12:35 +03:00
|
|
|
Handlebars.registerHelper('any', function (...args) {
|
|
|
|
return args.slice(0, -1).some(Boolean)
|
|
|
|
})
|
|
|
|
|
2022-11-21 17:17:44 +03:00
|
|
|
Handlebars.registerPartial('customEvents', Handlebars.compile(fs.readFileSync(relPath('src/customEvents.js')).toString()))
|
|
|
|
|
2020-05-29 09:51:16 +03:00
|
|
|
function relPath(segment) {
|
|
|
|
return path.join(__dirname, segment)
|
|
|
|
}
|
|
|
|
|
2020-08-25 10:56:36 +03:00
|
|
|
function compilefile(input, output, templateVars = {}) {
|
2020-06-11 09:30:00 +03:00
|
|
|
const code = fs.readFileSync(input).toString()
|
2020-08-25 10:56:36 +03:00
|
|
|
const template = Handlebars.compile(code)
|
|
|
|
const rendered = template(templateVars)
|
|
|
|
const result = uglify.minify(rendered)
|
2022-09-29 14:12:35 +03:00
|
|
|
if (result.code) {
|
|
|
|
fs.writeFileSync(output, result.code)
|
|
|
|
} else {
|
|
|
|
throw new Error(`Failed to compile ${output.split('/').pop()}.\n${result.error}\n`)
|
|
|
|
}
|
2020-05-29 09:51:16 +03:00
|
|
|
}
|
|
|
|
|
2022-11-21 17:17:44 +03:00
|
|
|
const base_variants = ["hash", "outbound-links", "exclusions", "compat", "local", "manual", "file-downloads", "dimensions", "tagged-events"]
|
2021-05-18 11:36:12 +03:00
|
|
|
const variants = [...g.clone.powerSet(base_variants)].filter(a => a.length > 0).map(a => a.sort());
|
|
|
|
|
2020-06-11 09:30:00 +03:00
|
|
|
compilefile(relPath('src/plausible.js'), relPath('../priv/tracker/js/plausible.js'))
|
|
|
|
compilefile(relPath('src/p.js'), relPath('../priv/tracker/js/p.js'))
|
2021-05-18 11:36:12 +03:00
|
|
|
|
|
|
|
variants.map(variant => {
|
|
|
|
const options = variant.map(variant => variant.replace('-', '_')).reduce((acc, curr) => (acc[curr] = true, acc), {})
|
|
|
|
compilefile(relPath('src/plausible.js'), relPath(`../priv/tracker/js/plausible.${variant.join('.')}.js`), options)
|
|
|
|
})
|