boxwood 0.77.1 → 0.79.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (3) hide show
  1. package/README.md +3 -3
  2. package/index.js +35 -15
  3. package/package.json +4 -1
package/README.md CHANGED
@@ -42,7 +42,7 @@ const { compile } = require("boxwood")
42
42
  const { join } = require("path")
43
43
  // ...
44
44
  const path = join(__dirname, "index.js")
45
- const { template } = await compile(path)
45
+ const { template } = compile(path)
46
46
  // ...
47
47
  const html = template({ foo: "bar" })
48
48
  console.log(html)
@@ -121,13 +121,13 @@ const assert = require("node:assert")
121
121
  const { compile } = require("boxwood")
122
122
 
123
123
  test("banner renders a title", async () => {
124
- const { template } = await compile(__dirname)
124
+ const { template } = compile(__dirname)
125
125
  const html = template({ title: "foo" })
126
126
  assert(html.includes("<h1>foo</h1>"))
127
127
  })
128
128
 
129
129
  test("banner renders an optional description", async () => {
130
- const { template } = await compile(__dirname)
130
+ const { template } = compile(__dirname)
131
131
  const html = template({ title: "foo", description: "bar" })
132
132
  assert(html.includes("<h1>foo</h1>"))
133
133
  assert(html.includes("<p>bar</p>"))
package/index.js CHANGED
@@ -3,14 +3,17 @@ const { readFileSync } = require("fs")
3
3
  const csstree = require("css-tree")
4
4
  const toHash = require("string-hash")
5
5
 
6
- async function compile(path) {
6
+ function compile(path) {
7
7
  const fn = require(path)
8
8
  return {
9
9
  template() {
10
10
  const tree = fn(...arguments)
11
11
  const nodes = {}
12
12
  const styles = []
13
- const scripts = []
13
+ const scripts = {
14
+ head: [],
15
+ body: [],
16
+ }
14
17
  const walk = (node) => {
15
18
  if (!node) {
16
19
  return
@@ -29,19 +32,27 @@ async function compile(path) {
29
32
  node.ignore = true
30
33
  }
31
34
  if (node.name === "script") {
32
- if (node.attributes && node.attributes.src) {
33
- return
34
- }
35
- const js = node.children
35
+ const attributes = node.attributes || {}
36
36
  if (
37
- node.attributes &&
38
- (node.attributes.type === "application/json" ||
39
- node.attributes.type === "application/ld+json")
37
+ attributes.src ||
38
+ ["application/json", "application/ld+json"].includes(
39
+ attributes.type
40
+ )
40
41
  ) {
41
42
  node.ignore = false
43
+ return
42
44
  } else {
43
- if (js && !scripts.includes(js)) {
44
- scripts.push(js)
45
+ const script = node.children
46
+ if (script) {
47
+ if (attributes.target === "head") {
48
+ if (!scripts.head.includes(script)) {
49
+ scripts.head.push(script)
50
+ }
51
+ } else {
52
+ if (!scripts.body.includes(script)) {
53
+ scripts.body.push(script)
54
+ }
55
+ }
45
56
  }
46
57
  node.ignore = true
47
58
  }
@@ -60,12 +71,18 @@ async function compile(path) {
60
71
  children: styles.join(""),
61
72
  })
62
73
  }
74
+ if (scripts.head.length > 0) {
75
+ nodes.head.children.push({
76
+ name: "script",
77
+ children: scripts.head.join(""),
78
+ })
79
+ }
63
80
  }
64
81
  if (nodes.body) {
65
- if (scripts.length > 0) {
82
+ if (scripts.body.length > 0) {
66
83
  nodes.body.children.push({
67
84
  name: "script",
68
- children: scripts.join(""),
85
+ children: scripts.body.join(""),
69
86
  })
70
87
  }
71
88
  }
@@ -317,10 +334,13 @@ js.load = function () {
317
334
  const content = readFileSync(file, "utf8")
318
335
 
319
336
  const options = arguments[arguments.length - 1]
337
+ const attributes = options.target ? { target: options.target } : {}
320
338
  if (options && options.transform) {
321
- return js`${options.transform(content)}`
339
+ return {
340
+ js: tag("script", attributes, options.transform(content)),
341
+ }
322
342
  }
323
- return js`${content}`
343
+ return { js: tag("script", attributes, content) }
324
344
  }
325
345
 
326
346
  const node = (name) => (options, children) => tag(name, options, children)
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "boxwood",
3
- "version": "0.77.1",
3
+ "version": "0.79.0",
4
4
  "description": "Compile HTML templates into JS",
5
5
  "main": "index.js",
6
6
  "scripts": {
@@ -64,5 +64,8 @@
64
64
  "dependencies": {
65
65
  "css-tree": "^2.3.1",
66
66
  "string-hash": "^1.1.3"
67
+ },
68
+ "prettier": {
69
+ "semi": false
67
70
  }
68
71
  }