在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称(OpenSource Name):choojs/nanohtml开源软件地址(OpenSource Url):https://github.com/choojs/nanohtml开源编程语言(OpenSource Language):JavaScript 100.0%开源软件介绍(OpenSource Introduction):nanohtmlHTML template strings for the Browser with support for Server Side Rendering in Node. Installation$ npm install nanohtml UsageBrowservar html = require('nanohtml')
var el = html`
<body>
<h1>Hello planet</h1>
</body>
`
document.body.appendChild(el) NodeNode doesn't have a DOM available. So in order to render HTML we use string concatenation instead. This has the fun benefit of being quite efficient, which in turn means it's great for server rendering! var html = require('nanohtml')
var el = html`
<body>
<h1>Hello planet</h1>
</body>
`
console.log(el.toString()) Node with custom DOMModules like var JSDOM = require('jsdom').JSDOM
var nanohtml = require('nanohtml/dom')
var jsdom = new JSDOM()
var html = nanohtml(jsdom.window.document)
var el = html`
<body>
<h1>Hello planet</h1>
</body>
`
el.appendChild(html`<p>A paragraph</p>`)
el.outerHTML === '<body><h1>Hello planet</h1><p>A paragraph</p></body>' Interpolating unescaped HTMLBy default all content inside template strings is escaped. This is great for
strings, but not ideal if you want to insert HTML that's been returned from
another function (for example: a markdown renderer). Use var raw = require('nanohtml/raw')
var html = require('nanohtml')
var string = '<h1>This a regular string.</h1>'
var el = html`
<body>
${raw(string)}
</body>
`
document.body.appendChild(el) Attaching event listenersvar html = require('nanohtml')
var el = html`
<body>
<button onclick=${onclick}>
Click Me
</button>
</body>
`
document.body.appendChild(el)
function onclick (e) {
console.log(`${e.target} was clicked`)
} Multiple root elementsIf you have more than one root element they will be combined with a DocumentFragment. var html = require('nanohtml')
var el = html`
<li>Chashu</li>
<li>Nori</li>
`
document.querySelector('ul').appendChild(el) Static optimizationsParsing HTML has significant overhead. Being able to parse HTML statically, ahead of time can speed up rendering to be about twice as fast. BrowserifyFrom the command line$ browserify -t nanohtml index.js > bundle.js Programmaticallyvar browserify = require('browserify')
var nanohtml = require('nanohtml')
var path = require('path')
var b = browserify(path.join(__dirname, 'index.js'))
.transform(nanohtml)
b.bundle().pipe(process.stdout) In package.json{
"name": "my-app",
"private": true,
"browserify": {
"transform": [
"nanohtml"
]
},
"dependencies": {
"nanohtml": "^1.0.0"
}
} BundlingWebpackAt the time of writing there's no Webpack loader yet. We'd love a contribution! Babel / ParcelAdd nanohtml to your Without options: {
"plugins": [
"nanohtml"
]
} With options: {
"plugins": [
["nanohtml", {
"useImport": true
}]
]
} Options
RollupUse the @rollup/plugin-commonjs plugin with @rollup/plugin-node-resolve. Explicitly import the browser or server entrypoint in your application. E.g.:
AttributionsShout out to Shama and Shuhei for their contributions to Bel, yo-yoify and pelo. This module is based on their work, and wouldn't have been possible otherwise! See AlsoLicense |
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论