boxwood 0.77.1 → 0.78.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 (2) hide show
  1. package/index.js +34 -14
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -10,7 +10,10 @@ async function compile(path) {
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.78.0",
4
4
  "description": "Compile HTML templates into JS",
5
5
  "main": "index.js",
6
6
  "scripts": {