1
0
Fork 0
mirror of synced 2024-10-02 10:08:09 +13:00

Indexjs to esm

This commit is contained in:
Adria Navarro 2024-02-21 17:54:20 +01:00
parent 7a7a30b8f5
commit efef47b77e
2 changed files with 56 additions and 64 deletions

View file

@ -1,21 +1,23 @@
const vm = require("vm")
const handlebars = require("handlebars")
const { registerAll, registerMinimum } = require("./helpers/index")
const processors = require("./processors")
const { atob, btoa, isBackendService } = require("./utilities")
const manifest = require("../manifest.json")
const {
import { createContext, runInNewContext } from "vm"
import { create } from "handlebars"
import { registerAll, registerMinimum } from "./helpers/index"
import { preprocess, postprocess } from "./processors"
import { atob, btoa, isBackendService } from "./utilities"
import manifest from "../manifest.json"
import {
FIND_HBS_REGEX,
FIND_ANY_HBS_REGEX,
findDoubleHbsInstances,
} = require("./utilities")
const { convertHBSBlock } = require("./conversion")
const javascript = require("./helpers/javascript")
const { helpersToRemoveForJs } = require("./helpers/list")
} from "./utilities"
import { convertHBSBlock } from "./conversion"
import { setJSRunner, removeJSRunner } from "./helpers/javascript"
import { helpersToRemoveForJs } from "./helpers/list"
const hbsInstance = handlebars.create()
export { setJSRunner, setOnErrorLog } from "./helpers/javascript"
const hbsInstance = create()
registerAll(hbsInstance)
const hbsInstanceNoHelpers = handlebars.create()
const hbsInstanceNoHelpers = create()
registerMinimum(hbsInstanceNoHelpers)
const defaultOpts = {
noHelpers: false,
@ -52,11 +54,11 @@ function createTemplate(string, opts) {
return templateCache[key]
}
string = processors.preprocess(string, opts)
string = preprocess(string, opts)
// Optionally disable built in HBS escaping
if (opts.noEscaping) {
string = exports.disableEscaping(string)
string = disableEscaping(string)
}
// This does not throw an error when template can't be fulfilled,
@ -77,23 +79,15 @@ function createTemplate(string, opts) {
* @param {object|undefined} [opts] optional - specify some options for processing.
* @returns {Promise<object|array>} The structure input, as fully updated as possible.
*/
module.exports.processObject = async (object, context, opts) => {
export async function processObject(object, context, opts) {
testObject(object)
for (let key of Object.keys(object || {})) {
if (object[key] != null) {
let val = object[key]
if (typeof val === "string") {
object[key] = await module.exports.processString(
object[key],
context,
opts
)
object[key] = await processString(object[key], context, opts)
} else if (typeof val === "object") {
object[key] = await module.exports.processObject(
object[key],
context,
opts
)
object[key] = await processObject(object[key], context, opts)
}
}
}
@ -108,9 +102,9 @@ module.exports.processObject = async (object, context, opts) => {
* @param {object|undefined} [opts] optional - specify some options for processing.
* @returns {Promise<string>} The enriched string, all templates should have been replaced if they can be.
*/
module.exports.processString = async (string, context, opts) => {
export async function processString(string, context, opts) {
// TODO: carry out any async calls before carrying out async call
return module.exports.processStringSync(string, context, opts)
return processStringSync(string, context, opts)
}
/**
@ -122,14 +116,14 @@ module.exports.processString = async (string, context, opts) => {
* @param {object|undefined} [opts] optional - specify some options for processing.
* @returns {object|array} The structure input, as fully updated as possible.
*/
module.exports.processObjectSync = (object, context, opts) => {
export function processObjectSync(object, context, opts) {
testObject(object)
for (let key of Object.keys(object || {})) {
let val = object[key]
if (typeof val === "string") {
object[key] = module.exports.processStringSync(object[key], context, opts)
object[key] = processStringSync(object[key], context, opts)
} else if (typeof val === "object") {
object[key] = module.exports.processObjectSync(object[key], context, opts)
object[key] = processObjectSync(object[key], context, opts)
}
}
return object
@ -143,7 +137,7 @@ module.exports.processObjectSync = (object, context, opts) => {
* @param {object|undefined} [opts] optional - specify some options for processing.
* @returns {string} The enriched string, all templates should have been replaced if they can be.
*/
module.exports.processStringSync = (string, context, opts) => {
export function processStringSync(string, context, opts) {
// Take a copy of input in case of error
const input = string
if (typeof string !== "string") {
@ -152,7 +146,7 @@ module.exports.processStringSync = (string, context, opts) => {
function process(stringPart) {
const template = createTemplate(stringPart, opts)
const now = Math.floor(Date.now() / 1000) * 1000
return processors.postprocess(
return postprocess(
template({
now: new Date(now).toISOString(),
__opts: {
@ -165,7 +159,7 @@ module.exports.processStringSync = (string, context, opts) => {
}
try {
if (opts && opts.onlyFound) {
const blocks = exports.findHBSBlocks(string)
const blocks = findHBSBlocks(string)
for (let block of blocks) {
const outcome = process(block)
string = string.replace(block, outcome)
@ -185,7 +179,7 @@ module.exports.processStringSync = (string, context, opts) => {
* this function will find any double braces and switch to triple.
* @param string the string to have double HBS statements converted to triple.
*/
module.exports.disableEscaping = string => {
export function disableEscaping(string) {
const matches = findDoubleHbsInstances(string)
if (matches == null) {
return string
@ -206,7 +200,7 @@ module.exports.disableEscaping = string => {
* @param {string} property The property which is to be wrapped.
* @returns {string} The wrapped property ready to be added to a templating string.
*/
module.exports.makePropSafe = property => {
export function makePropSafe(property) {
return `[${property}]`.replace("[[", "[").replace("]]", "]")
}
@ -216,7 +210,7 @@ module.exports.makePropSafe = property => {
* @param [opts] optional - specify some options for processing.
* @returns {boolean} Whether or not the input string is valid.
*/
module.exports.isValid = (string, opts) => {
export function isValid(string, opts) {
const validCases = [
"string",
"number",
@ -258,7 +252,7 @@ module.exports.isValid = (string, opts) => {
* This manifest provides information about each of the helpers and how it can be used.
* @returns The manifest JSON which has been generated from the helpers.
*/
module.exports.getManifest = () => {
export function getManifest() {
return manifest
}
@ -267,8 +261,8 @@ module.exports.getManifest = () => {
* @param handlebars the HBS expression to check
* @returns {boolean} whether the expression is JS or not
*/
module.exports.isJSBinding = handlebars => {
return module.exports.decodeJSBinding(handlebars) != null
export function isJSBinding(handlebars) {
return decodeJSBinding(handlebars) != null
}
/**
@ -276,7 +270,7 @@ module.exports.isJSBinding = handlebars => {
* @param javascript the JS code to encode
* @returns {string} the JS HBS expression
*/
module.exports.encodeJSBinding = javascript => {
export function encodeJSBinding(javascript) {
return `{{ js "${btoa(javascript)}" }}`
}
@ -285,7 +279,7 @@ module.exports.encodeJSBinding = javascript => {
* @param handlebars the JS HBS expression
* @returns {string|null} the raw JS code
*/
module.exports.decodeJSBinding = handlebars => {
export function decodeJSBinding(handlebars) {
if (!handlebars || typeof handlebars !== "string") {
return null
}
@ -310,7 +304,7 @@ module.exports.decodeJSBinding = handlebars => {
* @param {string[]} strings The strings to look for.
* @returns {boolean} Will return true if all strings found in HBS statement.
*/
module.exports.doesContainStrings = (template, strings) => {
export function doesContainStrings(template, strings) {
let regexp = new RegExp(FIND_HBS_REGEX)
let matches = template.match(regexp)
if (matches == null) {
@ -318,8 +312,8 @@ module.exports.doesContainStrings = (template, strings) => {
}
for (let match of matches) {
let hbs = match
if (exports.isJSBinding(match)) {
hbs = exports.decodeJSBinding(match)
if (isJSBinding(match)) {
hbs = decodeJSBinding(match)
}
let allFound = true
for (let string of strings) {
@ -340,7 +334,7 @@ module.exports.doesContainStrings = (template, strings) => {
* @param {string} string The string to search within.
* @return {string[]} The found HBS blocks.
*/
module.exports.findHBSBlocks = string => {
export function findHBSBlocks(string) {
if (!string || typeof string !== "string") {
return []
}
@ -361,15 +355,12 @@ module.exports.findHBSBlocks = string => {
* @param {string} string The word or sentence to search for.
* @returns {boolean} The this return true if the string is found, false if not.
*/
module.exports.doesContainString = (template, string) => {
return exports.doesContainStrings(template, [string])
export function doesContainString(template, string) {
return doesContainStrings(template, [string])
}
module.exports.setJSRunner = javascript.setJSRunner
module.exports.setOnErrorLog = javascript.setOnErrorLog
module.exports.convertToJS = hbs => {
const blocks = exports.findHBSBlocks(hbs)
export function convertToJS(hbs) {
const blocks = findHBSBlocks(hbs)
let js = "return `",
prevBlock = null
const variables = {}
@ -396,33 +387,34 @@ module.exports.convertToJS = hbs => {
return `${varBlock}${js}`
}
module.exports.FIND_ANY_HBS_REGEX = FIND_ANY_HBS_REGEX
const _FIND_ANY_HBS_REGEX = FIND_ANY_HBS_REGEX
export { _FIND_ANY_HBS_REGEX as FIND_ANY_HBS_REGEX }
const errors = require("./errors")
// We cannot use dynamic exports, otherwise the typescript file will not be generating it
module.exports.JsErrorTimeout = errors.JsErrorTimeout
export { JsErrorTimeout } from "./errors"
module.exports.helpersToRemoveForJs = helpersToRemoveForJs
const _helpersToRemoveForJs = helpersToRemoveForJs
export { _helpersToRemoveForJs as helpersToRemoveForJs }
function defaultJSSetup() {
if (!isBackendService()) {
/**
* Use polyfilled vm to run JS scripts in a browser Env
*/
javascript.setJSRunner((js, context) => {
setJSRunner((js, context) => {
context = {
...context,
alert: undefined,
setInterval: undefined,
setTimeout: undefined,
}
vm.createContext(context)
return vm.runInNewContext(js, context, { timeout: 1000 })
createContext(context)
return runInNewContext(js, context, { timeout: 1000 })
})
} else {
javascript.removeJSRunner()
removeJSRunner()
}
}
defaultJSSetup()
module.exports.defaultJSSetup = defaultJSSetup
const _defaultJSSetup = defaultJSSetup
export { _defaultJSSetup as defaultJSSetup }

View file

@ -1,4 +1,4 @@
import templates from "./index.js"
import * as templates from "./index.js"
/**
* ES6 entrypoint for rollup