-
Notifications
You must be signed in to change notification settings - Fork 671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add renderToString method #435
Merged
Merged
Changes from 2 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
d35141e
feat(render): add render method
eddyerburgh 5528ec0
feat(renderToString): add docs and types
eddyerburgh b21d5ab
Throw error if not in node in renderToString
eddyerburgh 1daf59a
fix flow errors
eddyerburgh 35263aa
fix: change runningInNode check
eddyerburgh a098f06
fix flow error
eddyerburgh e3d9139
fix: remove isNode check
eddyerburgh decc61b
Fix flow error
eddyerburgh 74a14bf
fix karma load-test caing
eddyerburgh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,103 @@ | ||
# `renderToString(component {, options}])` | ||
|
||
- **Arguments:** | ||
|
||
- `{Component} component` | ||
- `{Object} options` | ||
- `{Object} context` | ||
- `{Array<Component|Object>|Component} children` | ||
- `{Object} slots` | ||
- `{Array<Componet|Object>|Component|String} default` | ||
- `{Array<Componet|Object>|Component|String} named` | ||
- `{Object} mocks` | ||
- `{Object|Array<string>} stubs` | ||
- `{Vue} localVue` | ||
|
||
- **Returns:** `{string}` | ||
|
||
- **Options:** | ||
|
||
See [options](./options.md) | ||
|
||
- **Usage:** | ||
|
||
Renders a component to HTML. | ||
|
||
`renderToString` uses [`vue-server-renderer`](https://ssr.vuejs.org/en/basic.html) under the hood, to render a component to HTML. | ||
|
||
**Without options:** | ||
|
||
```js | ||
import { renderToString } from '@vue/test-utils' | ||
import { expect } from 'chai' | ||
import Foo from './Foo.vue' | ||
|
||
describe('Foo', () => { | ||
it('renders a div', () => { | ||
const renderedString = renderToString(Foo) | ||
expect(renderedString).toContain('<div></div>') | ||
}) | ||
}) | ||
``` | ||
|
||
**With Vue options:** | ||
|
||
```js | ||
import { renderToString } from '@vue/test-utils' | ||
import { expect } from 'chai' | ||
import Foo from './Foo.vue' | ||
|
||
describe('Foo', () => { | ||
it('renders a div', () => { | ||
const renderedString = renderToString(Foo, { | ||
propsData: { | ||
color: 'red' | ||
} | ||
}) | ||
expect(renderedString).toContain('red') | ||
}) | ||
}) | ||
``` | ||
|
||
**Default and named slots:** | ||
|
||
```js | ||
import { renderToString } from '@vue/test-utils' | ||
import { expect } from 'chai' | ||
import Foo from './Foo.vue' | ||
import Bar from './Bar.vue' | ||
import FooBar from './FooBar.vue' | ||
|
||
describe('Foo', () => { | ||
it('renders a div', () => { | ||
const renderedString = renderToString(Foo, { | ||
slots: { | ||
default: [Bar, FooBar], | ||
fooBar: FooBar, // Will match <slot name="FooBar" />, | ||
foo: '<div />' | ||
} | ||
}) | ||
expect(renderedString).toContain('<div></div>') | ||
}) | ||
}) | ||
``` | ||
|
||
**Stubbing global properties:** | ||
|
||
```js | ||
import { renderToString } from '@vue/test-utils' | ||
import { expect } from 'chai' | ||
import Foo from './Foo.vue' | ||
|
||
describe('Foo', () => { | ||
it('renders a div', () => { | ||
const $route = { path: 'http://www.example-path.com' } | ||
const renderedString = renderToString(Foo, { | ||
mocks: { | ||
$route | ||
} | ||
}) | ||
expect(renderedString).toContain($route.path) | ||
}) | ||
}) | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
// @flow | ||
|
||
import Vue from 'vue' | ||
import createInstance from './lib/create-instance' | ||
import './lib/polyfills/object-assign-polyfill' | ||
import { throwError } from './lib/util' | ||
|
||
Vue.config.productionTip = false | ||
Vue.config.devtools = false | ||
|
||
export default function renderToString (component: Component, options: Options = {}): string { | ||
const renderer = require('vue-server-renderer').createRenderer() | ||
// Remove cached constructor | ||
delete component._Ctor | ||
|
||
if (options.attachToDocument) { | ||
throwError('you cannot use attachToDocument with renderToString') | ||
} | ||
|
||
const vm = createInstance(component, options) | ||
|
||
let renderedString = '' | ||
|
||
renderer.renderToString(vm, (err, res) => { | ||
if (err) { | ||
console.log(err) | ||
} | ||
renderedString = res | ||
}) | ||
return renderedString | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why
vue-server-renderer
gets imported using CommonJS and not ESM?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
ESM imports are hoisted to the top, so any ESM import will always be bundled.
vue-server-renderer relies on node modules, like path, which makes it difficult to compile with webpack. require is only run when the code runs, so putting it inside the renderToString means we only try and import vue-server-renderer when the renderToString function is run.
We should also add a warning that checks if the user is in node, and we can throw an error if they aren't to tell them renderToString needs to be run in node.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Clear explanation, thank you very much!