|
1 | | -import md5 from 'md5' |
2 | 1 | import dust, { compile } from 'dustjs-linkedin' |
3 | | -import { basename, dirname, extname, resolve } from 'path' |
4 | | - |
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 | | -} |
| 2 | +import { dirname, extname, resolve } from 'path' |
| 3 | +import { generateName, hash } from './utils.js' |
18 | 4 |
|
| 5 | +/** |
| 6 | + * Finds all valid partials and replaces paths with respective |
| 7 | + * unique template names |
| 8 | + */ |
19 | 9 | function resolvePartials (source, parentPath) { |
20 | 10 | const partials = [] |
| 11 | + // matches {>"./file.dust" /} |
21 | 12 | const partialRegex = /\{>\s*"([^"]+)"[^\n]*\/\}/g |
22 | 13 | const newSource = source.replace(partialRegex, (tag, relPath) => { |
23 | 14 | if (extname(relPath) === '.dust') { |
24 | | - const fullPath = getFullPath(parentPath, relPath) |
| 15 | + const fullPath = resolve(dirname(parentPath), relPath) |
25 | 16 | const name = generateName(fullPath) |
26 | 17 |
|
27 | 18 | partials.push(relPath) |
|
0 commit comments