Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 22 additions & 21 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,33 +23,34 @@ module.exports = function(file, options) {
try {
var component = rcu.parse(source)

var script
var script = ['var component = module']

if (component.script) {
script = [
'var component = module',
component.script
]
if (component.template) {
script.push('component.exports.template = '+toSource(component.template))
}
if (component.css) {
script.push('component.exports.css = '+toSource(component.css))
}
this.queue(script.join('\n\n'))
} else {
script = []
script.push('exports.template = '+toSource(component.template))
if (component.css) {
script.push('exports.css = '+toSource(component.css))
}
this.queue(script.join('\n\n'))
script.push(component.script)
}

if (component.template) {
script.push('component.exports.template = '+toSource(component.template))
}
if (component.css) {
script.push('component.exports.css = '+toSource(component.css))
}
if (component.imports.length > 0) {
script.push('var __beforeInit = component.exports.beforeInit')
script.push('component.exports.beforeInit = function(options) {')
script.push('if (!options.components) options.components = {}')
component.imports.forEach(function(imp) {
var component = "options.components['" + imp.name + "']";
script.push('if (!' + component + ') ' + component + " = Ractive.extend(require('./" + imp.href + "'))")
})
script.push('__beforeInit && __beforeInit(options)')
script.push('}')
}
this.queue(script.join('\n\n'))
this.queue(null)
} catch (ex) {
stream.emit('error', ex)
}
}
)
return stream
};
};
3 changes: 3 additions & 0 deletions test/import.ract
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<link rel="ractive" href="test.ract" />

<div class="parent-component"><test></test></div>
20 changes: 20 additions & 0 deletions test/import.ract-output
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
var component = module

component.exports.template = { v:1,
t:[ { t:7,
e:"div",
a:{ "class":"parent-component" },
f:[ { t:7,
e:"test" } ] } ] }

var __beforeInit = component.exports.beforeInit

component.exports.beforeInit = function(options) {

if (!options.components) options.components = {}

if (!options.components['test']) options.components['test'] = Ractive.extend(require('./test.ract'))

__beforeInit && __beforeInit(options)

}
10 changes: 9 additions & 1 deletion test/simple.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,12 @@ test('bad.ract', function(done) {
assert.equal(error.toString(), 'ParseError: Expected closing delimiter \'}}\' after reference at line 1 character 12:\n{{#inverse Unexpected\n ^----')
done()
})
})
})

test('import.ract', function(done) {
getTransformedOutput(__dirname+"/import.ract", function(error, output) {
assert.ifError(error)
assert.equal(output, fs.readFileSync('test/import.ract-output', 'utf8'))
done()
})
})
4 changes: 3 additions & 1 deletion test/test.ract-output
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
exports.template = { v:1,
var component = module

component.exports.template = { v:1,
t:[ "Hello ",
{ t:2,
r:"world" },
Expand Down
6 changes: 4 additions & 2 deletions test/test2.ract-output
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
exports.template = { v:1,
var component = module

component.exports.template = { v:1,
t:[ { t:7,
e:"div",
a:{ "class":"kickass" },
Expand All @@ -7,4 +9,4 @@ exports.template = { v:1,
r:"styles" },
" only." ] } ] }

exports.css = "\ndiv.kickass {\n /* blue is pretty kickass */\n color: blue;\n}\n"
component.exports.css = "\ndiv.kickass {\n /* blue is pretty kickass */\n color: blue;\n}\n"