From ace40bfe7ffbd3ff5ab38445ef89a00610d81929 Mon Sep 17 00:00:00 2001 From: Blake Thomson Date: Thu, 11 Sep 2014 09:46:20 -0700 Subject: [PATCH 1/2] Translate import declarations into calls to require() in beforeInit. Import hrefs are assumed to be relative to the current file, and so the call to require has `./` prepended. e.g. require('./test.ract'); If a beforeInit callback was specified in the component, it will be called after the sub-components have been set into options. See the new test case for an example. --- index.js | 42 ++++++++++++++++++++--------------------- test/import.ract | 3 +++ test/import.ract-output | 20 ++++++++++++++++++++ test/simple.js | 10 +++++++++- test/test.ract-output | 4 +++- test/test2.ract-output | 6 ++++-- 6 files changed, 60 insertions(+), 25 deletions(-) create mode 100644 test/import.ract create mode 100644 test/import.ract-output diff --git a/index.js b/index.js index 687768e..d08350c 100644 --- a/index.js +++ b/index.js @@ -23,28 +23,28 @@ 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('options.components = {}') + component.imports.forEach(function(imp) { + script.push("options.components['" + imp.name + "'] = 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) @@ -52,4 +52,4 @@ module.exports = function(file, options) { } ) return stream -}; \ No newline at end of file +}; diff --git a/test/import.ract b/test/import.ract new file mode 100644 index 0000000..d73e228 --- /dev/null +++ b/test/import.ract @@ -0,0 +1,3 @@ + + +
diff --git a/test/import.ract-output b/test/import.ract-output new file mode 100644 index 0000000..0ad3419 --- /dev/null +++ b/test/import.ract-output @@ -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) { + +options.components = {} + +options.components['test'] = Ractive.extend(require('./test.ract')) + +__beforeInit && __beforeInit(options) + +} \ No newline at end of file diff --git a/test/simple.js b/test/simple.js index 5819e79..83a6424 100644 --- a/test/simple.js +++ b/test/simple.js @@ -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() }) -}) \ No newline at end of file +}) + +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() + }) +}) diff --git a/test/test.ract-output b/test/test.ract-output index 30d25e4..32d0944 100644 --- a/test/test.ract-output +++ b/test/test.ract-output @@ -1,4 +1,6 @@ -exports.template = { v:1, +var component = module + +component.exports.template = { v:1, t:[ "Hello ", { t:2, r:"world" }, diff --git a/test/test2.ract-output b/test/test2.ract-output index c4dd239..453a675 100644 --- a/test/test2.ract-output +++ b/test/test2.ract-output @@ -1,4 +1,6 @@ -exports.template = { v:1, +var component = module + +component.exports.template = { v:1, t:[ { t:7, e:"div", a:{ "class":"kickass" }, @@ -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" \ No newline at end of file +component.exports.css = "\ndiv.kickass {\n /* blue is pretty kickass */\n color: blue;\n}\n" \ No newline at end of file From dd5b70c4b9b1602665d4ebf730d2190de8d5f6de Mon Sep 17 00:00:00 2001 From: Blake Thomson Date: Thu, 11 Sep 2014 10:52:38 -0700 Subject: [PATCH 2/2] Do not override components passed into the constructor as options --- index.js | 5 +++-- test/import.ract-output | 4 ++-- 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/index.js b/index.js index d08350c..cf43ec3 100644 --- a/index.js +++ b/index.js @@ -37,9 +37,10 @@ module.exports = function(file, options) { if (component.imports.length > 0) { script.push('var __beforeInit = component.exports.beforeInit') script.push('component.exports.beforeInit = function(options) {') - script.push('options.components = {}') + script.push('if (!options.components) options.components = {}') component.imports.forEach(function(imp) { - script.push("options.components['" + imp.name + "'] = Ractive.extend(require('./" + imp.href + "'))") + var component = "options.components['" + imp.name + "']"; + script.push('if (!' + component + ') ' + component + " = Ractive.extend(require('./" + imp.href + "'))") }) script.push('__beforeInit && __beforeInit(options)') script.push('}') diff --git a/test/import.ract-output b/test/import.ract-output index 0ad3419..ea53020 100644 --- a/test/import.ract-output +++ b/test/import.ract-output @@ -11,9 +11,9 @@ var __beforeInit = component.exports.beforeInit component.exports.beforeInit = function(options) { -options.components = {} +if (!options.components) options.components = {} -options.components['test'] = Ractive.extend(require('./test.ract')) +if (!options.components['test']) options.components['test'] = Ractive.extend(require('./test.ract')) __beforeInit && __beforeInit(options)