Browse Source

feat: make vue and basic server renderer compatible in pure js runtimes

Evan You 8 years ago
parent
commit
c5d0fa0503

+ 1 - 1
src/platforms/web/runtime/index.js

@@ -44,7 +44,7 @@ Vue.prototype.$mount = function (
 
 // devtools global hook
 /* istanbul ignore next */
-setTimeout(() => {
+Vue.nextTick(() => {
   if (config.devtools) {
     if (devtools) {
       devtools.emit('init', Vue)

+ 5 - 3
src/platforms/web/runtime/transition-util.js

@@ -58,9 +58,11 @@ if (hasTransition) {
 }
 
 // binding to window is necessary to make hot reload work in IE in strict mode
-const raf = inBrowser && window.requestAnimationFrame
-  ? window.requestAnimationFrame.bind(window)
-  : setTimeout
+const raf = inBrowser
+  ? window.requestAnimationFrame
+    ? window.requestAnimationFrame.bind(window)
+    : setTimeout
+  : /* istanbul ignore next */ fn => fn()
 
 export function nextFrame (fn: Function) {
   raf(() => {

+ 18 - 1
src/server/write.js

@@ -1,6 +1,23 @@
 /* @flow */
 
 const MAX_STACK_DEPTH = 1000
+const noop = _ => _
+
+const defer = typeof process !== 'undefined' && process.nextTick
+  ? process.nextTick
+  : typeof Promise !== 'undefined'
+    ? fn => Promise.resolve().then(fn)
+    : typeof setTimeout !== 'undefined'
+      ? setTimeout
+      : noop
+
+if (defer === noop) {
+  throw new Error(
+    'Your JavaScript runtime does not support any asynchronous primitives ' +
+    'that are required by vue-server-renderer. Please use a polyfill for ' +
+    'either Promise or setTimeout.'
+  )
+}
 
 export function createWriteFunction (
   write: (text: string, next: Function) => boolean,
@@ -14,7 +31,7 @@ export function createWriteFunction (
     const waitForNext = write(text, next)
     if (waitForNext !== true) {
       if (stackDepth >= MAX_STACK_DEPTH) {
-        process.nextTick(() => {
+        defer(() => {
           try { next() } catch (e) {
             onError(e)
           }