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