Comparing version 2.0.0-build-2 to 2.0.0-build-3
@@ -27,5 +27,6 @@ #!/usr/bin/env node | ||
: path.join(opts.base, path.dirname(file)); | ||
return function readFile(name) { | ||
return function readFile(names) { | ||
if (Array.isArray(names)) names = path.join.apply(path, names); | ||
return Promise.resolve( | ||
fs.readFileSync(path.isAbsolute(name) ? name : path.join(base, name), { encoding: 'utf8' }) | ||
fs.readFileSync(path.isAbsolute(names) ? names : path.join(base, names), { encoding: 'utf8' }) | ||
); | ||
@@ -32,0 +33,0 @@ }; |
@@ -58,6 +58,61 @@ /* eslint-env node */ | ||
}); | ||
const tpl = Ractive.parse(string, rootOpts); | ||
return readTemplate(string, rootOpts, mkReadFile(readFile, ''), true).then(res => { | ||
return Promise.all([ | ||
compileCss(res.style, opts), | ||
reducePromiseFunctions(opts.partialProcessors, res.partials), | ||
reducePromiseFunctions(opts.templateProcessors, res.template), | ||
reducePromiseFunctions(opts.scriptProcessors, res.script) | ||
]).then(list => { | ||
const tpl = list[2]; | ||
const partials = list[1]; | ||
const style = list[0]; | ||
for (const k in tpl.p || {}) { | ||
if (!partials[k]) partials[k] = tpl.p[k]; | ||
} | ||
tpl.p = partials; | ||
let script = list[3]; | ||
script = dedent(script.join('')); | ||
if (!script && opts.autoExport) | ||
script = `${tpl ? `export const template = $TEMPLATE;\n` : ''}${ | ||
style.length ? `export const css = $CSS;\n` : '' | ||
}`; | ||
script = script.replace(/\$TEMPLATE/g, stringify(tpl, opts)); | ||
script = script.replace( | ||
/\$TEMPLATE_ONLY/g, | ||
stringify({ v: tpl.v, e: tpl.e, t: tpl.t }, opts) | ||
); | ||
script = script.replace(/\$CSS/g, style); | ||
script = script.replace(/\$PARTIALS\['([-a-zA-Z0-9_\/]+)'\]/g, (_m, n) => | ||
stringify({ v: tpl.v, t: partials[n] || '' }, opts) | ||
); | ||
script = script.replace(/\$PARTIALS/g, stringify(partials || {}, opts)); | ||
return script; | ||
}); | ||
}); | ||
} | ||
function readPartial(id, str, opts, readFile, style, partials, exprs) { | ||
return readTemplate(str, opts, readFile, false).then(res => { | ||
style.push.apply(style, res.style); | ||
if (!partials[id]) partials[id] = res.template.t; | ||
for (const k in res.partials) { | ||
if (res.partials[k] && !partials[k]) partials[k] = res.partials[k]; | ||
} | ||
for (const k in res.exprs || {}) { | ||
exprs[k] = res.exprs[k]; | ||
} | ||
}); | ||
} | ||
function readTemplate(string, opts, readFile, allowScript) { | ||
const tpl = Ractive.parse(string || '', opts); | ||
const partials = {}; | ||
let script = []; | ||
const script = []; | ||
const style = []; | ||
const exprs = tpl.e || {}; | ||
@@ -79,5 +134,9 @@ const promises = []; | ||
if (!src) { | ||
partials[id] = item.f[0]; | ||
promises.push(readPartial(id, item.f[0], opts, readFile, style, partials, exprs)); | ||
} else { | ||
promises.push(readFile(src).then(str => (partials[id] = str))); | ||
promises.push( | ||
readFile(src).then(str => | ||
readPartial(id, str, opts, readFile.extend(src), style, partials, exprs) | ||
) | ||
); | ||
} | ||
@@ -98,2 +157,7 @@ } else if (!type || type === 'text/javascript' || type === 'application/javascript') { | ||
} else { | ||
if (!allowScript) { | ||
i = drop(i, tpl.t); | ||
// TODO: warn | ||
continue; | ||
} | ||
script.unshift(item.f); | ||
@@ -105,2 +169,7 @@ } | ||
} else { | ||
if (!allowScript) { | ||
i = drop(i, tpl.t); | ||
// TODO: warn | ||
continue; | ||
} | ||
script.unshift(`script!${src}`); | ||
@@ -120,5 +189,11 @@ promises.push( | ||
if (src) { | ||
promises.push(readFile(src).then(str => !partials[id] && (partials[id] = str))); | ||
promises.push( | ||
readFile(src).then(str => | ||
readPartial(id, str, opts, readFile.extend(src), style, partials, exprs) | ||
) | ||
); | ||
} else { | ||
partials[id] = item.f ? item.f[0] : ''; | ||
promises.push( | ||
readPartial(id, item.f ? item.f[0] : '', opts, readFile, style, partials, exprs) | ||
); | ||
} | ||
@@ -160,64 +235,6 @@ } | ||
return Promise.all(promises).then(() => { | ||
script = dedent(script.join('')); | ||
if (!script && opts.autoExport) | ||
script = `${tpl ? `export const template = $TEMPLATE;\n` : ''}${ | ||
style.length ? `export const css = $CSS;\n` : '' | ||
}`; | ||
for (const k in partials) { | ||
if (!tpl.p) tpl.p = {}; | ||
// just in case, don't overwrite any existing partial and skip empty partials | ||
if (tpl.p[k] || !partials[k]) continue; | ||
const t = Ractive.parse(partials[k], opts); | ||
// extract any styles | ||
i = t.t.length; | ||
while (i--) { | ||
if (t.e === 'style') { | ||
const item = t.e[i]; | ||
const rel = getAttr('rel', item); | ||
if (rel === 'ractive') { | ||
style.unshift({ type: 'tpl', body: item.f[0] }); | ||
} else { | ||
style.unshift({ type: 'css', body: item.f[0] || '' }); | ||
} | ||
i = drop(i, tpl.t); | ||
} | ||
} | ||
// copy any expressions | ||
if (t.e) { | ||
if (!tpl.e) tpl.e = {}; | ||
for (const e in t.e) tpl.e[e] = t.e[e]; | ||
} | ||
// copy any partials | ||
if (t.p) { | ||
for (const p in t.p) { | ||
if (!tpl.p[p]) tpl.p[p] = t.p[p]; | ||
} | ||
} | ||
tpl.p[k] = t.t; | ||
for (const k in tpl.p || {}) { | ||
partials[k] = tpl.p[k]; | ||
} | ||
return Promise.all([ | ||
compileCss(style, opts), | ||
reducePromiseFunctions(opts.partialProcessors, partials), | ||
reducePromiseFunctions(opts.templateProcessors, tpl) | ||
]).then(list => { | ||
script = script.replace(/\$TEMPLATE/g, stringify(list[2], opts)); | ||
script = script.replace(/\$CSS/g, list[0]); | ||
script = script.replace(/\$PARTIALS\['([-a-zA-Z0-9_\/]+)'\]/g, (m, n) => | ||
stringify({ v: list[2].v, t: list[2].p[n] || '' }, opts) | ||
); | ||
script = script.replace(/\$PARTIALS/g, stringify(list[2].p || {}, opts)); | ||
return reducePromiseFunctions(opts.scriptProcessors, script); | ||
}); | ||
return { style, script, template: tpl, partials, exprs }; | ||
}); | ||
@@ -227,3 +244,3 @@ } | ||
function dedent(string) { | ||
const lines = string.split(/\r\n|\r|\n/); | ||
const lines = (string || '').split(/\r\n|\r|\n/); | ||
let strip = /^\s*/.exec(lines[lines.length - 1]); | ||
@@ -299,2 +316,24 @@ if (!strip) return string; | ||
function dirname(str) { | ||
const parts = str.split('/'); | ||
parts.pop(); | ||
return parts.join('/'); | ||
} | ||
function mkReadFile(read, path) { | ||
if (typeof path === 'string') path = dirname(path); | ||
const p = Array.isArray(path) ? path : [path]; | ||
const res = function readFile(file) { | ||
if (file[0] === '/') return read(file); | ||
else { | ||
return read(p.concat(file)); | ||
} | ||
}; | ||
res.extend = function (path) { | ||
if (path[0] === '/') return mkReadFile(read, dirname(path)); | ||
else return mkReadFile(read, p.concat(dirname(path))); | ||
}; | ||
return res; | ||
} | ||
module.exports = { help, build }; |
{ | ||
"name": "ractive", | ||
"description": "Next-generation DOM manipulation", | ||
"version": "2.0.0-build-2", | ||
"version": "2.0.0-build-3", | ||
"homepage": "https://ractive.js.org", | ||
@@ -6,0 +6,0 @@ "license": "MIT", |
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is too big to display
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
2989643
66245