Skip to content

Instantly share code, notes, and snippets.

@Akryum
Last active July 3, 2018 16:16
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Akryum/dd99707adf9340e70924ef85ffd568fb to your computer and use it in GitHub Desktop.
Save Akryum/dd99707adf9340e70924ef85ffd568fb to your computer and use it in GitHub Desktop.
vue-cli SSR
// Existing imports
import Vue from 'vue'
import router from './router'
import store from './store'
// Other existing code here
// Add 'app' variable
const app = new Vue({
// Existing options
router,
store,
provide: apolloProvider.provide(),
})
// Existing imports
import Vue from 'vue'
// Transform router import
import { createRouter } from './router'
// Transform store import
import { createStore } from './store'
// Inject import
import nuxt from 'nuxt'
// Generate createApp func
export default function createApp(context) {
// Generate createAppContext
const appContext = new nuxt.createAppContext(context)
// Generate create statements
const router = createRouter(context)
const store = createStore(context)
// Other existing code here
// Add 'app' variable
const app = new Vue({
// Existing options
router,
store,
provide: apolloProvider.provide(),
// Inject root option
...appContext.rootOptions()
})
// Detect router, vuex, ...
// Used in servery entry for pre-fetching
appContext.use({
router,
store,
apolloProvider
})
// Generate return
return {
app,
appContext
}
}
// Existing imports
import Vue from 'vue'
import VueRouter from 'vue-router'
// Existing Vue.use
Vue.use(VueRouter)
const router = new VueRouter({
// Existing options
})
export default router
// Existing imports
import Vue from 'vue'
import VueRouter from 'vue-router'
// Existing Vue.use
Vue.use(VueRouter)
// Wrap in function
export function createRouter (context) {
const router = new VueRouter({
// Existing options
})
// Generate return
return router
}
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
const store = new Vuex.Store({
// Existing options
})
export default store
import Vue from 'vue'
import Vuex from 'vuex'
Vue.use(Vuex)
export function createStore (context) {
const store = new Vuex.Store({
// Existing options
})
return store
}
@Akryum
Copy link
Author

Akryum commented Jul 3, 2018

@Atinux it seems doable to me with recast. createAppContext may run nuxt plugins.

@Akryum
Copy link
Author

Akryum commented Jul 3, 2018

We would need:

  • wrapInExportedFunction - takes all the non-imports and non-Vue.use() code and put it in a function with the const appContext = new nuxt.createAppContext(context) statement at the beginning and the return { app, appContext } statement at the end.
  • assignToVariable - adds const app = before new Vue().
  • generateAppContextUse - detects router, store, etc. in the scope and generates the appContext.use() statement after the new Vue statement.

@Akryum
Copy link
Author

Akryum commented Jul 3, 2018

And we would need to change the router as well:

  • replace import router from 'XXX' with import { createRouter } from 'XXX'
  • modify the file targeted by the above import with wrapInExportedFunction to generate the createRouter function.

Same for store.

@Akryum
Copy link
Author

Akryum commented Jul 3, 2018

Also we could modify every vuex module to change state to a function.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment