Skip to content

Commit

Permalink
Merge branch 'bug/1998' into next
Browse files Browse the repository at this point in the history
* bug/1998:
  closes #1998 and normalize the riot.require function
  wip
  • Loading branch information
GianlucaGuarini committed Oct 8, 2016
2 parents 24fd0ee + d6a82d9 commit 3104cb3
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 22 deletions.
6 changes: 3 additions & 3 deletions lib/browser/tag/core.js
Expand Up @@ -3,7 +3,7 @@ import { isString, isUndefined, isObject, isFunction } from './../common/util/ch
import { setAttr, getAttr, $$ } from './../common/util/dom'
import { each, extend } from './../common/util/misc'
import { mountTo, selectTags } from './../common/util/tags'

import { util } from '../../riot'

import {
__TAG_IMPL,
Expand Down Expand Up @@ -83,8 +83,8 @@ export function tag2(name, tmpl, css, attrs, fn) {
var exists = !!__TAG_IMPL[name]
__TAG_IMPL[name] = { name, tmpl, attrs, fn }

if (exists && riot.util.hotReloader)
riot.util.hotReloader(name)
if (exists && util.hotReloader)
util.hotReloader(name)

return name
}
Expand Down
53 changes: 36 additions & 17 deletions lib/server/index.js
Expand Up @@ -7,32 +7,51 @@ const
riot = require(riotPath),
// simple-dom helper
sdom = require('./sdom'),
compiler = require('riot-compiler'),
cache = {}

// shared function that will be used by riot.load and by require('some.tag')
function loadAndCompile(filename, opts) {
var src = compiler.compile(fs.readFileSync(filename, 'utf8'), opts)
var hasGlobalRiot = typeof global.riot !== 'undefined'
if (!hasGlobalRiot) global.riot = riot
module._compile(`module.exports = ${ src }`, filename)
if (!hasGlobalRiot) delete global.riot
Module = require('module'),
compiler = require('riot-compiler')

/**
* Function that will be used by riot.require and by require('some.tag')
* @param { String } filename - path to the file to load and compile
* @param { Object } opts - compiler options
* @param { Object } context - context where the tag will be mounted (Module)
*/
function loadAndCompile(filename, opts, context) {
const src = compiler.compile(fs.readFileSync(filename, 'utf8'), opts)
const preTag = src.substring(0, src.indexOf('riot.tag'))
const tagDefinition = src.substring(src.indexOf('riot.tag'))

context._compile(`
const riot = require('${ riotPath }')
${ preTag }
module.exports = ${ tagDefinition }
`, filename)
}

// enable the loading of riot tags with options riot.require('some.tag', { template: 'pug' })
/**
* Enable the loading of riot tags with options riot.require('some.tag', { template: 'pug' })
* @param { String } filename - path to the file to load and compile
* @param { Object } opts - compiler options
* @returns { String } tag name
*/
function riotRequire(filename, opts) {
if (cache[filename]) {
return cache[filename]
}
loadAndCompile(filename, opts)
return cache[filename] = module.exports
var module = new Module()
module.id = module.filename = filename
loadAndCompile(filename, opts, module)
return module.exports
}

// allow to require('some.tag')
require.extensions['.tag'] = function(module, filename) {
loadAndCompile(filename)
loadAndCompile(filename, {}, module)
}

/**
* Render riot tags returing a strings
* @param { String } tagName - tag identifier
* @param { Object } opts - options to pass to the tag
* @returns { String } tag resulting template
*/
function render(tagName, opts) {
var tag = render.tag(tagName, opts),
html = sdom.serialize(tag.root)
Expand Down
20 changes: 18 additions & 2 deletions test/specs/server/index.js
Expand Up @@ -4,7 +4,7 @@ var glob = require('glob'),
expect = require('chai').expect,
cheerio = require('cheerio')

describe('Node/io.js', function() {
describe('Node', function() {

// adds custom riot parsers used by some tag/*.tag files
// css
Expand All @@ -20,7 +20,7 @@ describe('Node/io.js', function() {
glob('../../tag/*.tag', { cwd: __dirname }, function (err, tags) {
expect(err).to.be.equal(null)
tags.forEach(function(tag) {
if (tag[0] === '~') return
if (/~/.test(tag)) return
expect(require(tag)).to.be.ok
})
done()
Expand Down Expand Up @@ -130,6 +130,22 @@ describe('Node/io.js', function() {
expect(tag).to.be.equal('custom-parsers')
expect(tmpl).to.be.equal('<custom-parsers></custom-parsers>')

tag = riot.require(path.resolve(__dirname, '../../tag/~custom-parsers.tag'))
tmpl = riot.render('custom-parsers')

expect(tag).to.be.equal('custom-parsers')
expect(tmpl).to.be.equal('<custom-parsers><p>hi</p></custom-parsers>')

expect(require('../../../lib/server')).to.be.not.equal('custom-parsers')
})


it('Load tags containing nested require calls', function() {
var tag = require(path.resolve(__dirname, '../../tag/~import-tags.tag'))
var tmpl = riot.render('import-tags')

expect(tag).to.be.equal('import-tags')
expect(tmpl).to.have.length
})

})
4 changes: 4 additions & 0 deletions test/tag/~import-tags.tag
@@ -0,0 +1,4 @@
require('./timer.tag')
<import-tags>
<timer></timer>
</import-tags>

0 comments on commit 3104cb3

Please sign in to comment.