Skip to content

Commit 3068de1

Browse files
committed
adding unique template naming and resolution for partials
1 parent f68d878 commit 3068de1

File tree

4 files changed

+70
-7
lines changed

4 files changed

+70
-7
lines changed

package.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,11 @@
1919
"peerDependencies": {
2020
"dustjs-linkedin": "^2.7.5"
2121
},
22+
"dependencies": {
23+
"md5": "^2.2.1"
24+
},
2225
"devDependencies": {
26+
"dustjs-linkedin": "^2.7.5",
2327
"rollup": "^0.41.6",
2428
"rollup-watch": "^3.2.2"
2529
}

rollup.config.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ export default {
44
format: 'cjs',
55
external: [
66
'dustjs-linkedin',
7+
'md5',
78
'path'
89
]
910
}

src/index.js

Lines changed: 48 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,59 @@
1+
import md5 from 'md5'
12
import { compile } from 'dustjs-linkedin'
2-
import { extname } from 'path'
3+
import { basename, dirname, extname, resolve } from 'path'
34

5+
const { floor, random } = Math
6+
const token = floor(random() * 10000000)
7+
8+
function getFullPath (from, to) {
9+
return resolve(dirname(from), to)
10+
}
11+
12+
function generateName (path) {
13+
const name = basename(path, '.dust')
14+
const hash = md5(token + path)
15+
16+
return `${name}-${hash}`
17+
}
18+
19+
function resolvePartials (source, parentPath) {
20+
const partials = []
21+
const partialRegex = /\{>\s*"([^"]+)" *\/\}/g
22+
const newSource = source.replace(partialRegex, (tag, relPath) => {
23+
if (extname(relPath) === '.dust') {
24+
const fullPath = getFullPath(parentPath, relPath)
25+
const name = generateName(fullPath)
26+
27+
partials.push(relPath)
28+
return tag.replace(relPath, name)
29+
} else {
30+
return tag
31+
}
32+
})
33+
34+
return {
35+
newSource,
36+
partials
37+
}
38+
}
39+
40+
// TODO: options
441
export default function dustjs () {
542
return {
643
name: 'dustjs',
744
transform (source, path) {
845
if (extname(path) === '.dust') {
9-
const template = compile(source)
46+
const { newSource, partials } = resolvePartials(source, path)
47+
const name = generateName(path)
48+
const template = compile(newSource, name)
49+
let compiled = `import dust from 'dustjs-linkedin'\n`
50+
51+
partials.forEach(relPath => {
52+
compiled += `import '${relPath}'\n`
53+
})
1054

11-
return `
12-
import dust from 'dustjs-linkedin'
13-
export default ${template}
14-
`
55+
compiled += `export default ${template}`
56+
return compiled
1557
}
1658
}
1759
}

yarn.lock

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ caseless@~0.12.0:
130130
version "0.12.0"
131131
resolved "https://registry.yarnpkg.com/caseless/-/caseless-0.12.0.tgz#1b681c21ff84033c826543090689420d187151dc"
132132

133+
charenc@~0.0.1:
134+
version "0.0.2"
135+
resolved "https://registry.yarnpkg.com/charenc/-/charenc-0.0.2.tgz#c0a1d2f3a7092e03774bfa83f14c0fc5790a8667"
136+
133137
chokidar@~1.5.1:
134138
version "1.5.2"
135139
resolved "https://registry.yarnpkg.com/chokidar/-/chokidar-1.5.2.tgz#293e728640cc93dd8277424334b3c6d4ad3a348a"
@@ -178,6 +182,10 @@ core-util-is@~1.0.0:
178182
version "1.0.2"
179183
resolved "https://registry.yarnpkg.com/core-util-is/-/core-util-is-1.0.2.tgz#b5fd54220aa2bc5ab57aab7140c940754503c1a7"
180184

185+
crypt@~0.0.1:
186+
version "0.0.2"
187+
resolved "https://registry.yarnpkg.com/crypt/-/crypt-0.0.2.tgz#88d7ff7ec0dfb86f713dc87bbb42d044d3e6c41b"
188+
181189
cryptiles@2.x.x:
182190
version "2.0.5"
183191
resolved "https://registry.yarnpkg.com/cryptiles/-/cryptiles-2.0.5.tgz#3bdfecdc608147c1c67202fa291e7dca59eaa3b8"
@@ -419,7 +427,7 @@ is-binary-path@^1.0.0:
419427
dependencies:
420428
binary-extensions "^1.0.0"
421429

422-
is-buffer@^1.0.2:
430+
is-buffer@^1.0.2, is-buffer@~1.1.1:
423431
version "1.1.5"
424432
resolved "https://registry.yarnpkg.com/is-buffer/-/is-buffer-1.1.5.tgz#1f3b26ef613b214b88cbca23cc6c01d87961eecc"
425433

@@ -528,6 +536,14 @@ kind-of@^3.0.2:
528536
dependencies:
529537
is-buffer "^1.0.2"
530538

539+
md5@^2.2.1:
540+
version "2.2.1"
541+
resolved "https://registry.yarnpkg.com/md5/-/md5-2.2.1.tgz#53ab38d5fe3c8891ba465329ea23fac0540126f9"
542+
dependencies:
543+
charenc "~0.0.1"
544+
crypt "~0.0.1"
545+
is-buffer "~1.1.1"
546+
531547
micromatch@^2.1.5:
532548
version "2.3.11"
533549
resolved "https://registry.yarnpkg.com/micromatch/-/micromatch-2.3.11.tgz#86677c97d1720b363431d04d0d15293bd38c1565"

0 commit comments

Comments
 (0)