riot-compiler
Advanced tools
Comparing version 2.4.1 to 2.5.0
/** | ||
* Compiler for riot custom tags | ||
* @version v2.4.1 | ||
* @version v2.5.0 | ||
*/ | ||
@@ -8,2 +8,17 @@ | ||
// istanbul ignore next | ||
function safeRegex (re) { | ||
var src = re.source | ||
var opt = re.global ? 'g' : '' | ||
if (re.ignoreCase) opt += 'i' | ||
if (re.multiline) opt += 'm' | ||
for (var i = 1; i < arguments.length; i++) { | ||
src = src.replace('@', '\\' + arguments[i]) | ||
} | ||
return new RegExp(src, opt) | ||
} | ||
/** | ||
@@ -47,3 +62,3 @@ * @module parsers | ||
/* eslint-disable */ | ||
console.log('DEPRECATION WARNING: jade was renamed "pug" - the jade parser will be removed in riot@3.0.0!') | ||
console.log('DEPRECATION WARNING: jade was renamed "pug" - The jade parser will be removed in riot@3.0.0!') | ||
/* eslint-enable */ | ||
@@ -117,4 +132,2 @@ return renderPug('jade', html, opts, url) | ||
/* eslint-disable */ | ||
var extend = parsers.utils.extend | ||
@@ -149,7 +162,9 @@ /* eslint-enable */ | ||
var IMPORT_STATEMENT = /^(?: )*(?:import)(?:(?:.*))*$/gm | ||
var TRIM_TRAIL = /[ \t]+$/gm | ||
var | ||
RE_HASEXPR = /\x01#\d/, | ||
RE_REPEXPR = /\x01#(\d+)/g, | ||
RE_HASEXPR = safeRegex(/@#\d/, 'x01'), | ||
RE_REPEXPR = safeRegex(/@#(\d+)/g, 'x01'), | ||
CH_IDEXPR = '\x01#', | ||
@@ -258,2 +273,16 @@ CH_DQCODE = '\u2057', | ||
function compileImports (js) { | ||
var imp = [] | ||
var imports = '' | ||
while (imp = IMPORT_STATEMENT.exec(js)) { | ||
imports += imp[0].trim() + '\n' | ||
} | ||
return imports | ||
} | ||
function rmImports (js) { | ||
var jsCode = js.replace(IMPORT_STATEMENT, '') | ||
return jsCode | ||
} | ||
function _compileHTML (html, opts, pcex) { | ||
@@ -369,7 +398,4 @@ | ||
var parser = opts.parser || (type ? parsers.js[type] : riotjs) | ||
var parser = opts.parser || type && parsers._req('js.' + type, true) || riotjs | ||
if (!parser) { | ||
throw new Error('JS parser not found: "' + type + '"') | ||
} | ||
return parser(js, parserOpts, url).replace(/\r\n?/g, '\n').replace(TRIM_TRAIL, '') | ||
@@ -430,6 +456,6 @@ } | ||
scoped = true | ||
} else if (parsers.css[type]) { | ||
css = parsers.css[type](tag, css, opts.parserOpts || {}, opts.url) | ||
} else if (type !== 'css') { | ||
throw new Error('CSS parser not found: "' + type + '"') | ||
var parser = parsers._req('css.' + type, true) | ||
css = parser(tag, css, opts.parserOpts || {}, opts.url) | ||
} | ||
@@ -470,3 +496,3 @@ } | ||
function mktag (name, html, css, attr, js, opts) { | ||
function mktag (name, html, css, attr, js, imports, opts) { | ||
var | ||
@@ -478,3 +504,3 @@ c = opts.debug ? ',\n ' : ', ', | ||
return 'riot.tag2(\'' + name + SQ + | ||
return imports + 'riot.tag2(\'' + name + SQ + | ||
c + _q(html, 1) + | ||
@@ -574,7 +600,4 @@ c + _q(css) + | ||
function compileTemplate (html, url, lang, opts) { | ||
var parser = parsers.html[lang] | ||
if (!parser) { | ||
throw new Error('Template parser not found: "' + lang + '"') | ||
} | ||
var parser = parsers._req('html.' + lang, true) | ||
return parser(html, opts, url) | ||
@@ -624,2 +647,3 @@ } | ||
html = '', | ||
imports = '', | ||
pcex = [] | ||
@@ -671,2 +695,4 @@ | ||
body = _compileJS(blocks[1], opts, null, null, url) | ||
imports = compileImports(jscode) | ||
jscode = rmImports(jscode) | ||
if (body) jscode += (jscode ? '\n' : '') + body | ||
@@ -690,3 +716,3 @@ } | ||
return mktag(tagName, html, styles, attribs, jscode, opts) | ||
return mktag(tagName, html, styles, attribs, jscode, imports, opts) | ||
}) | ||
@@ -699,3 +725,3 @@ | ||
var version = 'v2.4.1' | ||
var version = 'v2.5.0' | ||
@@ -702,0 +728,0 @@ export default { |
@@ -40,3 +40,3 @@ | ||
/* eslint-disable */ | ||
console.log('DEPRECATION WARNING: jade was renamed "pug" - the jade parser will be removed in riot@3.0.0!') | ||
console.log('DEPRECATION WARNING: jade was renamed "pug" - The jade parser will be removed in riot@3.0.0!') | ||
/* eslint-enable */ | ||
@@ -110,8 +110,6 @@ return renderPug('jade', html, opts, url) | ||
* Compiler for riot custom tags | ||
* @version v2.4.1 | ||
* @version v2.5.0 | ||
*/ | ||
var compile = (function () { | ||
/* eslint-disable */ | ||
var extend = parsers.utils.extend | ||
@@ -146,7 +144,9 @@ /* eslint-enable */ | ||
var IMPORT_STATEMENT = /^(?: )*(?:import)(?:(?:.*))*$/gm | ||
var TRIM_TRAIL = /[ \t]+$/gm | ||
var | ||
RE_HASEXPR = /\x01#\d/, | ||
RE_REPEXPR = /\x01#(\d+)/g, | ||
RE_HASEXPR = safeRegex(/@#\d/, 'x01'), | ||
RE_REPEXPR = safeRegex(/@#(\d+)/g, 'x01'), | ||
CH_IDEXPR = '\x01#', | ||
@@ -255,2 +255,16 @@ CH_DQCODE = '\u2057', | ||
function compileImports (js) { | ||
var imp = [] | ||
var imports = '' | ||
while (imp = IMPORT_STATEMENT.exec(js)) { | ||
imports += imp[0].trim() + '\n' | ||
} | ||
return imports | ||
} | ||
function rmImports (js) { | ||
var jsCode = js.replace(IMPORT_STATEMENT, '') | ||
return jsCode | ||
} | ||
function _compileHTML (html, opts, pcex) { | ||
@@ -366,7 +380,4 @@ | ||
var parser = opts.parser || (type ? parsers.js[type] : riotjs) | ||
var parser = opts.parser || type && parsers._req('js.' + type, true) || riotjs | ||
if (!parser) { | ||
throw new Error('JS parser not found: "' + type + '"') | ||
} | ||
return parser(js, parserOpts, url).replace(/\r\n?/g, '\n').replace(TRIM_TRAIL, '') | ||
@@ -427,6 +438,6 @@ } | ||
scoped = true | ||
} else if (parsers.css[type]) { | ||
css = parsers.css[type](tag, css, opts.parserOpts || {}, opts.url) | ||
} else if (type !== 'css') { | ||
throw new Error('CSS parser not found: "' + type + '"') | ||
var parser = parsers._req('css.' + type, true) | ||
css = parser(tag, css, opts.parserOpts || {}, opts.url) | ||
} | ||
@@ -467,3 +478,3 @@ } | ||
function mktag (name, html, css, attr, js, opts) { | ||
function mktag (name, html, css, attr, js, imports, opts) { | ||
var | ||
@@ -475,3 +486,3 @@ c = opts.debug ? ',\n ' : ', ', | ||
return 'riot.tag2(\'' + name + SQ + | ||
return imports + 'riot.tag2(\'' + name + SQ + | ||
c + _q(html, 1) + | ||
@@ -571,7 +582,4 @@ c + _q(css) + | ||
function compileTemplate (html, url, lang, opts) { | ||
var parser = parsers.html[lang] | ||
if (!parser) { | ||
throw new Error('Template parser not found: "' + lang + '"') | ||
} | ||
var parser = parsers._req('html.' + lang, true) | ||
return parser(html, opts, url) | ||
@@ -621,2 +629,3 @@ } | ||
html = '', | ||
imports = '', | ||
pcex = [] | ||
@@ -668,2 +677,4 @@ | ||
body = _compileJS(blocks[1], opts, null, null, url) | ||
imports = compileImports(jscode) | ||
jscode = rmImports(jscode) | ||
if (body) jscode += (jscode ? '\n' : '') + body | ||
@@ -687,3 +698,3 @@ } | ||
return mktag(tagName, html, styles, attribs, jscode, opts) | ||
return mktag(tagName, html, styles, attribs, jscode, imports, opts) | ||
}) | ||
@@ -701,3 +712,3 @@ | ||
js: compileJS, | ||
version: 'v2.4.1' | ||
version: 'v2.5.0' | ||
} | ||
@@ -704,0 +715,0 @@ return compile |
@@ -0,1 +1,3 @@ | ||
'use strict' | ||
/** | ||
@@ -5,2 +7,3 @@ * Brackets support for the node.js version of the riot-compiler | ||
*/ | ||
var safeRegex = require('./safe-regex.js') | ||
@@ -218,5 +221,4 @@ /** | ||
var | ||
INVALIDCH = /[\x00-\x1F<>a-zA-Z0-9'",;\\]/, // invalid characters for brackets | ||
ESCAPEDCH = /(?=[[\]()*+?.^$|])/g // this characters must be escaped | ||
var INVALIDCH = safeRegex(/[@-@<>a-zA-Z0-9'",;\\]/, 'x00', 'x1F') // invalid characters for brackets | ||
var ESCAPEDCH = /(?=[[\]()*+?.^$|])/g // this characters must be escaped | ||
@@ -223,0 +225,0 @@ /** |
/** | ||
* The riot-compiler v2.4.1 | ||
* The riot-compiler v2.5.0 | ||
* | ||
* @module compiler | ||
* @version v2.4.1 | ||
* @version v2.5.0 | ||
* @license MIT | ||
@@ -10,7 +10,8 @@ * @copyright Muut Inc. + contributors | ||
'use strict' | ||
var brackets = require('./brackets') | ||
var parsers = require('./parsers') | ||
var path = require('path') | ||
/* eslint-disable */ | ||
var brackets = require('./brackets') | ||
var parsers = require('./parsers') | ||
var safeRegex = require('./safe-regex') | ||
var path = require('path') | ||
var extend = require('./parsers/_utils').mixobj | ||
@@ -118,2 +119,8 @@ /* eslint-enable */ | ||
/** | ||
* Matches the 'import' statement | ||
* @const {RegExp} | ||
*/ | ||
var IMPORT_STATEMENT = /^(?: )*(?:import)(?:(?:.*))*$/gm | ||
/** | ||
* Matches trailing spaces and tabs by line. | ||
@@ -125,4 +132,4 @@ * @const {RegExp} | ||
var | ||
RE_HASEXPR = /\x01#\d/, | ||
RE_REPEXPR = /\x01#(\d+)/g, | ||
RE_HASEXPR = safeRegex(/@#\d/, 'x01'), | ||
RE_REPEXPR = safeRegex(/@#(\d+)/g, 'x01'), | ||
CH_IDEXPR = '\x01#', | ||
@@ -269,2 +276,26 @@ CH_DQCODE = '\u2057', | ||
/** | ||
* Return imports statement of the code as a string | ||
* @param {string} js - The js code containing the imports statement | ||
* @returns {string} Js code containing only the imports statement | ||
*/ | ||
function compileImports (js) { | ||
var imp = [] | ||
var imports = '' | ||
while (imp = IMPORT_STATEMENT.exec(js)) { | ||
imports += imp[0].trim() + '\n' | ||
} | ||
return imports | ||
} | ||
/** | ||
* Remove 'import' statement from JSCode | ||
* @param {string} js - The Js code | ||
* @returns {string} jsCode The js code without 'import' statement | ||
*/ | ||
function rmImports (js) { | ||
var jsCode = js.replace(IMPORT_STATEMENT, '') | ||
return jsCode | ||
} | ||
/** | ||
* The internal HTML compiler. | ||
@@ -443,7 +474,4 @@ * | ||
var parser = opts.parser || (type ? parsers.js[type] : riotjs) | ||
var parser = opts.parser || type && parsers._req('js.' + type, true) || riotjs | ||
if (!parser) { | ||
throw new Error('JS parser not found: "' + type + '"') | ||
} | ||
return parser(js, parserOpts, url).replace(/\r\n?/g, '\n').replace(TRIM_TRAIL, '') | ||
@@ -547,6 +575,6 @@ } | ||
scoped = true | ||
} else if (parsers.css[type]) { | ||
css = parsers.css[type](tag, css, opts.parserOpts || {}, opts.url) | ||
} else if (type !== 'css') { | ||
throw new Error('CSS parser not found: "' + type + '"') | ||
var parser = parsers._req('css.' + type, true) | ||
css = parser(tag, css, opts.parserOpts || {}, opts.url) | ||
} | ||
@@ -664,6 +692,7 @@ } | ||
* @param {string} js - JavaScript "constructor" | ||
* @param {string} imports - Code containing 'import' statements | ||
* @param {object} opts - Compiler options | ||
* @returns {string} Code to call `riot.tag2` | ||
*/ | ||
function mktag (name, html, css, attr, js, opts) { | ||
function mktag (name, html, css, attr, js, imports, opts) { | ||
var | ||
@@ -675,3 +704,3 @@ c = opts.debug ? ',\n ' : ', ', | ||
return 'riot.tag2(\'' + name + SQ + | ||
return imports + 'riot.tag2(\'' + name + SQ + | ||
c + _q(html, 1) + | ||
@@ -842,7 +871,4 @@ c + _q(css) + | ||
function compileTemplate (html, url, lang, opts) { | ||
var parser = parsers.html[lang] | ||
if (!parser) { | ||
throw new Error('Template parser not found: "' + lang + '"') | ||
} | ||
var parser = parsers._req('html.' + lang, true) | ||
return parser(html, opts, url) | ||
@@ -934,2 +960,3 @@ } | ||
html = '', | ||
imports = '', | ||
pcex = [] | ||
@@ -982,2 +1009,4 @@ | ||
body = _compileJS(blocks[1], opts, null, null, url) | ||
imports = compileImports(jscode) | ||
jscode = rmImports(jscode) | ||
if (body) jscode += (jscode ? '\n' : '') + body | ||
@@ -1001,3 +1030,3 @@ } | ||
return mktag(tagName, html, styles, attribs, jscode, opts) | ||
return mktag(tagName, html, styles, attribs, jscode, imports, opts) | ||
}) | ||
@@ -1021,3 +1050,3 @@ | ||
parsers: parsers, | ||
version: 'v2.4.1' | ||
version: 'v2.5.0' | ||
} |
@@ -5,3 +5,7 @@ /** | ||
*/ | ||
'use strict' | ||
var REQPATH = './parsers/' | ||
var TRUE = true | ||
var NULL = null | ||
@@ -13,24 +17,115 @@ // Passtrough for the internal `none` and `javascript` parsers | ||
// Initialize the cache with parsers that cannot be required | ||
var _mods = { none: _none, javascript: _none } | ||
// This is the main parsers object holding the html, js, and css keys | ||
// initialized with the parsers that cannot be required. | ||
// | ||
var _parsers = { | ||
html: {}, | ||
css: {}, | ||
js: { none: _none, javascript: _none } | ||
} | ||
// Native riot parsers go here, having false if already required. | ||
var _loaders = { | ||
html: { jade: TRUE, pug: TRUE }, | ||
css: { sass: TRUE, scss: TRUE, less: TRUE, stylus: TRUE }, | ||
js: { es6: TRUE, babel: TRUE, coffee: TRUE, livescript: TRUE, typescript: TRUE } | ||
} | ||
_loaders.js.coffeescript = TRUE // 4 the nostalgics | ||
/** | ||
* Loads a "native" riot parser. | ||
* | ||
* It set the flag in the _loaders object to false for the required parser. | ||
* Try to load the parser and save the module to the _parsers object. | ||
* On error, throws a custom exception (adds 'riot' notice to the original). | ||
* On success returns the loaded module. | ||
* | ||
* @param {string} branch - The branch name inside _parsers/loaders | ||
* @param {string} parser - The parser's name | ||
* @returns {Function} Loaded module. | ||
*/ | ||
function _load (branch, parser) { | ||
var req = REQPATH + (parser === 'coffeescript' ? 'coffee' : parser) | ||
var mod | ||
_loaders[branch][parser] = false // try once | ||
try { | ||
mod = _parsers[branch][parser] = require(req) | ||
} catch (e) { | ||
// istanbul ignore next | ||
var err = 'Can\'t load the ' + branch + '.' + parser + ' riot parser: ' + e | ||
// istanbul ignore next | ||
throw new Error(err) | ||
} | ||
return mod | ||
} | ||
/** | ||
* Returns the branch where the parser resides, or NULL if the parser not found. | ||
* If the parameter 'branch' is empty, the precedence order is js, css, html. | ||
* | ||
* @param {string} branch - The name of the branch to search, can be empty | ||
* @param {string} name - The parser's name | ||
* @returns {string} Name of the parser branch. | ||
*/ | ||
function _find (branch, name) { | ||
return branch ? _parsers[branch][name] && branch | ||
: _parsers.js[name] ? 'js' | ||
: _parsers.css[name] ? 'css' | ||
: _parsers.html[name] ? 'html' : NULL | ||
} | ||
/** | ||
* Returns a parser instance by its name, requiring the module without generating error. | ||
* Parsers name can include the branch (ej. 'js.es6'). | ||
* If branch is not included, the precedence order for searching is 'js', 'css', 'html' | ||
* | ||
* Public through the `parsers._req` function. | ||
* | ||
* @param {string} name - The parser's name, as registered in the parsers object | ||
* @param {string} [req] - To be used by require(). Defaults to parser's name | ||
* @returns {Function} The parser instance, null if the parser is not found. | ||
* @param {string} name - The parser's name, as registered in the parsers object | ||
* @param {boolean} [req] - true if required (throws on error) | ||
* @returns {Function} The parser instance, null if the parser is not found. | ||
*/ | ||
function _req (name, req) { | ||
var mod | ||
var | ||
err, | ||
mod, | ||
branch, | ||
parser = name.split('.') | ||
if (name in _mods) { | ||
mod = _mods[name] | ||
if (parser.length > 1) { | ||
branch = parser[0] | ||
parser = parser[1] | ||
} else { | ||
if (!req) req = REQPATH + (name === 'coffeescript' ? 'coffee' : name) | ||
try { mod = require(req) } catch (_) {/**/} | ||
_mods[name] = mod || null | ||
branch = NULL | ||
parser = name | ||
} | ||
// is the parser registered? | ||
branch = _find(branch, parser) | ||
if (!branch) { | ||
if (req) { | ||
err = 'Riot parser "' + name + '" is not registered.' | ||
throw new Error(err) | ||
} | ||
return NULL | ||
} | ||
// parser registered, needs load? | ||
if (_loaders[branch][parser]) { | ||
if (req) { | ||
mod = _load(branch, parser) | ||
} else { | ||
try { | ||
mod = _load(branch, parser) | ||
} catch (_) { | ||
// istanbul ignore next | ||
mod = NULL | ||
} | ||
} | ||
} else { | ||
mod = _parsers[branch][parser] | ||
} | ||
return mod | ||
@@ -46,22 +141,21 @@ } | ||
*/ | ||
function _makelist (_p) { | ||
var names = { | ||
html: ['jade', 'pug'], | ||
css: ['sass', 'scss', 'less', 'stylus'], | ||
js: ['es6', 'babel', 'coffee', 'livescript', 'typescript'] | ||
} | ||
function _setLoaders (_p) { | ||
// loads the module at first use and returns the parsed result | ||
function mkloader (dest, name) { | ||
function mkloader (branch, parser) { | ||
return function _loadParser (p1, p2, p3, p4) { | ||
return (dest[name] = _req(name))(p1, p2, p3, p4) | ||
var fn = _load(branch, parser) | ||
return fn(p1, p2, p3, p4) | ||
} | ||
} | ||
for (var type in names) { // eslint-disable-line guard-for-in | ||
var dest = _p[type] | ||
for (var branch in _loaders) { | ||
// istanbul ignore else | ||
if (_loaders.hasOwnProperty(branch)) { | ||
var names = Object.keys(_loaders[branch]) | ||
names[type].forEach(function (name) { // eslint-disable-line no-loop-func | ||
dest[name] = mkloader(dest, name) | ||
}) | ||
names.forEach(function (name) { | ||
_p[branch][name] = mkloader(branch, name) | ||
}) | ||
} | ||
} | ||
@@ -71,10 +165,4 @@ return _p | ||
// Exports the initialized parsers | ||
module.exports = _makelist({ | ||
_req: _req, | ||
html: {}, | ||
css: {}, | ||
js: { none: _none, javascript: _none } | ||
}) | ||
_setLoaders(_parsers)._req = _req | ||
module.exports.js.coffeescript = module.exports.js.coffee // 4 the nostalgics | ||
module.exports = _parsers |
{ | ||
"name": "riot-compiler", | ||
"version": "2.4.1", | ||
"version": "2.5.0", | ||
"description": "Compiler for riot .tag files", | ||
@@ -5,0 +5,0 @@ "main": "lib/compiler.js", |
@@ -13,2 +13,4 @@ /** | ||
//#include safe-regex | ||
//#include parsers_br | ||
@@ -15,0 +17,0 @@ |
@@ -11,11 +11,12 @@ //#if NODE | ||
'use strict' | ||
var brackets = require('./brackets') | ||
var parsers = require('./parsers') | ||
var path = require('path') // used by getCode() | ||
var brackets = require('./brackets') | ||
var parsers = require('./parsers') | ||
var safeRegex = require('./safe-regex') | ||
var path = require('path') // used by getCode() | ||
//#endif | ||
/* eslint-disable */ | ||
//#if NODE | ||
/*#if NODE | ||
var extend = require('./parsers/_utils').mixobj | ||
//#else | ||
//#else */ | ||
// shortcut to enable the use of the parsers util methods | ||
@@ -130,2 +131,8 @@ var extend = parsers.utils.extend | ||
/** | ||
* Matches the 'import' statement | ||
* @const {RegExp} | ||
*/ | ||
var IMPORT_STATEMENT = /^(?: )*(?:import)(?:(?:.*))*$/gm | ||
/** | ||
* Matches trailing spaces and tabs by line. | ||
@@ -138,6 +145,6 @@ * @const {RegExp} | ||
var | ||
RE_HASEXPR = /\x01#\d/, // for searching a hidden expression in a string | ||
RE_REPEXPR = /\x01#(\d+)/g, // used to restore a hidden expression | ||
CH_IDEXPR = '\x01#', // sequence for marking a hidden expression | ||
CH_DQCODE = '\u2057', // escape double quotes with this char | ||
RE_HASEXPR = safeRegex(/@#\d/, 'x01'), // for searching a hidden expression in a string | ||
RE_REPEXPR = safeRegex(/@#(\d+)/g, 'x01'), // used to restore a hidden expression | ||
CH_IDEXPR = '\x01#', // sequence for marking a hidden expression | ||
CH_DQCODE = '\u2057', // escape double quotes with this char | ||
DQ = '"', | ||
@@ -282,2 +289,26 @@ SQ = "'" | ||
/** | ||
* Return imports statement of the code as a string | ||
* @param {string} js - The js code containing the imports statement | ||
* @returns {string} Js code containing only the imports statement | ||
*/ | ||
function compileImports (js) { | ||
var imp = [] | ||
var imports = '' | ||
while (imp = IMPORT_STATEMENT.exec(js)) { | ||
imports += imp[0].trim() + '\n' | ||
} | ||
return imports | ||
} | ||
/** | ||
* Remove 'import' statement from JSCode | ||
* @param {string} js - The Js code | ||
* @returns {string} jsCode The js code without 'import' statement | ||
*/ | ||
function rmImports (js) { | ||
var jsCode = js.replace(IMPORT_STATEMENT, '') | ||
return jsCode | ||
} | ||
/* | ||
@@ -479,7 +510,5 @@ HTML Compiler | ||
var parser = opts.parser || (type ? parsers.js[type] : riotjs) | ||
// 2016-05-11: _req throws exception for invalid parser | ||
var parser = opts.parser || type && parsers._req('js.' + type, true) || riotjs | ||
if (!parser) { | ||
throw new Error('JS parser not found: "' + type + '"') | ||
} | ||
return parser(js, parserOpts, url).replace(/\r\n?/g, '\n').replace(TRIM_TRAIL, '') | ||
@@ -596,6 +625,6 @@ } | ||
scoped = true | ||
} else if (parsers.css[type]) { | ||
css = parsers.css[type](tag, css, opts.parserOpts || {}, opts.url) | ||
} else if (type !== 'css') { | ||
throw new Error('CSS parser not found: "' + type + '"') | ||
// 2016-05-11: _req throws exception for invalid parser | ||
var parser = parsers._req('css.' + type, true) | ||
css = parser(tag, css, opts.parserOpts || {}, opts.url) | ||
} | ||
@@ -722,6 +751,7 @@ } | ||
* @param {string} js - JavaScript "constructor" | ||
* @param {string} imports - Code containing 'import' statements | ||
* @param {object} opts - Compiler options | ||
* @returns {string} Code to call `riot.tag2` | ||
*/ | ||
function mktag (name, html, css, attr, js, opts) { | ||
function mktag (name, html, css, attr, js, imports, opts) { | ||
var | ||
@@ -735,3 +765,3 @@ c = opts.debug ? ',\n ' : ', ', | ||
// 2016-01-18: html can contain eols if opts.whitespace=1, fix with q(s,1) | ||
return 'riot.tag2(\'' + name + SQ + | ||
return imports + 'riot.tag2(\'' + name + SQ + | ||
c + _q(html, 1) + | ||
@@ -908,7 +938,4 @@ c + _q(css) + | ||
function compileTemplate (html, url, lang, opts) { | ||
var parser = parsers.html[lang] | ||
if (!parser) { | ||
throw new Error('Template parser not found: "' + lang + '"') | ||
} | ||
// 2016-05-11: _req throws exception for invalid parser (fix #60) | ||
var parser = parsers._req('html.' + lang, true) | ||
return parser(html, opts, url) | ||
@@ -1018,2 +1045,3 @@ } | ||
html = '', | ||
imports = '', | ||
pcex = [] | ||
@@ -1076,2 +1104,4 @@ | ||
body = _compileJS(blocks[1], opts, null, null, url) | ||
imports = compileImports(jscode) | ||
jscode = rmImports(jscode) | ||
if (body) jscode += (jscode ? '\n' : '') + body | ||
@@ -1098,3 +1128,3 @@ } | ||
// replace the tag with a call to the riot.tag2 function and we are done | ||
return mktag(tagName, html, styles, attribs, jscode, opts) | ||
return mktag(tagName, html, styles, attribs, jscode, imports, opts) | ||
}) | ||
@@ -1101,0 +1131,0 @@ |
@@ -60,3 +60,3 @@ //#if 0 | ||
/* eslint-disable */ | ||
console.log('DEPRECATION WARNING: jade was renamed "pug" - the jade parser will be removed in riot@3.0.0!') | ||
console.log('DEPRECATION WARNING: jade was renamed "pug" - The jade parser will be removed in riot@3.0.0!') | ||
/* eslint-enable */ | ||
@@ -63,0 +63,0 @@ return renderPug('jade', html, opts, url) |
@@ -11,4 +11,3 @@ // | ||
fs = require('fs'), | ||
norm = require('../helpers').normalizeJS, | ||
reqname = require('../helpers').requireName | ||
norm = require('../helpers').normalizeJS | ||
@@ -20,8 +19,7 @@ var | ||
function have (mod, req) { | ||
if (parsers._req(mod, req)) return true | ||
function have (name) { | ||
if (parsers._req(name)) return true | ||
if (mod !== 'unknown') { | ||
if (!req) req = reqname(mod) | ||
console.error('\tnot installed locally: ' + req + ' alias "' + mod + '"') | ||
if (name !== 'unknown') { | ||
console.error('\t' + name + ' parser not installed locally.') | ||
} | ||
@@ -251,3 +249,3 @@ return false | ||
it('custom parser using postcss + autoprefixer', function () { | ||
if (have('postcss', 'postcss')) { | ||
if (have('postcss')) { | ||
testParser('postcss') | ||
@@ -254,0 +252,0 @@ } |
@@ -13,4 +13,2 @@ /*eslint-env mocha */ | ||
basepath = path.resolve(__dirname, './'), | ||
//fixtures = path.relative(basepath, path.join(__dirname, 'fixtures')), | ||
//expected = path.relative(basepath, path.join(__dirname, 'expect')) | ||
fixtures = path.join(basepath, 'fixtures'), | ||
@@ -77,2 +75,6 @@ expected = path.join(basepath, 'expect') | ||
it('Make sure all the es6 import will be always moved in the global scope', function () { | ||
testFile('es6-import') | ||
}) | ||
it('Flexible method style (v2.3)', function () { | ||
@@ -121,2 +123,13 @@ testFile('free-style') | ||
it('parser can autoload from the `parsers` object', function () { | ||
var js | ||
try { | ||
js = require('coffee-script') | ||
} catch (_) { | ||
js = null | ||
} | ||
if (js) expect(compiler.parsers.js.coffee('x=0')).to.match(/var x/) | ||
else console.log('\tPlease give me a coffee.') | ||
}) | ||
it('The `whitespace` option preserves newlines and tabs', function () { | ||
@@ -123,0 +136,0 @@ var src = [ |
248285
179
5631
167