-
-
Notifications
You must be signed in to change notification settings - Fork 8.3k
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
fix(server-renderer): respect compilerOptions during runtime template compilation #4631
Merged
Merged
Changes from 1 commit
Commits
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
92 changes: 92 additions & 0 deletions
92
packages/server-renderer/__tests__/ssrCompilerOptions.spec.ts
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,92 @@ | ||
/** | ||
* @jest-environment node | ||
*/ | ||
|
||
import { createApp } from 'vue' | ||
import { renderToString } from '../src/renderToString' | ||
|
||
describe('ssr: compiler options', () => { | ||
test('config.isCustomElement (deprecated)', async () => { | ||
const app = createApp({ | ||
template: `<div><x-button/></div>` | ||
}) | ||
app.config.isCustomElement = tag => tag.startsWith('x-') | ||
expect(await renderToString(app)).toBe(`<div><x-button></x-button></div>`) | ||
}) | ||
|
||
test('config.compilerOptions.isCustomElement', async () => { | ||
const app = createApp({ | ||
template: `<div><x-panel/></div>` | ||
}) | ||
app.config.compilerOptions.isCustomElement = tag => tag.startsWith('x-') | ||
expect(await renderToString(app)).toBe(`<div><x-panel></x-panel></div>`) | ||
}) | ||
|
||
test('component.compilerOptions.isCustomElement', async () => { | ||
const app = createApp({ | ||
template: `<div><x-card/><y-child/></div>`, | ||
compilerOptions: { | ||
isCustomElement: (tag: string) => tag.startsWith('x-') | ||
}, | ||
components: { | ||
YChild: { | ||
template: `<div><y-button/></div>` | ||
} | ||
} | ||
}) | ||
app.config.compilerOptions.isCustomElement = tag => tag.startsWith('y-') | ||
expect(await renderToString(app)).toBe( | ||
`<div><x-card></x-card><div><y-button></y-button></div></div>` | ||
) | ||
}) | ||
|
||
test('component.delimiters (deprecated)', async () => { | ||
const app = createApp({ | ||
template: `<div>[[ 1 + 1 ]]</div>`, | ||
delimiters: ['[[', ']]'] | ||
}) | ||
expect(await renderToString(app)).toBe(`<div>2</div>`) | ||
}) | ||
|
||
test('config.compilerOptions.delimiters', async () => { | ||
const app = createApp({ | ||
template: `<div>[[ 1 + 1 ]]</div>` | ||
}) | ||
app.config.compilerOptions.delimiters = ['[[', ']]'] | ||
expect(await renderToString(app)).toBe(`<div>2</div>`) | ||
}) | ||
|
||
test('component.compilerOptions.delimiters', async () => { | ||
const app = createApp({ | ||
template: `<div>[[ 1 + 1 ]]<ChildComponent/></div>`, | ||
compilerOptions: { | ||
delimiters: ['[[', ']]'] | ||
}, | ||
components: { | ||
ChildComponent: { | ||
template: `<div>(( 2 + 2 ))</div>` | ||
} | ||
} | ||
}) | ||
app.config.compilerOptions.delimiters = ['((', '))'] | ||
expect(await renderToString(app)).toBe(`<div>2<div>4</div></div>`) | ||
}) | ||
|
||
test('compilerOptions.whitespace', async () => { | ||
const app = createApp({ | ||
template: `<div><span>Hello world</span><ChildComponent/></div>`, | ||
compilerOptions: { | ||
whitespace: 'condense' | ||
}, | ||
components: { | ||
ChildComponent: { | ||
template: `<span>Hello world</span>` | ||
} | ||
} | ||
}) | ||
app.config.compilerOptions.whitespace = 'preserve' | ||
expect(await renderToString(app)).toBe( | ||
`<div><span>Hello world</span><span>Hello world</span></div>` | ||
) | ||
}) | ||
}) |
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
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.
The runtime compiler options are limited and largely serializable. We can call
fn.toString()
ifisCustomElement
is present, andisNativeTag
is in fact constant sinceserver-renderer
impliesruntime-dom
.So we can resolve the options first, serialize it to get a cache key that takes options into account.
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.
OK, I think that should be working now.
There are some potential edge cases around using
fn.toString()
, e.g. if someone uses a utility function to createisCustomElement
functions they will likely have the sametoString
but different values caught in their closures. This probably isn't worth worrying about given the templates also have to be a perfect match. If someone did encounter that extreme edge case, it should be possible for them to work around it by passing a cache-buster in thecompilerOptions
.