Skip to content

Commit

Permalink
+ load(import.meta, { ...options } = {}, { ...data } = {})
Browse files Browse the repository at this point in the history
  • Loading branch information
Lcfvs committed Sep 13, 2021
1 parent 4863f72 commit 58ce89e
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 5 deletions.
9 changes: 9 additions & 0 deletions lib/backend.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import jsdom from 'jsdom'
import { readFile } from 'fs/promises'
import { renderer, serializer } from './dom.js'

const symbol = Symbol('source')
Expand All @@ -12,6 +13,14 @@ export const template = (source, { ...data } = {}) => ({
...data
})

export const load = async ({ url }, { ...options } = {}, { ...data } = {}) => {
const parts = url.split('.')

parts.splice(-1, 1, 'html')

return template((await readFile(parts.join('.'), options)).toString(), data)
}

const context = {
document,
parser: new DOMParser(),
Expand Down
10 changes: 10 additions & 0 deletions lib/frontend.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
import { readFile } from 'fs/promises'
import { renderer, serializer } from './dom.js'

const symbol = Symbol('source')
const read = response => response.text()

export const source = symbol

Expand All @@ -9,6 +11,14 @@ export const template = (source, { ...data } = {}) => ({
...data
})

export const load = async ({ url }, { ...options } = {}, { ...data } = {}) => {
const parts = url.split('.')

parts.splice(-1, 1, 'html')

return template(await fetch(parts.join('.'), options).then(read), data)
}

const { document, trustedTypes, DOMParser, XMLSerializer } = window

const context = {
Expand Down
9 changes: 9 additions & 0 deletions lib/worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ const {
} = Object

const symbol = Symbol('source')
const read = response => response.text()

export const source = symbol

Expand All @@ -13,6 +14,14 @@ export const template = (source, { ...data } = {}) => ({
...data
})

export const load = async ({ url }, { ...options } = {}, { ...data } = {}) => {
const parts = url.split('.')

parts.splice(-1, 1, 'html')

return template(await fetch(parts.join('.'), options).then(read), data)
}

const symbols = {
source
}
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@lcf.vs/dom-engine",
"version": "5.3.2",
"version": "5.4.0",
"description": "A composable DOM based template engine",
"type": "module",
"exports": {
Expand Down
31 changes: 29 additions & 2 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -147,18 +147,27 @@ const html = await serialize(p)
```js
import { template, source } from '@lcf.vs/dom-engine/lib/frontend.js'

const template = template(html, { ...fields } = {})
const tpl = template(html, { ...fields } = {})

// or

const template = {
const tpl = {
[source]: html,
...fields
}
```

### Back-End

```js
import { load } from '@lcf.vs/dom-engine/lib/backend.js'

// loads a file with the same name than the current module,
// but with a .html extension instead of the current one
const tpl = await load(import.meta, { ...options } = {}, { ...data } = {})
```
Where the options are the **async** `readFile()` ones
```js
import { serialize } from '@lcf.vs/dom-engine/lib/backend.js'

Expand All @@ -173,6 +182,15 @@ const htmlNode = render(template)
### Front-End
```js
import { load } from '@lcf.vs/dom-engine/lib/frontend.js'

// loads a file with the same name than the current module,
// but with a .html extension instead of the current one
const tpl = await load(import.meta, { ...options } = {}, { ...data } = {})
```
Where the options are the `fetch()` ones
```js
import { serialize } from '@lcf.vs/dom-engine/lib/frontend.js'

Expand All @@ -199,6 +217,15 @@ import '@lcf.vs/dom-engine/lib/worker-client.js'
#### Into the `ServiceWorker` script
```js
import { load } from '@lcf.vs/dom-engine/lib/frontend.js'

// loads a file with the same name than the current module,
// but with a .html extension instead of the current one
const tpl = await load(import.meta, { ...options } = {}, { ...data } = {})
```
Where the options are the `fetch()` ones
```js
import { serialize, source } from '@lcf.vs/dom-engine/lib/worker.js'

Expand Down

0 comments on commit 58ce89e

Please sign in to comment.