diff --git a/lib/renderer/renderer.js b/lib/renderer/renderer.js index 693653c..95ea1bc 100644 --- a/lib/renderer/renderer.js +++ b/lib/renderer/renderer.js @@ -66,6 +66,7 @@ class Renderer { this.internalCache = new Utils.Cache(); this.head = options.head || {}; this.data = options.data || {}; + this.propsData = options.propsData || {}; this.template = options.template || {}; const version = Utils.VueVersion(options.vueVersion); if (version.enabled) { @@ -128,6 +129,14 @@ class Renderer { return mergedData; }; } + /** + * @param {Object} oldPropsData + * @param {Object} newPropsData + * @returns {Function} + */ + FixPropsData(oldPropsData, newPropsData) { + return Object.assign({}, oldPropsData, this.propsData, newPropsData); + } /** * @param {string} componentFile * @param {string} filePath @@ -396,15 +405,19 @@ class Renderer { * * @param {string} filePath * @param {Object} data + * @param {Object} vueOptions * @returns {Promise<{vue: object, css: string, script: string}>} */ - MakeVueClass(filePath, data) { + MakeVueClass(filePath, data, vueOptions = {}) { return new Promise((resolve, reject) => { let cachedBundle = this.internalCache.get(filePath); if (cachedBundle) { if (cachedBundle.bundle.data && typeof cachedBundle.bundle.data === "function") { cachedBundle.bundle.data = this.FixData(cachedBundle.bundle.data(), data); } + if (vueOptions.propsData && (cachedBundle.bundle.propsData || cachedBundle.bundle.props)) { + cachedBundle.bundle.propsData = this.FixPropsData(cachedBundle.bundle.propsData || {}, vueOptions.propsData); + } // @ts-ignore const vue = new Vue(cachedBundle.bundle); const cleanBundle = this._deleteCtor(cachedBundle.bundle); @@ -425,6 +438,10 @@ class Renderer { if (bundle.data && typeof bundle.data === "function") { bundle.data = this.FixData(bundle.data(), data); } + //Insert propsData + if (vueOptions.propsData && (bundle.propsData || bundle.props)) { + bundle.propsData = this.FixPropsData(bundle.propsData || {}, vueOptions.propsData); + } //Create Vue Class // @ts-ignore @@ -501,7 +518,7 @@ class Renderer { return new Promise((resolve, reject) => { this.FindFile(vueFile) .then(filePath => { - this.MakeVueClass(filePath, data) + this.MakeVueClass(filePath, data, vueOptions) .then(vueClass => { const mergedHeadObject = Utils.MergeHead(vueOptions.head, this.head); const template = Object.assign({}, this.template, vueOptions.template); @@ -536,7 +553,7 @@ class Renderer { return new Promise((resolve, reject) => { this.FindFile(vueFile) .then(filePath => { - this.MakeVueClass(filePath, data) + this.MakeVueClass(filePath, data, vueOptions) .then(vueClass => { const mergedHeadObject = Utils.MergeHead(vueOptions.head, this.head); const headString = Utils.BuildHead(mergedHeadObject); diff --git a/package.json b/package.json index c83a4de..fa66680 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "vue-pronto", - "version": "1.7.0", + "version": "1.7.1", "description": "Seriously fast vue server renderer", "main": "lib/index.js", "files": [ diff --git a/tests/example/views/index/index-with-props.vue b/tests/example/views/index/index-with-props.vue new file mode 100644 index 0000000..842f90b --- /dev/null +++ b/tests/example/views/index/index-with-props.vue @@ -0,0 +1,51 @@ + + + + + diff --git a/tests/renderer.js b/tests/renderer.js index 2f8b7b9..eb4b78e 100644 --- a/tests/renderer.js +++ b/tests/renderer.js @@ -4,6 +4,7 @@ const path = require("path"); const Pronto = require("../lib"); const vueFile = path.join("index/index.vue"); +const vueFileWithProps = path.join("index/index-with-props.vue"); const rootPath = path.normalize(path.join(__dirname, "../tests/example/views")); //@ts-ignore @@ -106,6 +107,22 @@ test("String returns with no object", t => { }); }); + +//@ts-ignore +test("String returns with propsData", t => { + // @ts-ignore + const renderer = new Pronto({ rootPath: rootPath }); + const expected = `

Hello world!

Hello from component

Hello from subcomponent

Welcome to the demo. Click a link:

import fooTransitionExpand from '@foo/styles-animation/src/components/foo-transition-expand.vue';

const test = require("foo.vue");

const bar = require("bar.vue");

Say Foo

    Hello From Component in node_modules

    `; + return renderer + .RenderToString(vueFileWithProps, {}, {propsData: {}}) + .then(rendered => { + t.is(rendered.replace(/([\[|\s|\"])data-v-(.*)([\]|\>|\"])/gm, function(match, p1, p2, p3) { return p1 + 'data-v-123456' + p3; }), expected.replace(/([\[|\s|\"])data-v-(.*)([\]|\>|\"])/gm, function(match, p1, p2, p3) { return p1 + 'data-v-123456' + p3; })); + }) + .catch(error => { + t.fail(error); + }); +}); + //@ts-ignore test.cb("Stream returns with full object", t => { // @ts-ignore @@ -158,3 +175,36 @@ test.cb("Stream returns with no object", t => { t.fail(error); }); }); + +//@ts-ignore +test.cb("Stream returns with full propsData", t => { + // @ts-ignore + const propsData = { + bar: true, + fakehtml: '

    FAKEHTML

    ' + }; + const renderer = new Pronto({ rootPath: rootPath, propsData: propsData }); + + const templateLiteral = `\n\n\n{{title}}\n\n\n\n

    FOOOOO

    \n\n\n`; + + const vueOptions = { + head: {}, + template: templateLiteral + }; + const expected = `

    Hello world!

    Hello from component

    Hello from subcomponent

    true

    FAKEHTML

    Welcome to the demo. Click a link:

    import fooTransitionExpand from '@foo/styles-animation/src/components/foo-transition-expand.vue';

    const test = require("foo.vue");

    const bar = require("bar.vue");

    Say Foo

      Hello From Component in node_modules

      `; + // @ts-ignore + renderer + .RenderToStream(vueFileWithProps, {}, vueOptions) + .then(stream => { + let rendered = ""; + stream.on("data", chunk => (rendered += chunk)); + stream.on("end", () => { + t.is(rendered.replace(/([\[|\s|\"])data-v-(.*)([\]|\>|\"])/gm, function(match, p1, p2, p3) { return p1 + 'data-v-123456' + p3; }), expected.replace(/([\[|\s|\"])data-v-(.*)([\]|\>|\"])/gm, function(match, p1, p2, p3) { return p1 + 'data-v-123456' + p3; })); + t.end(); + }); + }) + .catch(error => { + t.fail(error); + }); +}); +