Skip to content

Commit

Permalink
migration: remove use of filters + set compiler to v3 mode
Browse files Browse the repository at this point in the history
  • Loading branch information
yyx990803 committed May 5, 2021
1 parent 14f6f18 commit b05d955
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 13 deletions.
6 changes: 0 additions & 6 deletions src/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,10 @@ import { createStore } from './store'
import { createRouter } from './router'
import { sync } from 'vuex-router-sync'
import titleMixin from './util/title'
import * as filters from './util/filters'

// mixin for handling title
Vue.mixin(titleMixin)

// register global utility filters.
Object.keys(filters).forEach(key => {
Vue.filter(key, filters[key])
})

// Expose a factory function that creates a fresh set of store, router,
// app instances on each call (which is called for each SSR request)
export function createApp () {
Expand Down
5 changes: 4 additions & 1 deletion src/components/Comment.vue
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
<li v-if="comment" class="comment">
<div class="by">
<router-link :to="'/user/' + comment.by">{{ comment.by }}</router-link>
{{ comment.time | timeAgo }} ago
{{ timeAgo(comment.time) }} ago

This comment has been minimized.

Copy link
@asadbr

asadbr Jul 8, 2021

Thanks Evan
This ({{ timeAgo(comment.time) }}) is clearer than it {{ comment.time | timeAgo }} for a new vue js developers

This comment has been minimized.

Copy link
@HiZhaoxiaoyang

HiZhaoxiaoyang Oct 4, 2021

it's more like a format function in methods actually.

This comment has been minimized.

Copy link
@shenggaowei

shenggaowei Aug 12, 2022

the code readability is better

</div>
<div class="text" v-html="comment.text"></div>
<div class="toggle" :class="{ open }" v-if="comment.kids && comment.kids.length">
Expand All @@ -19,6 +19,8 @@
</template>

<script>
import { timeAgo } from '../util/filters'
export default {
name: 'comment',
props: ['id'],
Expand All @@ -33,6 +35,7 @@ export default {
}
},
methods: {
timeAgo,
pluralize: n => n + (n === 1 ? ' reply' : ' replies')
}
}
Expand Down
10 changes: 7 additions & 3 deletions src/components/Item.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<span class="title">
<template v-if="item.url">
<a :href="item.url" target="_blank" rel="noopener">{{ item.title }}</a>
<span class="host"> ({{ item.url | host }})</span>
<span class="host"> ({{ host(item.url) }})</span>
</template>
<template v-else>
<router-link :to="'/item/' + item.id">{{ item.title }}</router-link>
Expand All @@ -16,7 +16,7 @@
by <router-link :to="'/user/' + item.by">{{ item.by }}</router-link>
</span>
<span class="time">
{{ item.time | timeAgo }} ago
{{ timeAgo(item.time) }} ago
</span>
<span v-if="item.type !== 'job'" class="comments-link">
| <router-link :to="'/item/' + item.id">{{ item.descendants }} comments</router-link>
Expand All @@ -27,10 +27,14 @@
</template>

<script>
import { timeAgo } from '../util/filters'
import { timeAgo, host } from '../util/filters'
export default {
name: 'news-item',
methods: {
timeAgo,
host
},
props: ['item'],
// http://ssr.vuejs.org/en/caching.html#component-level-caching
serverCacheKey: ({ item: { id, __lastUpdated, time }}) => {
Expand Down
7 changes: 5 additions & 2 deletions src/views/ItemView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
<h1>{{ item.title }}</h1>
</a>
<span v-if="item.url" class="host">
({{ item.url | host }})
({{ host(item.url) }})
</span>
<p class="meta">
{{ item.score }} points
| by <router-link :to="'/user/' + item.by">{{ item.by }}</router-link>
{{ item.time | timeAgo }} ago
{{ timeAgo(item.time) }} ago
</p>
</div>
<div class="item-view-comments">
Expand All @@ -30,6 +30,7 @@
<script>
import Spinner from '../components/Spinner.vue'
import Comment from '../components/Comment.vue'
import { host, timeAgo } from '../util/filters'
export default {
name: 'item-view',
Expand Down Expand Up @@ -67,6 +68,8 @@ export default {
},
methods: {
host,
timeAgo,
fetchComments () {
if (!this.item || !this.item.kids) {
return
Expand Down
2 changes: 1 addition & 1 deletion vite.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export default defineConfig({
template: {
compilerOptions: {
compatConfig: {
MODE: 2
MODE: 3
}
}
}
Expand Down

1 comment on commit b05d955

@yyx990803
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After switching to the migration build, you will see both runtime warnings (in the browser console) and compiler warnings (in the command line). It's recommended to fix the compiler warnings first (some of them may be breaking the application).

In this project, filters are the only compiler-specific feature that needs fixing. After fixing all the compiler deprecation warnings, we can set the compiler to use Vue 3 behavior by default. Note the compatConfig option should still be kept until the full migration is complete.

Please sign in to comment.