als-render
Advanced tools
Comparing version 0.8.3 to 0.9.0
@@ -59,5 +59,8 @@ ## Usage | ||
const data = {} | ||
const selector = 'body' | ||
const selector = 'body' // optional. Default 'body' | ||
const version = '1.0.0' // optional. Default undefined | ||
const contextName = 'contextName' // optional. Default 'context' | ||
const path = './path/to/your/JSXComponent' | ||
render(path, selector, data).then(() => { | ||
render(path, data, {selector,version}) | ||
.then(() => { | ||
console.log('Component rendered successfully'); | ||
@@ -64,0 +67,0 @@ }); |
const build = require('./build') | ||
async function render(path, selector = 'body', data = {}, contextName) { | ||
async function render(path, data = {}, options = {}) { | ||
const { selector = 'body', contextName, version } = options | ||
Require.version = version | ||
const modules = new Require(path) | ||
await modules.getContent() | ||
const { resultFn, context, add } = build(modules, contextName,true,false) | ||
const { resultFn, context, add } = build(modules, contextName, true, false) | ||
context.data = data | ||
@@ -11,3 +13,4 @@ if (selector) { | ||
if (element) { | ||
element.innerHTML = add + resultFn(data).trim() | ||
const result = await resultFn(data) | ||
element.innerHTML = add + result.trim() | ||
context.runActions() | ||
@@ -14,0 +17,0 @@ } |
@@ -1,10 +0,16 @@ | ||
const buildContent = require('./build-content/index') | ||
const Context = require('./context/context') | ||
const jsxParser = require('./jsx/jsx-parser') | ||
const buildComponent = require('./build-component/index') | ||
const removeComments = require('./utils/remove-comments') | ||
function build(app, contextName = 'context',browser,ssr) { | ||
function build(modules, contextName = 'context',browser,ssr) { | ||
const context = new Context(browser,ssr) | ||
for (const path in app.contents) { | ||
app.contents[path] = buildContent(app.contents[path], path) | ||
for (const path in modules.contents) { | ||
const componentName = path.split('/').pop().replace(/\.js$/,'') | ||
const curPath = `context.currentPath = '${path}';\n` | ||
const newContent = curPath+jsxParser(removeComments(modules.contents[path]),componentName) | ||
modules.contents[path] = buildComponent(newContent,componentName) | ||
} | ||
const resultFn = app.build({}, context, contextName) | ||
const resultFn = modules.build({}, context, contextName) | ||
let add = '' | ||
@@ -11,0 +17,0 @@ let { links, styles } = context |
@@ -14,3 +14,2 @@ class Component { | ||
constructor(componentName, props = {},inner) { | ||
@@ -17,0 +16,0 @@ const { key = '' } = props |
@@ -14,5 +14,3 @@ const build = require('./build') | ||
static cache = {}; | ||
static render(path, data, minified) { | ||
return new Render(path, minified).build(data) | ||
} | ||
static render(path, data, minified) { return new Render(path, minified).build(data)} | ||
constructor(path, minified = Render.minified) { | ||
@@ -40,4 +38,14 @@ if (Render.cache[path]) { | ||
build(data = {}) { | ||
this.rawHtml = this.add + this.resultFn(data).trim() | ||
const result = this.resultFn(data) | ||
this.callBundle = `${Render.bundleName}(${JSON.stringify(data)})` | ||
if(result instanceof Promise) return new Promise(async (resolve,reject) => { | ||
try { | ||
const resolved = await result | ||
this.rawHtml = this.add + resolved.trim() | ||
return resolve(this) | ||
} catch (error) { | ||
return reject(error) | ||
} | ||
}) | ||
this.rawHtml = this.add + result.trim() | ||
return this | ||
@@ -44,0 +52,0 @@ } |
{ | ||
"name": "als-render", | ||
"version": "0.8.3", | ||
"version": "0.9.0", | ||
"main": "index.js", | ||
@@ -28,3 +28,3 @@ "scripts": { | ||
"dependencies": { | ||
"als-require": "^1.5.2", | ||
"als-require": "^1.6.0", | ||
"uglify-js": "^3.18.0" | ||
@@ -31,0 +31,0 @@ }, |
@@ -81,5 +81,8 @@ # ALS-Render | ||
const data = {} | ||
const selector = 'body' | ||
const selector = 'body' // optional. Default 'body' | ||
const version = '1.0.0' // optional. Default undefined | ||
const contextName = 'contextName' // optional. Default 'context' | ||
const path = './path/to/your/JSXComponent' | ||
render(path, selector, data).then(() => { | ||
render(path, data, {selector,version}) | ||
.then(() => { | ||
console.log('Component rendered successfully'); | ||
@@ -86,0 +89,0 @@ }); |
410
render.js
const Require = (function(){ | ||
const packageJsonCache = {}; | ||
function isCyclyc(fullPath, path) { | ||
if (Require.contents[fullPath] && Require.contents[fullPath].children.includes(path)) { | ||
throw `cyclic dependency between ${path} and ${fullPath}` | ||
} | ||
} | ||
function getFullPath(path, relative) { | ||
@@ -22,8 +17,3 @@ const pathParts = path.split('/'); | ||
} | ||
async function $fetch(path, type = 'text') { | ||
let response = await fetch(path) | ||
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`); | ||
return await response[type]() | ||
} | ||
async function getNodeModules(nodeModules, children, content) { | ||
async function getNodeModules(nodeModules, children, content,Require) { | ||
if (nodeModules.length === 0) return content | ||
@@ -49,3 +39,3 @@ for (let { match, modulePath } of nodeModules) { | ||
else { | ||
const { main = 'index.js' } = await $fetch(pkgJsonPath, 'json') | ||
const { main = 'index.js' } = await Require.fetch(pkgJsonPath, 'json') | ||
packageJsonCache[pkgJsonPath] = main | ||
@@ -64,3 +54,3 @@ filename = main | ||
if(!fullPath.endsWith('.js')) fullPath += '.js' | ||
isCyclyc(fullPath, modulePath) | ||
Require.isCyclyc(fullPath, modulePath) | ||
children.push(fullPath); | ||
@@ -77,3 +67,3 @@ content = content.replace(match, match.replace(r, (m,quoute) => { | ||
if (!Require.contents[path]) { | ||
let content = await $fetch(path) | ||
let content = await Require.fetch(path) | ||
const children = [], nodeModules = []; | ||
@@ -86,7 +76,7 @@ content = content.replace(/^(?!\/\/|\/\*.*\*\/).*require\(["'`](.*)["'`]\)/gm, (match, modulePath) => { | ||
const fullPath = getFullPath(modulePath, path) | ||
isCyclyc(fullPath, path) | ||
Require.isCyclyc(fullPath, path) | ||
children.push(fullPath); | ||
return match.replace(modulePath, fullPath) | ||
}); | ||
content = await getNodeModules(nodeModules, children, content) | ||
content = await getNodeModules(nodeModules, children, content,Require) | ||
Require.contents[path] = { content, children } | ||
@@ -152,2 +142,17 @@ } | ||
static contents = {} | ||
static version | ||
static isCyclyc(fullPath, path) { | ||
if (this.contents[fullPath] && this.contents[fullPath].children.includes(path)) { | ||
throw `cyclic dependency between ${path} and ${fullPath}` | ||
} | ||
} | ||
static async fetch(path, type = 'text') { | ||
if(this.version) path += '?version='+this.version | ||
let response = await fetch(path) | ||
if (!response.ok) console.error(`HTTP error! status: ${response.status}`); | ||
return await response[type]() | ||
} | ||
static async getModule(path, context, contextName, modules) { | ||
@@ -216,139 +221,5 @@ const mod = new Require(path) | ||
function require(path) { return modules[path] || null }; | ||
modules['/lib/context/component.js'] = (function (){ | ||
modules['/lib/utils/remove-comments.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
class Component { | ||
static fns = {} | ||
static components = {} | ||
static componentsToUpdate = {} | ||
static genHash(content) { | ||
const encoder = new TextEncoder(); | ||
const data = encoder.encode(content); | ||
let hash = 0, cur = 0; | ||
const fns = [n => { hash += n; return 1 }, n => { hash -= n; return 0 }] | ||
for (let b of data) { cur = fns[cur](b) } | ||
return hash | ||
} | ||
constructor(componentName, props = {},inner) { | ||
const { key = '' } = props | ||
const name = componentName + key | ||
if (Component.components[name]) { | ||
const component = Component.components[name] | ||
component.init(props,inner) | ||
return component | ||
} | ||
Component.components[name] = this | ||
this.mounted = false | ||
this.name = name | ||
this.selector = `[component=${this.name}]` | ||
this.fn = Component.fns[componentName] | ||
this.init() | ||
} | ||
init(props,inner) { | ||
this.actions = [] | ||
this.counter = 0; | ||
this.props = props | ||
this.inner = inner | ||
this.hooks = { mount: [() => this.mounted = true], unmount: [] } | ||
} | ||
addAction(event, fn) { | ||
const id = this.name + this.counter++ | ||
this.actions.push({ event, id, fn }) | ||
return id | ||
} | ||
on(event, fn) { | ||
if (!this.hooks[event]) return | ||
this.hooks[event].push(fn) | ||
} | ||
update(props=this.props,inner=this.inner) { | ||
this.props = props | ||
this.inner = inner | ||
const element = document.querySelector(this.selector) | ||
if(!element || !this.fn) return | ||
const newHtml = this.fn(props, inner, this) | ||
if(this.hash === this.oldHash) return | ||
element.outerHTML = newHtml | ||
Component.context.runActions() | ||
} | ||
genHash(content) { | ||
const hash = Component.genHash(content+this.name) | ||
this.oldHash = this.hash | ||
Component.componentsToUpdate[this.name] = this | ||
this.hash = hash | ||
return content | ||
} | ||
} | ||
module.exports = Component | ||
return module.exports; | ||
})(); | ||
modules['/lib/context/context.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
const Component = require('/lib/context/component.js') | ||
class Context { | ||
links = []; styles = []; counter=0; | ||
constructor(browser = true,ssr=false) { | ||
Component.context = this; | ||
this.browser = browser | ||
this.ssr = ssr | ||
} | ||
addComponentFn(name,fn) { Component.fns[name] = fn} | ||
component(componentName, props, inner) { return new Component(componentName,props,inner) } | ||
style(styles) { this.styles.push(styles) } | ||
runActions() { | ||
for (const name in Component.componentsToUpdate) { | ||
const component = Component.componentsToUpdate[name] | ||
const { actions, hooks, selector } = component | ||
const element = document.querySelector(selector) | ||
const parent = element || document | ||
hooks.mount.forEach(fn => fn(element)) | ||
actions.forEach(({ event, fn, id }) => { | ||
const elementForEvent = parent.querySelector(`[${event}="${id}"]`) | ||
if (!elementForEvent) return | ||
if (event === 'load') fn(elementForEvent) | ||
else elementForEvent.addEventListener(event, fn) | ||
}) | ||
component.actions = [] | ||
hooks.mount = [] | ||
} | ||
for (const name in Component.components) { | ||
const { selector, hooks } = Component.components[name] | ||
if (document.querySelector(selector)) continue | ||
hooks.unmount.forEach(fn => fn()); | ||
delete Component.components[name] | ||
} | ||
Component.componentsToUpdate = {} | ||
} | ||
link(link) { | ||
const arr = this.currentPath.split('/'); | ||
arr.pop(); | ||
link.split('/').forEach(part => { | ||
if (part === '..') arr.pop() | ||
else if (part !== '.') arr.push(part) | ||
}) | ||
this.links.push(arr.join('/')) | ||
} | ||
} | ||
module.exports = Context | ||
return module.exports; | ||
})(); | ||
modules['/lib/build-content/remove-comments.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
function removeComments(code) { | ||
@@ -368,3 +239,3 @@ const r = /`.*?\/\/.*?`|".*?\/\/.*?"|'.*?\/\/.*?'/ | ||
})(); | ||
modules['/lib/build-content/jsx/breckets.js'] = (function (){ | ||
modules['/lib/jsx/breckets.js'] = (function (){ | ||
const module = { exports: {} } | ||
@@ -405,16 +276,16 @@ const exports = module.exports | ||
})(); | ||
modules['/lib/build-content/build-component/get-function.js'] = (function (){ | ||
modules['/lib/build-component/get-function.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
const getInsideBreckets = require('/lib/build-content/jsx/breckets.js') | ||
const getInsideBreckets = require('/lib/jsx/breckets.js') | ||
function getFunction(content,componentName) { | ||
const all = '[\\s\\S]*?',s = '\\s*?' | ||
const r = new RegExp(`^\\s*?function${s}${componentName}${s}\\(${all}\\)${all}{`,'m') | ||
function getFunction(content, componentName) { | ||
const all = '[\\s\\S]*?', s = '\\s*?' | ||
const r = new RegExp(`^\\s*?(async\\s)?function${s}${componentName}${s}\\(${all}\\)${all}{`, 'm') | ||
const fnStart = content.match(r) | ||
if(!fnStart) return null | ||
const start = fnStart.index+fnStart[0].length | ||
const end = getInsideBreckets(start,content) | ||
return content.slice(fnStart.index,end) | ||
if (!fnStart) return null | ||
const start = fnStart.index + fnStart[0].length | ||
const end = getInsideBreckets(start, content) | ||
return content.slice(fnStart.index, end) | ||
} | ||
@@ -425,6 +296,6 @@ | ||
})(); | ||
modules['/lib/build-content/build-component/index.js'] = (function (){ | ||
modules['/lib/build-component/index.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
const getFunction = require('/lib/build-content/build-component/get-function.js') | ||
const getFunction = require('/lib/build-component/get-function.js') | ||
@@ -434,9 +305,10 @@ function buildComponentFn(content,componentName) { | ||
const originalFn = getFunction(content,componentName) | ||
if(originalFn === null) return content | ||
const newFunction = /*js*/`function ${componentName}(props={},inner) { | ||
const isAsync = originalFn.startsWith('async') | ||
const newFunction = /*js*/`${isAsync ? 'async ' : ''}function ${componentName}(props={},inner) { | ||
let originalFn = ${originalFn} | ||
const component = context.component('${componentName}',props,inner) | ||
originalFn = originalFn.bind(component) | ||
return component.genHash(originalFn(props,inner)) | ||
const result = ${isAsync ? 'await ' : ''}originalFn(props,inner) | ||
return component.genHash(result) | ||
} | ||
@@ -451,3 +323,3 @@ context.addComponentFn('${componentName}',${componentName}) | ||
})(); | ||
modules['/lib/build-content/jsx/outer.js'] = (function (){ | ||
modules['/lib/jsx/outer.js'] = (function (){ | ||
const module = { exports: {} } | ||
@@ -471,3 +343,4 @@ const exports = module.exports | ||
const atts = [...attributes,...props].map(([name,value]) => { | ||
return value ? `${name}="${value}"` : name | ||
if(value) return `${name}="${value.replace(/\"/g,'\\\"')}"` | ||
else return name | ||
}).join(' ') | ||
@@ -483,3 +356,3 @@ outer = `<${tagName}${atts.length ? ' '+atts : ''}>` | ||
})(); | ||
modules['/lib/build-content/jsx/attributes/build-action.js'] = (function (){ | ||
modules['/lib/jsx/attributes/build-action.js'] = (function (){ | ||
const module = { exports: {} } | ||
@@ -497,3 +370,3 @@ const exports = module.exports | ||
})(); | ||
modules['/lib/build-content/jsx/attributes/build-prop.js'] = (function (){ | ||
modules['/lib/jsx/attributes/build-prop.js'] = (function (){ | ||
const module = { exports: {} } | ||
@@ -503,3 +376,3 @@ const exports = module.exports | ||
const buildAction = require('/lib/build-content/jsx/attributes/build-action.js') | ||
const buildAction = require('/lib/jsx/attributes/build-action.js') | ||
function buildProp(toAdd, to, element) { | ||
@@ -510,4 +383,3 @@ if (!toAdd) return | ||
if (singleProps.includes(name)) { | ||
name = `\${${value} ? '${name}' : ''}` | ||
value = undefined | ||
if(value) name = `\${${value} ? '${name}' : ''}` | ||
element.attributes.push([name]) | ||
@@ -521,6 +393,6 @@ } else if (to === 'props' && name.startsWith('on')) buildAction(toAdd, element) | ||
})(); | ||
modules['/lib/build-content/jsx/attributes/get-attributes.js'] = (function (){ | ||
modules['/lib/jsx/attributes/get-attributes.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
const buildProp = require('/lib/build-content/jsx/attributes/build-prop.js') | ||
const buildProp = require('/lib/jsx/attributes/build-prop.js') | ||
function getAttributes(char, i, element, content) { | ||
@@ -547,4 +419,9 @@ let prop = '', value = '', parsingProp = true, parsingValue = false, open, count = 0; | ||
if (prop.length > 0) { | ||
parsingProp = false; | ||
parsingValue = true | ||
if(char === ' ' && content[i+1] !== '=') { | ||
element.attributes.push([prop]) | ||
prop = '' | ||
} else { | ||
parsingProp = false; | ||
parsingValue = true | ||
} | ||
} | ||
@@ -595,8 +472,8 @@ } else if(content[i+1] === '>') { | ||
})(); | ||
modules['/lib/build-content/jsx/element.js'] = (function (){ | ||
modules['/lib/jsx/element.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
const getAttributes = require('/lib/build-content/jsx/attributes/get-attributes.js') | ||
const getInsideBreckets = require('/lib/build-content/jsx/breckets.js') | ||
const getOuter = require('/lib/build-content/jsx/outer.js') | ||
const getAttributes = require('/lib/jsx/attributes/get-attributes.js') | ||
const getInsideBreckets = require('/lib/jsx/breckets.js') | ||
const getOuter = require('/lib/jsx/outer.js') | ||
class Element { | ||
@@ -658,3 +535,3 @@ tagName = ''; rest = ''; inner = ''; attributes = []; props = []; selfClosed = false; | ||
i = getInsideBreckets(i, this.inner) | ||
const inside = this.inner.slice(start, i) | ||
const inside = this.inner.slice(start, i+1) | ||
newInner += '$' + Element.jsxParser(inside) | ||
@@ -670,6 +547,6 @@ } else newInner += this.inner[i] | ||
})(); | ||
modules['/lib/build-content/jsx/jsx-parser.js'] = (function (){ | ||
modules['/lib/jsx/jsx-parser.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
const Element = require('/lib/build-content/jsx/element.js') | ||
const Element = require('/lib/jsx/element.js') | ||
@@ -699,31 +576,153 @@ function jsxParser(content,componentName) { | ||
})(); | ||
modules['/lib/build-content/index.js'] = (function (){ | ||
modules['/lib/context/component.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
const jsxParser = require('/lib/build-content/jsx/jsx-parser.js') | ||
const buildComponent = require('/lib/build-content/build-component/index.js') | ||
const removeComments = require('/lib/build-content/remove-comments.js') | ||
class Component { | ||
static fns = {} | ||
static components = {} | ||
static componentsToUpdate = {} | ||
static genHash(content) { | ||
const encoder = new TextEncoder(); | ||
const data = encoder.encode(content); | ||
let hash = 0, cur = 0; | ||
const fns = [n => { hash += n; return 1 }, n => { hash -= n; return 0 }] | ||
for (let b of data) { cur = fns[cur](b) } | ||
return hash | ||
} | ||
function buildContent(content,path) { | ||
const componentName = path.split('/').pop().replace(/\.js$/,'') | ||
const curPath = `context.currentPath = '${path}';\n` | ||
const newContent = curPath+jsxParser(removeComments(content),componentName) | ||
return buildComponent(newContent,componentName) | ||
constructor(componentName, props = {},inner) { | ||
const { key = '' } = props | ||
const name = componentName + key | ||
if (Component.components[name]) { | ||
const component = Component.components[name] | ||
component.init(props,inner) | ||
return component | ||
} | ||
Component.components[name] = this | ||
this.mounted = false | ||
this.name = name | ||
this.selector = `[component=${this.name}]` | ||
this.fn = Component.fns[componentName] | ||
this.init() | ||
} | ||
init(props,inner) { | ||
this.actions = [] | ||
this.counter = 0; | ||
this.props = props | ||
this.inner = inner | ||
this.hooks = { mount: [() => this.mounted = true], unmount: [] } | ||
} | ||
addAction(event, fn) { | ||
const id = this.name + this.counter++ | ||
this.actions.push({ event, id, fn }) | ||
return id | ||
} | ||
on(event, fn) { | ||
if (!this.hooks[event]) return | ||
this.hooks[event].push(fn) | ||
} | ||
update(props=this.props,inner=this.inner) { | ||
this.props = props | ||
this.inner = inner | ||
const element = document.querySelector(this.selector) | ||
if(!element || !this.fn) return | ||
const newHtml = this.fn(props, inner, this) | ||
if(this.hash === this.oldHash) return | ||
element.outerHTML = newHtml | ||
Component.context.runActions() | ||
} | ||
genHash(content) { | ||
const hash = Component.genHash(content+this.name) | ||
this.oldHash = this.hash | ||
Component.componentsToUpdate[this.name] = this | ||
this.hash = hash | ||
return content | ||
} | ||
} | ||
module.exports = buildContent | ||
module.exports = Component | ||
return module.exports; | ||
})(); | ||
modules['/lib/context/context.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
const Component = require('/lib/context/component.js') | ||
class Context { | ||
links = []; styles = []; counter=0; | ||
constructor(browser = true,ssr=false) { | ||
Component.context = this; | ||
this.browser = browser | ||
this.ssr = ssr | ||
} | ||
addComponentFn(name,fn) { Component.fns[name] = fn} | ||
component(componentName, props, inner) { return new Component(componentName,props,inner) } | ||
style(styles) { this.styles.push(styles) } | ||
runActions() { | ||
for (const name in Component.componentsToUpdate) { | ||
const component = Component.componentsToUpdate[name] | ||
const { actions, hooks, selector } = component | ||
const element = document.querySelector(selector) | ||
const parent = element || document | ||
hooks.mount.forEach(fn => fn(element)) | ||
actions.forEach(({ event, fn, id }) => { | ||
const elementForEvent = parent.querySelector(`[${event}="${id}"]`) | ||
if (!elementForEvent) return | ||
if (event === 'load') fn(elementForEvent) | ||
else elementForEvent.addEventListener(event, fn) | ||
}) | ||
component.actions = [] | ||
hooks.mount = [] | ||
} | ||
for (const name in Component.components) { | ||
const { selector, hooks } = Component.components[name] | ||
if (document.querySelector(selector)) continue | ||
hooks.unmount.forEach(fn => fn()); | ||
delete Component.components[name] | ||
} | ||
Component.componentsToUpdate = {} | ||
} | ||
link(link) { | ||
const arr = this.currentPath.split('/'); | ||
arr.pop(); | ||
link.split('/').forEach(part => { | ||
if (part === '..') arr.pop() | ||
else if (part !== '.') arr.push(part) | ||
}) | ||
this.links.push(arr.join('/')) | ||
} | ||
} | ||
module.exports = Context | ||
return module.exports; | ||
})(); | ||
modules['/lib/build.js'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
const buildContent = require('/lib/build-content/index.js') | ||
const Context = require('/lib/context/context.js') | ||
const jsxParser = require('/lib/jsx/jsx-parser.js') | ||
const buildComponent = require('/lib/build-component/index.js') | ||
const removeComments = require('/lib/utils/remove-comments.js') | ||
function build(app, contextName = 'context',browser,ssr) { | ||
function build(modules, contextName = 'context',browser,ssr) { | ||
const context = new Context(browser,ssr) | ||
for (const path in app.contents) { | ||
app.contents[path] = buildContent(app.contents[path], path) | ||
for (const path in modules.contents) { | ||
const componentName = path.split('/').pop().replace(/\.js$/,'') | ||
const curPath = `context.currentPath = '${path}';\n` | ||
const newContent = curPath+jsxParser(removeComments(modules.contents[path]),componentName) | ||
modules.contents[path] = buildComponent(newContent,componentName) | ||
} | ||
const resultFn = app.build({}, context, contextName) | ||
const resultFn = modules.build({}, context, contextName) | ||
let add = '' | ||
@@ -748,6 +747,8 @@ let { links, styles } = context | ||
async function render(path, selector = 'body', data = {}, contextName) { | ||
async function render(path, data = {}, options = {}) { | ||
const { selector = 'body', contextName, version } = options | ||
Require.version = version | ||
const modules = new Require(path) | ||
await modules.getContent() | ||
const { resultFn, context, add } = build(modules, contextName,true,false) | ||
const { resultFn, context, add } = build(modules, contextName, true, false) | ||
context.data = data | ||
@@ -757,3 +758,4 @@ if (selector) { | ||
if (element) { | ||
element.innerHTML = add + resultFn(data).trim() | ||
const result = await resultFn(data) | ||
element.innerHTML = add + result.trim() | ||
context.runActions() | ||
@@ -777,4 +779,4 @@ } | ||
} catch(error) { | ||
parseError(error, {"/lib/context/component.js":{"from":4,"to":78},"/lib/context/context.js":{"from":79,"to":137},"/lib/build-content/remove-comments.js":{"from":138,"to":154},"/lib/build-content/jsx/breckets.js":{"from":155,"to":190},"/lib/build-content/build-component/get-function.js":{"from":191,"to":209},"/lib/build-content/build-component/index.js":{"from":210,"to":233},"/lib/build-content/jsx/outer.js":{"from":234,"to":263},"/lib/build-content/jsx/attributes/build-action.js":{"from":264,"to":276},"/lib/build-content/jsx/attributes/build-prop.js":{"from":277,"to":297},"/lib/build-content/jsx/attributes/get-attributes.js":{"from":298,"to":369},"/lib/build-content/jsx/element.js":{"from":370,"to":442},"/lib/build-content/jsx/jsx-parser.js":{"from":443,"to":470},"/lib/build-content/index.js":{"from":471,"to":487},"/lib/build.js":{"from":488,"to":513},"/lib/browser.js":{"from":514,"to":537}}, 537) | ||
parseError(error, {"/lib/utils/remove-comments.js":{"from":4,"to":20},"/lib/jsx/breckets.js":{"from":21,"to":56},"/lib/build-component/get-function.js":{"from":57,"to":75},"/lib/build-component/index.js":{"from":76,"to":100},"/lib/jsx/outer.js":{"from":101,"to":131},"/lib/jsx/attributes/build-action.js":{"from":132,"to":144},"/lib/jsx/attributes/build-prop.js":{"from":145,"to":164},"/lib/jsx/attributes/get-attributes.js":{"from":165,"to":241},"/lib/jsx/element.js":{"from":242,"to":314},"/lib/jsx/jsx-parser.js":{"from":315,"to":342},"/lib/context/component.js":{"from":343,"to":416},"/lib/context/context.js":{"from":417,"to":475},"/lib/build.js":{"from":476,"to":507},"/lib/browser.js":{"from":508,"to":534}}, 534) | ||
} | ||
})() |
@@ -1,17 +0,18 @@ | ||
let Require=(()=>{let h={};function d(t,e){if(o.contents[t]&&o.contents[t].children.includes(e))throw`cyclic dependency between ${e} and `+t}function i(t,e){var n,t=t.split("/"),r=[];for(n of[...e.split("/").slice(0,-1),...t])".."===n?0<r.length&&".."!==r[r.length-1]?r.pop():r.push(n):"."!==n&&r.push(n);e=r.join("/");return e.endsWith(".js")?e:e+".js"}async function p(t,e="text"){t=await fetch(t);if(t.ok)return t[e]();throw new Error("HTTP error! status: "+t.status)}async function t({contents:n,fullPath:t},a){let r=async s=>{if(void 0===n[s]){if(!a.contents[s]){let t=await p(s),r=[],o=[];t=t.replace(/^(?!\/\/|\/\*.*\*\/).*require\(["'`](.*)["'`]\)/gm,(t,e)=>{var n;return e.startsWith(".")?(d(n=i(e,s),s),r.push(n),t.replace(e,n)):(o.push({match:t,modulePath:e}),t)}),t=await(async(t,o,s)=>{if(0!==t.length)for(var{match:a,modulePath:i}of t){let n,t,e,r=i;i.includes("/")&&(c=i.split("/"),r=c.shift(),t=c.join("/"));var c=`/node_modules/${r}/package.json`,l=await fetch(c,{method:"HEAD"}),u=new RegExp(`require\\((["'\`])${i}["'\`]\\)`);!1===l.ok?(s=s.replace(u,"{}"),console.warn(`The module "${i}" can't be imported and will be replaced with {}`)):(e=h[c]||({main:l="index.js"}=await p(c,"json"),h[c]=l),(n=(n=t?((l=e.split("/")).pop(),`/node_modules/${r}/${l.join("/")}/`+t):`/node_modules/${r}/`+e).replace(/\/\.?\//g,"/")).endsWith(".js")||(n+=".js"),d(n,i),o.push(n),s=s.replace(a,a.replace(u,(t,e)=>`require(${e}${n}${e})`)))}return s})(o,r,t),a.contents[s]={content:t,children:r}}let{content:t,children:e}=a.contents[s];n[s]=t,await Promise.all(e.map(t=>r(t)))}};await r(t)}class o{static contents={};static async getModule(t,e,n,r){t=new o(t);return await t.getContent(),t.build(r,e,n)}constructor(t){this.contents={},this.path=t,this.fullPath=i(t,location.pathname),this.contentReady=!1}async getContent(){return this.contentReady||(await t(this,o),this.keys=((e,n)=>{let r=new Set,o=t=>{t.forEach(t=>{e[t]&&(o(n.contents[t].children),r.add(t))})};return o(Object.keys(e).reverse()),Array.from(r)})(this.contents,o),this.contentReady=!0),this}build(t={},e={},r="context"){var{fn:r,modulesLines:o,curLastLine:s}=((t="context",r)=>{let o={},s=3;var e=r.keys.map((t,e)=>{let n=`modules['${t}'] = (function (){ | ||
let Require=(()=>{let d={};function a(t,e){var n,t=t.split("/"),s=[];for(n of[...e.split("/").slice(0,-1),...t])".."===n?0<s.length&&".."!==s[s.length-1]?s.pop():s.push(n):"."!==n&&s.push(n);e=s.join("/");return e.endsWith(".js")?e:e+".js"}async function t({contents:n,fullPath:t},i){let s=async o=>{if(void 0===n[o]){if(!i.contents[o]){let t=await i.fetch(o),s=[],r=[];t=t.replace(/^(?!\/\/|\/\*.*\*\/).*require\(["'`](.*)["'`]\)/gm,(t,e)=>{var n;return e.startsWith(".")?(n=a(e,o),i.isCyclyc(n,o),s.push(n),t.replace(e,n)):(r.push({match:t,modulePath:e}),t)}),t=await(async(t,r,o,i)=>{if(0!==t.length)for(var{match:a,modulePath:c}of t){let n,t,e,s=c;c.includes("/")&&(l=c.split("/"),s=l.shift(),t=l.join("/"));var l=`/node_modules/${s}/package.json`,u=await fetch(l,{method:"HEAD"}),h=new RegExp(`require\\((["'\`])${c}["'\`]\\)`);!1===u.ok?(o=o.replace(h,"{}"),console.warn(`The module "${c}" can't be imported and will be replaced with {}`)):(e=d[l]||({main:u="index.js"}=await i.fetch(l,"json"),d[l]=u),(n=(n=t?((u=e.split("/")).pop(),`/node_modules/${s}/${u.join("/")}/`+t):`/node_modules/${s}/`+e).replace(/\/\.?\//g,"/")).endsWith(".js")||(n+=".js"),i.isCyclyc(n,c),r.push(n),o=o.replace(a,a.replace(h,(t,e)=>`require(${e}${n}${e})`)))}return o})(r,s,t,i),i.contents[o]={content:t,children:s}}let{content:t,children:e}=i.contents[o];n[o]=t,await Promise.all(e.map(t=>s(t)))}};await s(t)}class r{static contents={};static version;static isCyclyc(t,e){if(this.contents[t]&&this.contents[t].children.includes(e))throw`cyclic dependency between ${e} and `+t}static async fetch(t,e="text"){this.version&&(t+="?version="+this.version);t=await fetch(t);return t.ok||console.error("HTTP error! status: "+t.status),t[e]()}static async getModule(t,e,n,s){t=new r(t);return await t.getContent(),t.build(s,e,n)}constructor(t){this.contents={},this.path=t,this.fullPath=a(t,location.pathname),this.contentReady=!1}async getContent(){return this.contentReady||(await t(this,r),this.keys=((e,n)=>{let s=new Set,r=t=>{t.forEach(t=>{e[t]&&(r(n.contents[t].children),s.add(t))})};return r(Object.keys(e).reverse()),Array.from(s)})(this.contents,r),this.contentReady=!0),this}build(t={},e={},s="context"){var{fn:s,modulesLines:r,curLastLine:o}=((t="context",s)=>{let r={},o=3;var e=s.keys.map((t,e)=>{let n=`modules['${t}'] = (function (){ | ||
const module = { exports: {} } | ||
const exports = module.exports | ||
${r.contents[t]} | ||
${s.contents[t]} | ||
return module.exports; | ||
})();`;return e===r.keys.length-1&&(n+=` | ||
return modules['${t}']`),o[t]={from:s+1},s+=n.split("\n").length,o[t].to=s,n}).join("\n");return{fn:new Function("modules",t,`function require(path) { return modules[path] || null }; | ||
`+e),modulesLines:o,curLastLine:s}})(r,this);try{return r(t,e)}catch(n){{r=n;var a=o;var i=s;let[t,...e]=r.stack.split("\n");throw e=e.map(t=>{var e=t.match(/<anonymous>:(\d*):(\d*)\)$/);if(e){let n=Number(e[1]);if(n+1!==i){var r,o,e=Number(e[2]),s=Object.entries(a).filter(([,{from:t,to:e}])=>n>=t&&n<=e);if(0!==s.length)return[s,{from:r,to:o}]=s[0],` at ${t.match(/at\s(.*?)\s/)[1]} ${s} (${n-r-2}:${e})`}}}).filter(Boolean),r.stack=t+"\n"+e.join("\n"),r;return}}}}return o})(),require=Require.getModule; | ||
const render = (()=>{function t(e,t){function n(t){return e[t]||null}var s;return e["/lib/context/component.js"]=(()=>{var t={exports:{}};class r{static fns={};static components={};static componentsToUpdate={};static genHash(t){t=(new TextEncoder).encode(t);let e=0,n=0;var s,o=[t=>(e+=t,1),t=>(e-=t,0)];for(s of t)n=o[n](s);return e}constructor(t,e={},n){var s,{key:o=""}=e,o=t+o;if(r.components[o])return(s=r.components[o]).init(e,n),s;(r.components[o]=this).mounted=!1,this.name=o,this.selector=`[component=${this.name}]`,this.fn=r.fns[t],this.init()}init(t,e){this.actions=[],this.counter=0,this.props=t,this.inner=e,this.hooks={mount:[()=>this.mounted=!0],unmount:[]}}addAction(t,e){var n=this.name+this.counter++;return this.actions.push({event:t,id:n,fn:e}),n}on(t,e){this.hooks[t]&&this.hooks[t].push(e)}update(t=this.props,e=this.inner){this.props=t,this.inner=e;var n=document.querySelector(this.selector);n&&this.fn&&(t=this.fn(t,e,this),this.hash!==this.oldHash)&&(n.outerHTML=t,r.context.runActions())}genHash(t){var e=r.genHash(t+this.name);return this.oldHash=this.hash,(r.componentsToUpdate[this.name]=this).hash=e,t}}return t.exports=r,t.exports})(),e["/lib/context/context.js"]=(()=>{var t={exports:{}};let l=n("/lib/context/component.js");return t.exports=class{links=[];styles=[];counter=0;constructor(t=!0,e=!1){(l.context=this).browser=t,this.ssr=e}addComponentFn(t,e){l.fns[t]=e}component(t,e,n){return new l(t,e,n)}style(t){this.styles.push(t)}runActions(){for(var t in l.componentsToUpdate){var t=l.componentsToUpdate[t],{actions:n,hooks:o,selector:r}=t;let e=document.querySelector(r),s=e||document;o.mount.forEach(t=>t(e)),n.forEach(({event:t,fn:e,id:n})=>{n=s.querySelector(`[${t}="${n}"]`);n&&("load"===t?e(n):n.addEventListener(t,e))}),t.actions=[],o.mount=[]}for(var e in l.components){var{selector:s,hooks:i}=l.components[e];document.querySelector(s)||(i.unmount.forEach(t=>t()),delete l.components[e])}l.componentsToUpdate={}}link(t){let e=this.currentPath.split("/");e.pop(),t.split("/").forEach(t=>{".."===t?e.pop():"."!==t&&e.push(t)}),this.links.push(e.join("/"))}},t.exports})(),e["/lib/build-content/remove-comments.js"]=((s={exports:{}}).exports=function(t){let s=/`.*?\/\/.*?`|".*?\/\/.*?"|'.*?\/\/.*?'/;return t.replace(/^(.*)(\/\/.*)($|\n)/gm,(t,e,n)=>{n=(e+n).match(s);return n&&n.index<e.length?t:e}).replace(/\{?\/\*[\s\S]*?\*\/\}?/gm,"")},s.exports),e["/lib/build-content/jsx/breckets.js"]=((s={exports:{}}).exports=function(e,n){let t=0,s={'"':[],"'":[],"`":[]};for(n.replace(/["'`]/g,(t,e)=>{"\\"!==n[e-1]&&s[t].push(e)});e<n.length;){var o=n[++e];if(s[o]&&s[o].length){let t;for(var r of s[o])if(r>e){t=r;break}t&&(e=t,s[o]=s[o].filter(t=>e<t))}else if("{"===o)t++;else if("}"===o){if(!(0<t))break;t--}}return e+1},s.exports),e["/lib/build-content/build-component/get-function.js"]=(()=>{var t={exports:{}};let s=n("/lib/build-content/jsx/breckets.js");return t.exports=function(t,e){var n="[\\s\\S]*?",e=new RegExp(`^\\s*?function\\s*?${e}\\s*?\\(${n}\\)${n}{`,"m");return(n=t.match(e))?(e=n.index+n[0].length,e=s(e,t),t.slice(n.index,e)):null},t.exports})(),e["/lib/build-content/build-component/index.js"]=(()=>{var t={exports:{}};let s=n("/lib/build-content/build-component/get-function.js");return t.exports=function(t,e){var n;return!1===/[A-Z]/.test(e[0])||null===(n=s(t,e))?t:t.replace(n,`function ${e}(props={},inner) { | ||
let originalFn = ${n} | ||
})();`;return e===s.keys.length-1&&(n+=` | ||
return modules['${t}']`),r[t]={from:o+1},o+=n.split("\n").length,r[t].to=o,n}).join("\n");return{fn:new Function("modules",t,`function require(path) { return modules[path] || null }; | ||
`+e),modulesLines:r,curLastLine:o}})(s,this);try{return s(t,e)}catch(n){{s=n;var i=r;var a=o;let[t,...e]=s.stack.split("\n");throw e=e.map(t=>{var e=t.match(/<anonymous>:(\d*):(\d*)\)$/);if(e){let n=Number(e[1]);if(n+1!==a){var s,r,e=Number(e[2]),o=Object.entries(i).filter(([,{from:t,to:e}])=>n>=t&&n<=e);if(0!==o.length)return[o,{from:s,to:r}]=o[0],` at ${t.match(/at\s(.*?)\s/)[1]} ${o} (${n-s-2}:${e})`}}}).filter(Boolean),s.stack=t+"\n"+e.join("\n"),s;return}}}}return r})(),require=Require.getModule; | ||
const render = (()=>{function t(e,t){function s(t){return e[t]||null}var n;return e["/lib/utils/remove-comments.js"]=((n={exports:{}}).exports=function(t){let n=/`.*?\/\/.*?`|".*?\/\/.*?"|'.*?\/\/.*?'/;return t.replace(/^(.*)(\/\/.*)($|\n)/gm,(t,e,s)=>{s=(e+s).match(n);return s&&s.index<e.length?t:e}).replace(/\{?\/\*[\s\S]*?\*\/\}?/gm,"")},n.exports),e["/lib/jsx/breckets.js"]=((n={exports:{}}).exports=function(e,s){let t=0,n={'"':[],"'":[],"`":[]};for(s.replace(/["'`]/g,(t,e)=>{"\\"!==s[e-1]&&n[t].push(e)});e<s.length;){var r=s[++e];if(n[r]&&n[r].length){let t;for(var o of n[r])if(o>e){t=o;break}t&&(e=t,n[r]=n[r].filter(t=>e<t))}else if("{"===r)t++;else if("}"===r){if(!(0<t))break;t--}}return e+1},n.exports),e["/lib/build-component/get-function.js"]=(()=>{var t={exports:{}};let n=s("/lib/jsx/breckets.js");return t.exports=function(t,e){var s="[\\s\\S]*?",e=new RegExp(`^\\s*?(async\\s)?function\\s*?${e}\\s*?\\(${s}\\)${s}{`,"m");return(s=t.match(e))?(e=s.index+s[0].length,e=n(e,t),t.slice(s.index,e)):null},t.exports})(),e["/lib/build-component/index.js"]=(()=>{var t={exports:{}};let r=s("/lib/build-component/get-function.js");return t.exports=function(t,e){var s,n;return!1===/[A-Z]/.test(e[0])||null===(s=r(t,e))?t:(n=s.startsWith("async"),t.replace(s,`${n?"async ":""}function ${e}(props={},inner) { | ||
let originalFn = ${s} | ||
const component = context.component('${e}',props,inner) | ||
originalFn = originalFn.bind(component) | ||
return component.genHash(originalFn(props,inner)) | ||
const result = ${n?"await ":""}originalFn(props,inner) | ||
return component.genHash(result) | ||
} | ||
context.addComponentFn('${e}',${e}) | ||
`)},t.exports})(),e["/lib/build-content/jsx/outer.js"]=((s={exports:{}}).exports=function(t){let{tagName:e,selfClosed:n,attributes:s,props:o,isComponent:r,rest:i,inner:l}=t,u="";if(r){t=[...o,...s.map(([t,e])=>[t,'"'+e+'"'])].map(([t,e])=>t+":"+e);i&&t.push(i),o="{"+t.join(",")+"}",u="${"+`${e}(${o},\`${l}\`)`+"}"}else{if(""===e)return l||"";o=o.map(([t,e])=>[t,"${"+e+"}"]);t=[...s,...o].map(([t,e])=>e?t+`="${e}"`:t).join(" ");u=`<${e}${t.length?" "+t:""}>`,n||(u+=l+`</${e}>`)}return u},s.exports),e["/lib/build-content/jsx/attributes/build-action.js"]=((s={exports:{}}).exports=function(t,e){var[t,n]=t,t=t.split("on")[1].toLowerCase();e.attributes.push([t,"$"+`{this.addAction('${t}',${n})}`])},s.exports),e["/lib/build-content/jsx/attributes/build-prop.js"]=(()=>{var t={exports:{}};let r=["disabled","checked","readonly","required","hidden","autofocus","multiple","selected","controls","loop","muted","open","spellcheck","draggable","contenteditable","novalidate"],i=n("/lib/build-content/jsx/attributes/build-action.js");return t.exports=function(n,s,o){if(n){let[t,e]=n;"className"===t&&(t="class"),r.includes(t)?(t=`\${${e} ? '${t}' : ''}`,e=void 0,o.attributes.push([t])):"props"===s&&t.startsWith("on")?i(n,o):o[s].push([t,e])}},t.exports})(),e["/lib/build-content/jsx/attributes/get-attributes.js"]=(()=>{var t={exports:{}};let p=n("/lib/build-content/jsx/attributes/build-prop.js");return t.exports=function(t,e,s,o){let r="",i="",l=!0,u=!1,c,n=0;function a(t,e,n="attributes"){for(p(e,n,s),l=!0,u=!1,r="",i="",c=null;0===o[t].trim().length;)t++;return t}for(;">"!==t&&!(e>=o.length);){if(l)if("{"===t){for(;e<o.length&&"}"!==(t=o[++e]);)s.rest+=t;a(e)}else if("="===t||0===t.trim().length)0<r.length&&(l=!1,u=!0);else{if(">"===o[e+1]){"/"===t?s.selfClosed=!0:""!==r&&s.attributes.push([r+t]),e++;break}r+=t}else u&&(c?"{"===c?(i+=t,"{"===t?n++:"}"===t&&(0<n?n--:e=a(e,[r,i.slice(0,-1)],"props"))):"\\"!==o[e-1]&&t===c?e=a(e,[r,i]):i+=t:/["'`{]/.test(t)?c=t:/[a-zA-Z]/.test(t)&&(""!==r&&s.attributes.push([r]),l=!0,u=!1,r=t));">"===(t=o[++e])&&u&&(i+=t,t=o[++e])}return++e},t.exports})(),e["/lib/build-content/jsx/element.js"]=(()=>{var t={exports:{}};let s=n("/lib/build-content/jsx/attributes/get-attributes.js"),o=n("/lib/build-content/jsx/breckets.js"),e=n("/lib/build-content/jsx/outer.js");class r{tagName="";rest="";inner="";attributes=[];props=[];selfClosed=!1;constructor(t,e,n){if(">"===t[e+1])this.isComponent=!1,this.tagName="",this.i=e+2;else{for(this.isComponent=/[A-Z]/.test(t[e+1]);e<t.length&&!1!==/[A-Za-z0-9.]/.test(t[++e]);)this.tagName+=t[e];n&&this.attributes.push(["component","${this.name}"]),this.i=s(t[e],e,this,t)}!1===this.selfClosed&&this.getInner(t)}get outer(){return e(this)}getInner(t){var e=`</${this.tagName}>`;let n=0;for(var s="</>"==e?"<>":"<"+this.tagName;this.i<t.length;){if(this.inner+=t[this.i],this.inner.endsWith(s)&&n++,this.inner.endsWith(e)){if(!(0<n)){this.inner=this.inner.slice(0,-e.length).trim();break}n--}this.i++}this.buildInner()}buildInner(){let e="";if(!(this.inner.trim().length<2)&&(this.inner.includes("<")||this.inner.includes("{"))){for(let t=0;t<this.inner.length;t++){var n,s;"<"===this.inner[t]?(n=new r(this.inner,t),e+=n.outer,t=n.i):"{"===this.inner[t]?(n=t,t=o(t,this.inner),s=this.inner.slice(n,t),e+="$"+r.jsxParser(s)):e+=this.inner[t]}this.inner=e}}}return t.exports=r,t.exports})(),e["/lib/build-content/jsx/jsx-parser.js"]=(()=>{var t={exports:{}};let i=n("/lib/build-content/jsx/element.js");function e(e,n){let s="";for(let t=0;t<e.length;t++)if("("===e[t]){var o=t;for(t++;0===e[t].trim().length;)t++;if("<"===e[t]){var r=new i(e,t,n);for(t=r.i,s+="`"+r.outer+"`";")"!==e[t];)t++}else s+=e.slice(o,t+1)}else s+=e[t];return s}return i.jsxParser=e,t.exports=e,t.exports})(),e["/lib/build-content/index.js"]=(()=>{var t={exports:{}};let s=n("/lib/build-content/jsx/jsx-parser.js"),o=n("/lib/build-content/build-component/index.js"),r=n("/lib/build-content/remove-comments.js");return t.exports=function(t,e){var n=e.split("/").pop().replace(/\.js$/,""),e=`context.currentPath = '${e}'; | ||
`+s(r(t),n);return o(e,n)},t.exports})(),e["/lib/build.js"]=(()=>{var t={exports:{}};let c=n("/lib/build-content/index.js"),a=n("/lib/context/context.js");return t.exports=function(t,e="context",n,s){var o,r=new a(n,s);for(o in t.contents)t.contents[o]=c(t.contents[o],o);let i="";var{links:l,styles:u}=r;return{resultFn:t.build({},r,e),context:r,add:i=s&&!n||!s&&n?[...l.map(t=>`<link rel="stylesheet" href="${t}">`),u.length?`<style>${u.join("\n")}</style>`:""].filter(Boolean).join("\n")+"\n":i}},t.exports})(),e["/lib/browser.js"]=(()=>{var t={exports:{}};let r=n("/lib/build.js");return t.exports=async function(t,e="body",n={},s){await(t=new Require(t)).getContent();var{resultFn:t,context:s,add:o}=r(t,s,!0,!1);return s.data=n,e&&(e=document.querySelector(e))&&(e.innerHTML=o+t(n).trim(),s.runActions()),s},t.exports})(),e["/lib/browser.js"]}{var e;let s=new Function("return "+"{}")();(function t(e){for(var n in e)"function"==typeof e[n]&&e[n].name===Obj.recursiveName?e[n]=e[n](s):null!==e[n]&&"object"==typeof e[n]&&t(e[n])})(s),s}try{t({})}catch(n){{var s=n;var i={"/lib/context/component.js":{from:4,to:78},"/lib/context/context.js":{from:79,to:137},"/lib/build-content/remove-comments.js":{from:138,to:154},"/lib/build-content/jsx/breckets.js":{from:155,to:190},"/lib/build-content/build-component/get-function.js":{from:191,to:209},"/lib/build-content/build-component/index.js":{from:210,to:233},"/lib/build-content/jsx/outer.js":{from:234,to:263},"/lib/build-content/jsx/attributes/build-action.js":{from:264,to:276},"/lib/build-content/jsx/attributes/build-prop.js":{from:277,to:297},"/lib/build-content/jsx/attributes/get-attributes.js":{from:298,to:369},"/lib/build-content/jsx/element.js":{from:370,to:442},"/lib/build-content/jsx/jsx-parser.js":{from:443,to:470},"/lib/build-content/index.js":{from:471,to:487},"/lib/build.js":{from:488,to:513},"/lib/browser.js":{from:514,to:537}};var l=537;let[t,...e]=s.stack.split("\n");throw e=e.map(t=>{var e=t.match(/<anonymous>:(\d*):(\d*)\)$/);if(e){let n=Number(e[1]);if(n+1!==l){var s,o,e=Number(e[2]),r=Object.entries(i).filter(([,{from:t,to:e}])=>n>=t&&n<=e);if(0!==r.length)return[r,{from:s,to:o}]=r[0],` at ${t.match(/at\s(.*?)\s/)[1]} ${r} (${n-s-2}:${e})`}}}).filter(Boolean),s.stack=t+"\n"+e.join("\n"),s;return}}})(); | ||
`))},t.exports})(),e["/lib/jsx/outer.js"]=((n={exports:{}}).exports=function(t){let{tagName:e,selfClosed:s,attributes:n,props:r,isComponent:o,rest:i,inner:l}=t,a="";if(o){t=[...r,...n.map(([t,e])=>[t,'"'+e+'"'])].map(([t,e])=>t+":"+e);i&&t.push(i),r="{"+t.join(",")+"}",a="${"+`${e}(${r},\`${l}\`)`+"}"}else{if(""===e)return l||"";r=r.map(([t,e])=>[t,"${"+e+"}"]);t=[...n,...r].map(([t,e])=>e?`${t}="${e.replace(/\"/g,'\\"')}"`:t).join(" ");a=`<${e}${t.length?" "+t:""}>`,s||(a+=l+`</${e}>`)}return a},n.exports),e["/lib/jsx/attributes/build-action.js"]=((n={exports:{}}).exports=function(t,e){var[t,s]=t,t=t.split("on")[1].toLowerCase();e.attributes.push([t,"$"+`{this.addAction('${t}',${s})}`])},n.exports),e["/lib/jsx/attributes/build-prop.js"]=(()=>{var t={exports:{}};let o=["disabled","checked","readonly","required","hidden","autofocus","multiple","selected","controls","loop","muted","open","spellcheck","draggable","contenteditable","novalidate"],i=s("/lib/jsx/attributes/build-action.js");return t.exports=function(s,n,r){if(s){let[t,e]=s;"className"===t&&(t="class"),o.includes(t)?(e&&(t=`\${${e} ? '${t}' : ''}`),r.attributes.push([t])):"props"===n&&t.startsWith("on")?i(s,r):r[n].push([t,e])}},t.exports})(),e["/lib/jsx/attributes/get-attributes.js"]=(()=>{var t={exports:{}};let p=s("/lib/jsx/attributes/build-prop.js");return t.exports=function(t,e,n,r){let o="",i="",l=!0,a=!1,u,s=0;function c(t,e,s="attributes"){for(p(e,s,n),l=!0,a=!1,o="",i="",u=null;0===r[t].trim().length;)t++;return t}for(;">"!==t&&!(e>=r.length);){if(l)if("{"===t){for(;e<r.length&&"}"!==(t=r[++e]);)n.rest+=t;c(e)}else if("="===t||0===t.trim().length)0<o.length&&(" "===t&&"="!==r[e+1]?(n.attributes.push([o]),o=""):(l=!1,a=!0));else{if(">"===r[e+1]){"/"===t?n.selfClosed=!0:""!==o&&n.attributes.push([o+t]),e++;break}o+=t}else a&&(u?"{"===u?(i+=t,"{"===t?s++:"}"===t&&(0<s?s--:e=c(e,[o,i.slice(0,-1)],"props"))):"\\"!==r[e-1]&&t===u?e=c(e,[o,i]):i+=t:/["'`{]/.test(t)?u=t:/[a-zA-Z]/.test(t)&&(""!==o&&n.attributes.push([o]),l=!0,a=!1,o=t));">"===(t=r[++e])&&a&&(i+=t,t=r[++e])}return++e},t.exports})(),e["/lib/jsx/element.js"]=(()=>{var t={exports:{}};let n=s("/lib/jsx/attributes/get-attributes.js"),r=s("/lib/jsx/breckets.js"),e=s("/lib/jsx/outer.js");class o{tagName="";rest="";inner="";attributes=[];props=[];selfClosed=!1;constructor(t,e,s){if(">"===t[e+1])this.isComponent=!1,this.tagName="",this.i=e+2;else{for(this.isComponent=/[A-Z]/.test(t[e+1]);e<t.length&&!1!==/[A-Za-z0-9.]/.test(t[++e]);)this.tagName+=t[e];s&&this.attributes.push(["component","${this.name}"]),this.i=n(t[e],e,this,t)}!1===this.selfClosed&&this.getInner(t)}get outer(){return e(this)}getInner(t){var e=`</${this.tagName}>`;let s=0;for(var n="</>"==e?"<>":"<"+this.tagName;this.i<t.length;){if(this.inner+=t[this.i],this.inner.endsWith(n)&&s++,this.inner.endsWith(e)){if(!(0<s)){this.inner=this.inner.slice(0,-e.length).trim();break}s--}this.i++}this.buildInner()}buildInner(){let e="";if(!(this.inner.trim().length<2)&&(this.inner.includes("<")||this.inner.includes("{"))){for(let t=0;t<this.inner.length;t++){var s,n;"<"===this.inner[t]?(s=new o(this.inner,t),e+=s.outer,t=s.i):"{"===this.inner[t]?(s=t,t=r(t,this.inner),n=this.inner.slice(s,t+1),e+="$"+o.jsxParser(n)):e+=this.inner[t]}this.inner=e}}}return t.exports=o,t.exports})(),e["/lib/jsx/jsx-parser.js"]=(()=>{var t={exports:{}};let i=s("/lib/jsx/element.js");function e(e,s){let n="";for(let t=0;t<e.length;t++)if("("===e[t]){var r=t;for(t++;0===e[t].trim().length;)t++;if("<"===e[t]){var o=new i(e,t,s);for(t=o.i,n+="`"+o.outer+"`";")"!==e[t];)t++}else n+=e.slice(r,t+1)}else n+=e[t];return n}return i.jsxParser=e,t.exports=e,t.exports})(),e["/lib/context/component.js"]=(()=>{var t={exports:{}};class o{static fns={};static components={};static componentsToUpdate={};static genHash(t){t=(new TextEncoder).encode(t);let e=0,s=0;var n,r=[t=>(e+=t,1),t=>(e-=t,0)];for(n of t)s=r[s](n);return e}constructor(t,e={},s){var n,{key:r=""}=e,r=t+r;if(o.components[r])return(n=o.components[r]).init(e,s),n;(o.components[r]=this).mounted=!1,this.name=r,this.selector=`[component=${this.name}]`,this.fn=o.fns[t],this.init()}init(t,e){this.actions=[],this.counter=0,this.props=t,this.inner=e,this.hooks={mount:[()=>this.mounted=!0],unmount:[]}}addAction(t,e){var s=this.name+this.counter++;return this.actions.push({event:t,id:s,fn:e}),s}on(t,e){this.hooks[t]&&this.hooks[t].push(e)}update(t=this.props,e=this.inner){this.props=t,this.inner=e;var s=document.querySelector(this.selector);s&&this.fn&&(t=this.fn(t,e,this),this.hash!==this.oldHash)&&(s.outerHTML=t,o.context.runActions())}genHash(t){var e=o.genHash(t+this.name);return this.oldHash=this.hash,(o.componentsToUpdate[this.name]=this).hash=e,t}}return t.exports=o,t.exports})(),e["/lib/context/context.js"]=(()=>{var t={exports:{}};let l=s("/lib/context/component.js");return t.exports=class{links=[];styles=[];counter=0;constructor(t=!0,e=!1){(l.context=this).browser=t,this.ssr=e}addComponentFn(t,e){l.fns[t]=e}component(t,e,s){return new l(t,e,s)}style(t){this.styles.push(t)}runActions(){for(var t in l.componentsToUpdate){var t=l.componentsToUpdate[t],{actions:s,hooks:r,selector:o}=t;let e=document.querySelector(o),n=e||document;r.mount.forEach(t=>t(e)),s.forEach(({event:t,fn:e,id:s})=>{s=n.querySelector(`[${t}="${s}"]`);s&&("load"===t?e(s):s.addEventListener(t,e))}),t.actions=[],r.mount=[]}for(var e in l.components){var{selector:n,hooks:i}=l.components[e];document.querySelector(n)||(i.unmount.forEach(t=>t()),delete l.components[e])}l.componentsToUpdate={}}link(t){let e=this.currentPath.split("/");e.pop(),t.split("/").forEach(t=>{".."===t?e.pop():"."!==t&&e.push(t)}),this.links.push(e.join("/"))}},t.exports})(),e["/lib/build.js"]=(()=>{var t={exports:{}};let p=s("/lib/context/context.js"),h=s("/lib/jsx/jsx-parser.js"),m=s("/lib/build-component/index.js"),b=s("/lib/utils/remove-comments.js");return t.exports=function(t,e="context",s,n){var r,o=new p(s,n);for(r in t.contents){var i=r.split("/").pop().replace(/\.js$/,""),l=`context.currentPath = '${r}'; | ||
`+h(b(t.contents[r]),i);t.contents[r]=m(l,i)}let a="";var{links:u,styles:c}=o;return{resultFn:t.build({},o,e),context:o,add:a=n&&!s||!n&&s?[...u.map(t=>`<link rel="stylesheet" href="${t}">`),c.length?`<style>${c.join("\n")}</style>`:""].filter(Boolean).join("\n")+"\n":a}},t.exports})(),e["/lib/browser.js"]=(()=>{var t={exports:{}};let o=s("/lib/build.js");return t.exports=async function(t,e={},s={}){var{selector:s="body",contextName:n,version:r}=s;Require.version=r;await(r=new Require(t)).getContent();var{resultFn:t,context:r,add:n}=o(r,n,!0,!1);return r.data=e,s&&(s=document.querySelector(s))&&(t=await t(e),s.innerHTML=n+t.trim(),r.runActions()),r},t.exports})(),e["/lib/browser.js"]}{var e;let n=new Function("return "+"{}")();(function t(e){for(var s in e)"function"==typeof e[s]&&e[s].name===Obj.recursiveName?e[s]=e[s](n):null!==e[s]&&"object"==typeof e[s]&&t(e[s])})(n),n}try{t({})}catch(s){{var n=s;var i={"/lib/utils/remove-comments.js":{from:4,to:20},"/lib/jsx/breckets.js":{from:21,to:56},"/lib/build-component/get-function.js":{from:57,to:75},"/lib/build-component/index.js":{from:76,to:100},"/lib/jsx/outer.js":{from:101,to:131},"/lib/jsx/attributes/build-action.js":{from:132,to:144},"/lib/jsx/attributes/build-prop.js":{from:145,to:164},"/lib/jsx/attributes/get-attributes.js":{from:165,to:241},"/lib/jsx/element.js":{from:242,to:314},"/lib/jsx/jsx-parser.js":{from:315,to:342},"/lib/context/component.js":{from:343,to:416},"/lib/context/context.js":{from:417,to:475},"/lib/build.js":{from:476,to:507},"/lib/browser.js":{from:508,to:534}};var l=534;let[t,...e]=n.stack.split("\n");throw e=e.map(t=>{var e=t.match(/<anonymous>:(\d*):(\d*)\)$/);if(e){let s=Number(e[1]);if(s+1!==l){var n,r,e=Number(e[2]),o=Object.entries(i).filter(([,{from:t,to:e}])=>s>=t&&s<=e);if(0!==o.length)return[o,{from:n,to:r}]=o[0],` at ${t.match(/at\s(.*?)\s/)[1]} ${o} (${s-n-2}:${e})`}}}).filter(Boolean),n.stack=t+"\n"+e.join("\n"),n;return}}})(); |
@@ -7,3 +7,3 @@ try { | ||
} catch (error) {} | ||
const getInsideBreckets = require('../lib/build-content/jsx/breckets'); | ||
const getInsideBreckets = require('../lib/jsx/breckets'); | ||
describe('getInsideBreckets', () => { | ||
@@ -10,0 +10,0 @@ it('should return correct index for simple case', () => { |
@@ -8,3 +8,3 @@ try { | ||
const buildAction = require('../lib/build-content/jsx/attributes/build-action'); | ||
const buildAction = require('../lib/jsx/attributes/build-action'); | ||
const Component = require('../lib/context/component') | ||
@@ -11,0 +11,0 @@ describe('buildAction', () => { |
@@ -8,3 +8,3 @@ try { | ||
const buildProp = require('../lib/build-content/jsx/attributes/build-prop'); | ||
const buildProp = require('../lib/jsx/attributes/build-prop'); | ||
@@ -11,0 +11,0 @@ describe('buildProp', () => { |
@@ -8,4 +8,4 @@ try { | ||
const Element = require('../lib/build-content/jsx/element'); | ||
const jsxParser = require('../lib/build-content/jsx/jsx-parser') | ||
const Element = require('../lib/jsx/element'); | ||
const jsxParser = require('../lib/jsx/jsx-parser') | ||
Element.jsxParser = jsxParser | ||
@@ -12,0 +12,0 @@ |
@@ -8,3 +8,3 @@ try { | ||
const getAttributes = require('../lib/build-content/jsx/attributes/get-attributes'); | ||
const getAttributes = require('../lib/jsx/attributes/get-attributes'); | ||
const Component = require('../lib/context/component') | ||
@@ -25,2 +25,3 @@ | ||
describe('getAttributes', () => { | ||
it('should parse attributes without values', () => { | ||
@@ -27,0 +28,0 @@ const content = '<div checked disabled>'; |
@@ -8,3 +8,3 @@ try { | ||
const getFunction = require('../lib/build-content/build-component/get-function'); | ||
const getFunction = require('../lib/build-component/get-function'); | ||
@@ -11,0 +11,0 @@ describe('getFunction', () => { |
@@ -8,5 +8,26 @@ try { | ||
const jsxParser = require('../lib/build-content/jsx/jsx-parser'); | ||
const jsxParser = require('../lib/jsx/jsx-parser'); | ||
describe('jsxParser', () => { | ||
it('should correctly parse inner with ({expression})', () => { | ||
const content = 'const element = (<div>some ({text} and something) some</div>);'; | ||
const result = jsxParser(content); | ||
const expected = 'const element = `<div>some (${text} and something) some</div>`;' | ||
assert(result === expected) | ||
}); | ||
it('should correctly parse inner with ({expression})', () => { | ||
const content = 'const element = (<div>some ({text}) some</div>);'; | ||
const result = jsxParser(content); | ||
const expected = 'const element = `<div>some (${text}) some</div>`;' | ||
assert(result === expected) | ||
}); | ||
it('should correctly parse element with single attribute', () => { | ||
const content = 'const element = (<div><input type="text" value={`some value with "`} disabled /></div>);'; | ||
const result = jsxParser(content); | ||
const expected = 'const element = `<div><input type="text" disabled value="${`some value with \\"`}"></div>`;' | ||
assert(result === expected) | ||
}); | ||
it('should correctly parse simple JSX expressions', () => { | ||
@@ -13,0 +34,0 @@ const content = 'const element = (<div>Text</div>);'; |
@@ -8,3 +8,3 @@ try { | ||
const getOuter = require('../lib/build-content/jsx/outer'); | ||
const getOuter = require('../lib/jsx/outer'); | ||
@@ -24,2 +24,11 @@ class Element { | ||
describe('getOuter', () => { | ||
it('Should escape " character in value',() => { | ||
const element = new Element(); | ||
element.tagName = 'div'; | ||
element.attributes = [['some',`test with "`]] | ||
element.inner = '' | ||
const result = getOuter(element); | ||
assert.strictEqual(result,'<div some="test with \\""></div>') | ||
}) | ||
it('should correctly generate outer HTML for a simple tag with attributes', () => { | ||
@@ -26,0 +35,0 @@ const element = new Element(); |
@@ -8,3 +8,3 @@ try { | ||
const removeComments = require('../lib/build-content/remove-comments'); | ||
const removeComments = require('../lib/utils/remove-comments'); | ||
@@ -11,0 +11,0 @@ describe('removeComments', () => { |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
130213
2148
152
56
Updatedals-require@^1.6.0