riot-compiler
Advanced tools
Comparing version 2.3.0 to 2.3.1
@@ -1,2 +0,2 @@ | ||
/* riot-compiler 2.3.0, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
/* riot-compiler 2.3.1, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
;(function (root, factory) { | ||
@@ -24,3 +24,3 @@ | ||
function _try(name, req) { | ||
function _try(name, req) { //eslint-disable-line complexity | ||
@@ -39,4 +39,5 @@ function fn(r) { | ||
case 'es6': | ||
req = 'babel' | ||
break | ||
/* istanbul ignore next */ | ||
return fn('babel') || fn('babel-core') | ||
/* istanbul ignore next */ | ||
case 'babel': | ||
@@ -55,2 +56,7 @@ req = 'babel-core' | ||
break | ||
/* istanbul ignore next */ | ||
case 'scss': | ||
case 'sass': | ||
req = 'node-sass' | ||
break | ||
default: | ||
@@ -68,4 +74,4 @@ if (!req) req = name | ||
var _html = { | ||
jade: function (html) { | ||
return _req('jade').render(html, {pretty: true, doctype: 'html'}) | ||
jade: function (html, opts) { | ||
return _req('jade').render(html, extend({pretty: true, doctype: 'html'}, opts)) | ||
} | ||
@@ -75,3 +81,37 @@ } | ||
var _css = { | ||
stylus: function (tag, css) { | ||
sass: function(tag, css, opts) { | ||
var sass = _req('sass') | ||
return sass.renderSync(extend({ | ||
data: css, | ||
indentedSyntax: true, | ||
omitSourceMapUrl: true, | ||
outputStyle: 'compact' | ||
}, opts)).css + '' | ||
}, | ||
scss: function(tag, css, opts) { | ||
var sass = _req('sass') | ||
return sass.renderSync(extend({ | ||
data: css, | ||
indentedSyntax: false, | ||
omitSourceMapUrl: true, | ||
outputStyle: 'compact' | ||
}, opts)).css + '' | ||
}, | ||
less: function(tag, css, opts) { | ||
var less = _req('less'), | ||
ret | ||
less.render(css, extend({ | ||
sync: true, | ||
compress: true | ||
}, opts), function (err, result) { | ||
// istanbul ignore next | ||
if (err) throw err | ||
ret = result.css | ||
}) | ||
return ret | ||
}, | ||
stylus: function (tag, css, opts) { | ||
var | ||
@@ -86,23 +126,26 @@ stylus = _req('stylus'), nib = _req('nib') | ||
var _js = { | ||
none: function (js) { | ||
none: function (js, opts) { | ||
return js | ||
}, | ||
livescript: function (js) { | ||
return _req('livescript').compile(js, {bare: true, header: false}) | ||
livescript: function (js, opts) { | ||
return _req('livescript').compile(js, extend({bare: true, header: false}, opts)) | ||
}, | ||
typescript: function (js) { | ||
return _req('typescript')(js).replace(/\r\n?/g, '\n') | ||
typescript: function (js, opts) { | ||
return _req('typescript')(js, opts).replace(/\r\n?/g, '\n') | ||
}, | ||
es6: function (js) { | ||
return _req('es6').transform(js, { | ||
blacklist: ['useStrict', 'react'], sourceMaps: false, comments: false | ||
}).code | ||
es6: /* istanbul ignore next */ function (js, opts) { | ||
return _req('es6').transform(js, extend({ | ||
blacklist: ['useStrict', 'strict', 'react'], sourceMaps: false, comments: false | ||
}, opts)).code | ||
}, | ||
babel: function (js) { | ||
return _req('babel').transform(js, { | ||
presets: ['es2015'], ast: false, sourceMaps: false, comments: false | ||
}).code | ||
babel: function (js, opts) { | ||
js = 'function __parser_babel_wrapper__(){' + js + '}' | ||
return _req('babel').transform(js, | ||
extend({ | ||
presets: ['es2015'] | ||
}, opts) | ||
).code.replace(/["']use strict["'];[\r\n]+/, '').slice(38, -2) | ||
}, | ||
coffee: function (js) { | ||
return _req('coffee').compile(js, {bare: true}) | ||
coffee: function (js, opts) { | ||
return _req('coffee').compile(js, extend({bare: true}, opts)) | ||
} | ||
@@ -166,2 +209,12 @@ } | ||
function extend(obj, props) { | ||
for (var prop in props) { | ||
/* istanbul ignore next */ | ||
if (props.hasOwnProperty(prop)) { | ||
obj[prop] = props[prop] | ||
} | ||
} | ||
return obj | ||
} | ||
function parseAttrs(str) { | ||
@@ -217,10 +270,9 @@ var | ||
expr = list[i] | ||
if (expr[0] === '^') { | ||
if (expr[0] === '^') | ||
expr = expr.slice(1) | ||
} | ||
else if (jsfn) { | ||
expr = jsfn(expr, opts).replace(/[\r\n]+/g, ' ').trim() | ||
if (expr.slice(-1) === ';') expr = expr.slice(0, -1) | ||
expr = jsfn(expr, opts) | ||
if (/;\s*$/.test(expr)) expr = expr.slice(0, expr.search(/;\s*$/)) | ||
} | ||
list[i] = '\u0001' + (pcex.push(expr.trim()) - 1) + _bp[1] | ||
list[i] = '\u0001' + (pcex.push(expr.replace(/[\r\n]+/g, ' ').trim()) - 1) + _bp[1] | ||
} | ||
@@ -322,3 +374,3 @@ html = list.join('') | ||
function compileJS(js, opts, type) { | ||
function compileJS(js, opts, type, parserOpts) { | ||
if (!type) type = opts.type | ||
@@ -330,3 +382,3 @@ | ||
return parser(js).replace(TRIM_TRAIL, '') | ||
return parser(js, parserOpts).replace(TRIM_TRAIL, '') | ||
} | ||
@@ -359,3 +411,3 @@ | ||
function compileCSS(style, tag, type, scoped) { | ||
function compileCSS(style, tag, type, scoped, opts) { | ||
@@ -367,3 +419,3 @@ if (type) { | ||
else if (parsers.css[type]) { | ||
style = parsers.css[type](tag, style) | ||
style = parsers.css[type](tag, style, opts) | ||
} | ||
@@ -391,39 +443,51 @@ else if (type !== 'css') { | ||
function getAttr(str, name) { | ||
if (str) { | ||
var | ||
re = _regEx(TYPE_ATTR.source.replace('type', name), 'i'), | ||
match = str && str.match(re) | ||
/* istanbul ignore next */ | ||
str = match && (match[1] || match[2]) | ||
} | ||
return str || '' | ||
} | ||
function getParserOptions(attrs) { | ||
var opts = getAttr(attrs, 'options') | ||
/* istanbul ignore next */ | ||
if (opts) opts = JSON.parse(parserOpts) | ||
return opts | ||
} | ||
function getCode(code, opts, attrs) { | ||
var type = getType(attrs) | ||
var type = getType(attrs), | ||
parserOpts = getParserOptions(attrs) | ||
return compileJS(code, opts, type) | ||
return compileJS(code, opts, type, parserOpts) | ||
} | ||
var END_TAGS = /\/>\n|<(?:\/[\w\-]+\s*|[\w\-]+(?:\s+(?:[-\w:\xA0-\xFF][\S\s]*?)?)?)>\n/g | ||
var END_TAGS = /\/>\n|^<(?:\/[\w\-]+\s*|[\w\-]+(?:\s+(?:[-\w:\xA0-\xFF][\S\s]*?)?)?)>\n/ | ||
function splitBlocks(str) { | ||
var | ||
i, k, js = '', len = str.length | ||
var k, m | ||
k = str.indexOf('<') | ||
if (k < 0 || (i = str.lastIndexOf('>\n')) < 0 || k > i) | ||
return ['', str] | ||
/* istanbul ignore next: this if() can't be true, but just in case... */ | ||
if (str[str.length - 1] === '>') | ||
return [str, ''] | ||
i += 2 | ||
js = str.slice(i) | ||
str = str.slice(0, i) | ||
if (str[i - 3] !== '/') { | ||
if (str.match(END_TAGS)) { | ||
var s = RegExp.rightContext | ||
if (s) { | ||
js = s + js | ||
str = str.slice(0, len - js.length) | ||
} | ||
k = str.lastIndexOf('<') | ||
while (~k) { | ||
if (m = str.slice(k).match(END_TAGS)) { | ||
k += m.index + m[0].length | ||
return [str.slice(0, k), str.slice(k)] | ||
} | ||
else { | ||
js = str + js | ||
str = '' | ||
} | ||
k = str.lastIndexOf('<', k -1) | ||
} | ||
return [str, js] | ||
return ['', str] | ||
} | ||
function compileTemplate(lang, html) { | ||
function compileTemplate(lang, html, opts) { | ||
var parser = parsers.html[lang] | ||
@@ -434,3 +498,3 @@ | ||
return parser(html) | ||
return parser(html, opts) | ||
} | ||
@@ -451,3 +515,3 @@ | ||
if (opts.template) | ||
src = compileTemplate(opts.template, src) | ||
src = compileTemplate(opts.template, src, opts.templateOptions) | ||
@@ -482,3 +546,3 @@ label = url ? '//src: ' + path.relative('.', url) + '\n' : '' | ||
styles += (styles ? ' ' : '') + | ||
compileCSS(_style, tagName, getType(_attrs), scoped) | ||
compileCSS(_style, tagName, getType(_attrs), scoped, getParserOptions(_attrs)) | ||
return '' | ||
@@ -485,0 +549,0 @@ }) |
@@ -34,4 +34,4 @@ | ||
var _html = { | ||
jade: function (html) { | ||
return _req('jade').render(html, {pretty: true, doctype: 'html'}) | ||
jade: function (html, opts) { | ||
return _req('jade').render(html, extend({pretty: true, doctype: 'html'}, opts)) | ||
} | ||
@@ -41,3 +41,3 @@ } | ||
var _css = { | ||
stylus: function (tag, css) { | ||
stylus: function (tag, css, opts) { | ||
var | ||
@@ -52,23 +52,26 @@ stylus = _req('stylus'), nib = _req('nib') | ||
var _js = { | ||
none: function (js) { | ||
none: function (js, opts) { | ||
return js | ||
}, | ||
livescript: function (js) { | ||
return _req('livescript').compile(js, {bare: true, header: false}) | ||
livescript: function (js, opts) { | ||
return _req('livescript').compile(js, extend({bare: true, header: false}, opts)) | ||
}, | ||
typescript: function (js) { | ||
return _req('typescript')(js).replace(/\r\n?/g, '\n') | ||
typescript: function (js, opts) { | ||
return _req('typescript')(js, opts).replace(/\r\n?/g, '\n') | ||
}, | ||
es6: function (js) { | ||
return _req('es6').transform(js, { | ||
blacklist: ['useStrict', 'react'], sourceMaps: false, comments: false | ||
}).code | ||
es6: /* istanbul ignore next */ function (js, opts) { | ||
return _req('es6').transform(js, extend({ | ||
blacklist: ['useStrict', 'strict', 'react'], sourceMaps: false, comments: false | ||
}, opts)).code | ||
}, | ||
babel: function (js) { | ||
return _req('babel').transform(js, { | ||
presets: ['es2015'], ast: false, sourceMaps: false, comments: false | ||
}).code | ||
babel: function (js, opts) { | ||
js = 'function __parser_babel_wrapper__(){' + js + '}' | ||
return _req('babel').transform(js, | ||
extend({ | ||
presets: ['es2015'] | ||
}, opts) | ||
).code.replace(/["']use strict["'];[\r\n]+/, '').slice(38, -2) | ||
}, | ||
coffee: function (js) { | ||
return _req('coffee').compile(js, {bare: true}) | ||
coffee: function (js, opts) { | ||
return _req('coffee').compile(js, extend({bare: true}, opts)) | ||
} | ||
@@ -133,2 +136,12 @@ } | ||
function extend(obj, props) { | ||
for (var prop in props) { | ||
/* istanbul ignore next */ | ||
if (props.hasOwnProperty(prop)) { | ||
obj[prop] = props[prop] | ||
} | ||
} | ||
return obj | ||
} | ||
function parseAttrs(str) { | ||
@@ -184,10 +197,9 @@ var | ||
expr = list[i] | ||
if (expr[0] === '^') { | ||
if (expr[0] === '^') | ||
expr = expr.slice(1) | ||
} | ||
else if (jsfn) { | ||
expr = jsfn(expr, opts).replace(/[\r\n]+/g, ' ').trim() | ||
if (expr.slice(-1) === ';') expr = expr.slice(0, -1) | ||
expr = jsfn(expr, opts) | ||
if (/;\s*$/.test(expr)) expr = expr.slice(0, expr.search(/;\s*$/)) | ||
} | ||
list[i] = '\u0001' + (pcex.push(expr.trim()) - 1) + _bp[1] | ||
list[i] = '\u0001' + (pcex.push(expr.replace(/[\r\n]+/g, ' ').trim()) - 1) + _bp[1] | ||
} | ||
@@ -289,3 +301,3 @@ html = list.join('') | ||
function compileJS(js, opts, type) { | ||
function compileJS(js, opts, type, parserOpts) { | ||
if (!type) type = opts.type | ||
@@ -297,3 +309,3 @@ | ||
return parser(js).replace(TRIM_TRAIL, '') | ||
return parser(js, parserOpts).replace(TRIM_TRAIL, '') | ||
} | ||
@@ -326,3 +338,3 @@ | ||
function compileCSS(style, tag, type, scoped) { | ||
function compileCSS(style, tag, type, scoped, opts) { | ||
@@ -334,3 +346,3 @@ if (type) { | ||
else if (parsers.css[type]) { | ||
style = parsers.css[type](tag, style) | ||
style = parsers.css[type](tag, style, opts) | ||
} | ||
@@ -358,39 +370,51 @@ else if (type !== 'css') { | ||
function getAttr(str, name) { | ||
if (str) { | ||
var | ||
re = _regEx(TYPE_ATTR.source.replace('type', name), 'i'), | ||
match = str && str.match(re) | ||
/* istanbul ignore next */ | ||
str = match && (match[1] || match[2]) | ||
} | ||
return str || '' | ||
} | ||
function getParserOptions(attrs) { | ||
var opts = getAttr(attrs, 'options') | ||
/* istanbul ignore next */ | ||
if (opts) opts = JSON.parse(parserOpts) | ||
return opts | ||
} | ||
function getCode(code, opts, attrs) { | ||
var type = getType(attrs) | ||
var type = getType(attrs), | ||
parserOpts = getParserOptions(attrs) | ||
return compileJS(code, opts, type) | ||
return compileJS(code, opts, type, parserOpts) | ||
} | ||
var END_TAGS = /\/>\n|<(?:\/[\w\-]+\s*|[\w\-]+(?:\s+(?:[-\w:\xA0-\xFF][\S\s]*?)?)?)>\n/g | ||
var END_TAGS = /\/>\n|^<(?:\/[\w\-]+\s*|[\w\-]+(?:\s+(?:[-\w:\xA0-\xFF][\S\s]*?)?)?)>\n/ | ||
function splitBlocks(str) { | ||
var | ||
i, k, js = '', len = str.length | ||
var k, m | ||
k = str.indexOf('<') | ||
if (k < 0 || (i = str.lastIndexOf('>\n')) < 0 || k > i) | ||
return ['', str] | ||
/* istanbul ignore next: this if() can't be true, but just in case... */ | ||
if (str[str.length - 1] === '>') | ||
return [str, ''] | ||
i += 2 | ||
js = str.slice(i) | ||
str = str.slice(0, i) | ||
if (str[i - 3] !== '/') { | ||
if (str.match(END_TAGS)) { | ||
var s = RegExp.rightContext | ||
if (s) { | ||
js = s + js | ||
str = str.slice(0, len - js.length) | ||
} | ||
k = str.lastIndexOf('<') | ||
while (~k) { | ||
if (m = str.slice(k).match(END_TAGS)) { | ||
k += m.index + m[0].length | ||
return [str.slice(0, k), str.slice(k)] | ||
} | ||
else { | ||
js = str + js | ||
str = '' | ||
} | ||
k = str.lastIndexOf('<', k -1) | ||
} | ||
return [str, js] | ||
return ['', str] | ||
} | ||
function compileTemplate(lang, html) { | ||
function compileTemplate(lang, html, opts) { | ||
var parser = parsers.html[lang] | ||
@@ -401,3 +425,3 @@ | ||
return parser(html) | ||
return parser(html, opts) | ||
} | ||
@@ -418,3 +442,3 @@ | ||
if (opts.template) | ||
src = compileTemplate(opts.template, src) | ||
src = compileTemplate(opts.template, src, opts.templateOptions) | ||
@@ -449,3 +473,3 @@ label = url ? '//src: ' + url + '\n' : '' | ||
styles += (styles ? ' ' : '') + | ||
compileCSS(_style, tagName, getType(_attrs), scoped) | ||
compileCSS(_style, tagName, getType(_attrs), scoped, getParserOptions(_attrs)) | ||
return '' | ||
@@ -452,0 +476,0 @@ }) |
102
lib/core.js
@@ -85,2 +85,19 @@ /** | ||
/** | ||
* Merge two javascript object extending the properties of the first one with | ||
* the second | ||
* @param {object} obj - source object | ||
* @param {object} props - extra properties | ||
* @returns {object} source object containing the new properties | ||
*/ | ||
function extend(obj, props) { | ||
for (var prop in props) { | ||
/* istanbul ignore next */ | ||
if (props.hasOwnProperty(prop)) { | ||
obj[prop] = props[prop] | ||
} | ||
} | ||
return obj | ||
} | ||
/** | ||
* Parses and format attributes. | ||
@@ -147,10 +164,9 @@ * | ||
expr = list[i] | ||
if (expr[0] === '^') { | ||
if (expr[0] === '^') | ||
expr = expr.slice(1) | ||
} | ||
else if (jsfn) { | ||
expr = jsfn(expr, opts).replace(/[\r\n]+/g, ' ').trim() | ||
if (expr.slice(-1) === ';') expr = expr.slice(0, -1) | ||
expr = jsfn(expr, opts) | ||
if (/;\s*$/.test(expr)) expr = expr.slice(0, expr.search(/;\s*$/)) | ||
} | ||
list[i] = '\u0001' + (pcex.push(expr.trim()) - 1) + _bp[1] | ||
list[i] = '\u0001' + (pcex.push(expr.replace(/[\r\n]+/g, ' ').trim()) - 1) + _bp[1] | ||
} | ||
@@ -289,6 +305,7 @@ html = list.join('') | ||
* @param {Object} opts - Options, can include a custom parser function | ||
* @param {string} [type] - Optional type for parser selection | ||
* @param {string} type - Optional type for parser selection | ||
* @param {Object} parserOpts - Optional parser options | ||
* @returns {string} The parsed JavaScript code | ||
*/ | ||
function compileJS(js, opts, type) { | ||
function compileJS(js, opts, type, parserOpts) { | ||
if (!type) type = opts.type | ||
@@ -300,3 +317,3 @@ | ||
return parser(js).replace(TRIM_TRAIL, '') | ||
return parser(js, parserOpts).replace(TRIM_TRAIL, '') | ||
} | ||
@@ -348,5 +365,6 @@ | ||
* @param {boolean} [scoped] - `true` for scoped styles | ||
* @param {object} opts - get the custom parser options | ||
* @returns {string} The processed style block | ||
*/ | ||
function compileCSS(style, tag, type, scoped) { | ||
function compileCSS(style, tag, type, scoped, opts) { | ||
@@ -358,3 +376,3 @@ if (type) { | ||
else if (parsers.css[type]) { | ||
style = parsers.css[type](tag, style) | ||
style = parsers.css[type](tag, style, opts) | ||
} | ||
@@ -390,3 +408,3 @@ else if (type !== 'css') { | ||
//#if READ_JS_SRC | ||
// Returns the value of any attribute, or the empty string for missing attribute. | ||
@@ -399,2 +417,3 @@ function getAttr(str, name) { | ||
match = str && str.match(re) | ||
/* istanbul ignore next */ | ||
str = match && (match[1] || match[2]) | ||
@@ -404,11 +423,22 @@ } | ||
} | ||
//#endif | ||
// get the parser options from the options attribute | ||
function getParserOptions(attrs) { | ||
var opts = getAttr(attrs, 'options') | ||
// convert the string into a valid js object | ||
/* istanbul ignore next */ | ||
if (opts) opts = JSON.parse(parserOpts) | ||
return opts | ||
} | ||
// Runs the custom or default parser on the received JavaScript code. | ||
// The CLI version can read code from the file system (experimental) | ||
function getCode(code, opts, attrs) { | ||
var type = getType(attrs) | ||
var type = getType(attrs), | ||
parserOpts = getParserOptions(attrs) | ||
//#if READ_JS_SRC | ||
var src = getAttr(attrs, 'src') | ||
if (src) { | ||
@@ -421,3 +451,3 @@ var | ||
//#endif | ||
return compileJS(code, opts, type) | ||
return compileJS(code, opts, type, parserOpts) | ||
} | ||
@@ -430,35 +460,25 @@ | ||
// ``` | ||
var END_TAGS = /\/>\n|<(?:\/[\w\-]+\s*|[\w\-]+(?:\s+(?:[-\w:\xA0-\xFF][\S\s]*?)?)?)>\n/g | ||
var END_TAGS = /\/>\n|^<(?:\/[\w\-]+\s*|[\w\-]+(?:\s+(?:[-\w:\xA0-\xFF][\S\s]*?)?)?)>\n/ | ||
function splitBlocks(str) { | ||
var | ||
i, k, js = '', len = str.length | ||
var k, m | ||
k = str.indexOf('<') | ||
if (k < 0 || (i = str.lastIndexOf('>\n')) < 0 || k > i) | ||
return ['', str] | ||
/* istanbul ignore next: this if() can't be true, but just in case... */ | ||
if (str[str.length - 1] === '>') | ||
return [str, ''] | ||
// we have html and js code, maybe | ||
i += 2 | ||
js = str.slice(i) // js code | ||
str = str.slice(0, i) // likely html | ||
if (str[i - 3] !== '/') { | ||
// not ending with `/>`, so go slow to re-check | ||
if (str.match(END_TAGS)) { | ||
var s = RegExp.rightContext | ||
if (s) { | ||
js = s + js // code before last tag | ||
str = str.slice(0, len - js.length) | ||
} | ||
k = str.lastIndexOf('<') // first probable open tag | ||
while (~k) { | ||
if (m = str.slice(k).match(END_TAGS)) { | ||
k += m.index + m[0].length | ||
return [str.slice(0, k), str.slice(k)] | ||
} | ||
else { // no html, we catch something | ||
js = str + js // like `v < 0 && v >\n x` | ||
str = '' | ||
} | ||
k = str.lastIndexOf('<', k -1) | ||
} | ||
return [str, js] | ||
return ['', str] | ||
} | ||
// Runs the external HTML parser for the entire tag file | ||
function compileTemplate(lang, html) { | ||
function compileTemplate(lang, html, opts) { | ||
var parser = parsers.html[lang] | ||
@@ -469,3 +489,3 @@ | ||
return parser(html) | ||
return parser(html, opts) | ||
} | ||
@@ -509,3 +529,3 @@ | ||
if (opts.template) | ||
src = compileTemplate(opts.template, src) | ||
src = compileTemplate(opts.template, src, opts.templateOptions) | ||
@@ -549,3 +569,3 @@ //#if NODE | ||
styles += (styles ? ' ' : '') + | ||
compileCSS(_style, tagName, getType(_attrs), scoped) | ||
compileCSS(_style, tagName, getType(_attrs), scoped, getParserOptions(_attrs)) | ||
return '' | ||
@@ -552,0 +572,0 @@ }) |
//#if NODE | ||
//#undef RIOT | ||
/* riot-compiler 2.3.0, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
/* riot-compiler 2.3.1, @license MIT, (c) 2015 Muut Inc. + contributors */ | ||
;(function (root, factory) { | ||
@@ -5,0 +5,0 @@ |
@@ -13,3 +13,3 @@ /** | ||
//#if NODE | ||
function _try(name, req) { | ||
function _try(name, req) { //eslint-disable-line complexity | ||
@@ -28,4 +28,5 @@ function fn(r) { | ||
case 'es6': | ||
req = 'babel' | ||
break | ||
/* istanbul ignore next */ | ||
return fn('babel') || fn('babel-core') // versions 5.8x | ||
/* istanbul ignore next */ | ||
case 'babel': | ||
@@ -44,2 +45,7 @@ req = 'babel-core' | ||
break | ||
/* istanbul ignore next */ | ||
case 'scss': | ||
case 'sass': | ||
req = 'node-sass' | ||
break | ||
default: | ||
@@ -78,4 +84,4 @@ if (!req) req = name | ||
var _html = { | ||
jade: function (html) { | ||
return _req('jade').render(html, {pretty: true, doctype: 'html'}) | ||
jade: function (html, opts) { | ||
return _req('jade').render(html, extend({pretty: true, doctype: 'html'}, opts)) | ||
} | ||
@@ -85,3 +91,39 @@ } | ||
var _css = { | ||
stylus: function (tag, css) { | ||
//#if NODE | ||
sass: function(tag, css, opts) { // there's no standard sass for browsers | ||
var sass = _req('sass') | ||
return sass.renderSync(extend({ | ||
data: css, | ||
indentedSyntax: true, | ||
omitSourceMapUrl: true, | ||
outputStyle: 'compact' | ||
}, opts)).css + '' | ||
}, | ||
scss: function(tag, css, opts) { // there's no standard sass for browsers | ||
var sass = _req('sass') | ||
return sass.renderSync(extend({ | ||
data: css, | ||
indentedSyntax: false, | ||
omitSourceMapUrl: true, | ||
outputStyle: 'compact' | ||
}, opts)).css + '' | ||
}, | ||
less: function(tag, css, opts) { | ||
var less = _req('less'), | ||
ret | ||
less.render(css, extend({ // less has a different API for browser | ||
sync: true, | ||
compress: true | ||
}, opts), function (err, result) { | ||
// istanbul ignore next | ||
if (err) throw err | ||
ret = result.css | ||
}) | ||
return ret | ||
}, | ||
//#endif | ||
stylus: function (tag, css, opts) { | ||
var | ||
@@ -96,23 +138,26 @@ stylus = _req('stylus'), nib = _req('nib') // optional nib support | ||
var _js = { | ||
none: function (js) { | ||
none: function (js, opts) { | ||
return js | ||
}, | ||
livescript: function (js) { | ||
return _req('livescript').compile(js, {bare: true, header: false}) | ||
livescript: function (js, opts) { | ||
return _req('livescript').compile(js, extend({bare: true, header: false}, opts)) | ||
}, | ||
typescript: function (js) { | ||
return _req('typescript')(js).replace(/\r\n?/g, '\n') | ||
typescript: function (js, opts) { | ||
return _req('typescript')(js, opts).replace(/\r\n?/g, '\n') | ||
}, | ||
es6: function (js) { | ||
return _req('es6').transform(js, { | ||
blacklist: ['useStrict', 'react'], sourceMaps: false, comments: false | ||
}).code | ||
es6: /* istanbul ignore next */ function (js, opts) { | ||
return _req('es6').transform(js, extend({ | ||
blacklist: ['useStrict', 'strict', 'react'], sourceMaps: false, comments: false | ||
}, opts)).code | ||
}, | ||
babel: function (js) { | ||
return _req('babel').transform(js, { | ||
presets: ['es2015'], ast: false, sourceMaps: false, comments: false | ||
}).code | ||
babel: function (js, opts) { | ||
js = 'function __parser_babel_wrapper__(){' + js + '}' | ||
return _req('babel').transform(js, | ||
extend({ | ||
presets: ['es2015'] | ||
}, opts) | ||
).code.replace(/["']use strict["'];[\r\n]+/, '').slice(38, -2) | ||
}, | ||
coffee: function (js) { | ||
return _req('coffee').compile(js, {bare: true}) | ||
coffee: function (js, opts) { | ||
return _req('coffee').compile(js, extend({bare: true}, opts)) | ||
} | ||
@@ -119,0 +164,0 @@ } |
{ | ||
"name": "riot-compiler", | ||
"version": "2.3.0", | ||
"version": "2.3.1", | ||
"description": "Compiler for riot .tag files", | ||
@@ -5,0 +5,0 @@ "main": "dist/compiler.js", |
riot.tag2('babel', '<h3>{test}</h3>', '', '', function(opts) { | ||
var type = 'JavaScript'; | ||
this.test = 'This is ' + type; | ||
var type = 'JavaScript'; | ||
this.test = 'This is ' + type; | ||
}, '{ }'); |
@@ -95,2 +95,6 @@ // | ||
function _custom(js) { | ||
return 'var foo' | ||
} | ||
this.timeout(25000) // first call to babel-core is slooooow! | ||
@@ -144,20 +148,12 @@ | ||
// testParser.es6.tag | ||
it('es6 (for babel 5.8.x)', function () { | ||
// testParser.babel.tag | ||
it('babel', function () { | ||
if (have('es6')) { | ||
testParser('test', { type: 'es6' }) | ||
testParser('test', { type: 'babel' }) | ||
} | ||
}) | ||
// testParser.es6.tag | ||
/* | ||
it('new css.babel parser (babel-core 6.0.2+)', function () { | ||
if (have('babel')) { | ||
testParser('test', { type: 'babel' }) | ||
} | ||
})*/ | ||
// testParser-attr.es6.tag | ||
// testParser-attr.babel.tag | ||
it('babel with shorthands (fix #1090)', function () { | ||
if (have('babel')) { | ||
if (have('es6')) { | ||
testParser('test-attr', { type: 'babel', expr: true }) | ||
@@ -167,2 +163,10 @@ } | ||
// test.random.tag | ||
it('custom js parser', function () { | ||
compiler.parsers.js.custom = _custom | ||
testParser('test', { type: 'custom' }) | ||
}) | ||
}) | ||
@@ -175,8 +179,5 @@ | ||
function _sass(tag, css) { | ||
return '' + compiler.parsers._req('sass').renderSync({ | ||
data: css, | ||
indentedSyntax: true, | ||
omitSourceMapUrl: true, | ||
outputStyle: 'compact' }).css | ||
// custom parser | ||
compiler.parsers.css.postcss = function(tag, css, opts) { | ||
return require('postcss')([require('autoprefixer')]).process(css).css | ||
} | ||
@@ -202,5 +203,4 @@ | ||
// sass.tag | ||
it('sass, indented 2, margin 0 (custom parser)', function () { | ||
if (have('sass', 'node-sass')) { | ||
compiler.parsers.css.sass = _sass | ||
it('sass, indented 2, margin 0', function () { | ||
if (have('sass')) { | ||
testParser('sass', {}) | ||
@@ -210,2 +210,23 @@ } | ||
// scss.tag | ||
it('scss, indented 2, margin 0', function () { | ||
if (have('scss', 'node-sass')) { | ||
testParser('scss', {}) | ||
} | ||
}) | ||
// scss.tag | ||
it('custom parser using postcss + autoprefixer', function () { | ||
if (have('postcss', 'postcss')) { | ||
testParser('postcss', {}) | ||
} | ||
}) | ||
// less.tag | ||
it('less', function () { | ||
if (have('less')) { | ||
testParser('less', {}) | ||
} | ||
}) | ||
it('Mixing CSS blocks with different type', function () { | ||
@@ -237,2 +258,3 @@ testParser('mixed-css', {}) | ||
expect(compiler.compile).withArgs(str2).to.throwError() | ||
expect(have('unknown')).to.be(false) | ||
}) | ||
@@ -239,0 +261,0 @@ |
@@ -97,2 +97,6 @@ var fs = require('fs'), | ||
it('In shorthands newlines are converted to spaces #1306', function () { | ||
testFile('so-input') | ||
}) | ||
}) |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
115808
106
2784