瀏覽代碼

support embedding scripts

Evan You 9 年之前
父節點
當前提交
8827406c86

+ 7 - 8
src/server/template-renderer/index.js

@@ -1,7 +1,5 @@
 /* @flow */
 /* @flow */
 
 
-// TODO: handle <script> tag embedding (so we can get rid of html-webpack-plugin)
-
 const serialize = require('serialize-javascript')
 const serialize = require('serialize-javascript')
 
 
 import TemplateStream from './template-stream'
 import TemplateStream from './template-stream'
@@ -73,8 +71,7 @@ export default class TemplateRenderer {
       template.neck +
       template.neck +
       content +
       content +
       this.renderState(context) +
       this.renderState(context) +
-      template.waist +
-      this.renderAsyncChunks(context) +
+      this.renderScripts(context) +
       template.tail
       template.tail
     )
     )
   }
   }
@@ -117,10 +114,12 @@ export default class TemplateRenderer {
       : ''
       : ''
   }
   }
 
 
-  renderAsyncChunks (context: Object): string {
-    const renderedFiles = this.getRenderedFilesFromContext(context)
-    if (renderedFiles) {
-      return renderedFiles.map(file => {
+  renderScripts (context: Object): string {
+    if (this.clientManifest) {
+      const initial = this.clientManifest.initial
+      const async = this.getRenderedFilesFromContext(context)
+      const needed = [initial[0]].concat(async || [], initial.slice(1))
+      return needed.map(file => {
         return `<script src="${this.publicPath}/${file}"></script>`
         return `<script src="${this.publicPath}/${file}"></script>`
       }).join('')
       }).join('')
     } else {
     } else {

+ 1 - 12
src/server/template-renderer/parse-template.js

@@ -3,7 +3,6 @@
 export type ParsedTemplate = {
 export type ParsedTemplate = {
   head: string;
   head: string;
   neck: string;
   neck: string;
-  waist: string;
   tail: string;
   tail: string;
 };
 };
 
 
@@ -29,19 +28,9 @@ export function parseTemplate (
     }
     }
   }
   }
 
 
-  let waist = ''
-  let tail = template.slice(j + contentPlaceholder.length)
-  let k = tail.indexOf('</script>')
-  if (k > 0) {
-    k += '</script>'.length
-    waist = tail.slice(0, k)
-    tail = tail.slice(k)
-  }
-
   return {
   return {
     head: template.slice(0, i),
     head: template.slice(0, i),
     neck: template.slice(i, j),
     neck: template.slice(i, j),
-    waist,
-    tail
+    tail: template.slice(j + contentPlaceholder.length)
   }
   }
 }
 }

+ 4 - 6
src/server/template-renderer/template-stream.js

@@ -65,12 +65,10 @@ export default class TemplateStream extends Transform {
       this.push(state)
       this.push(state)
     }
     }
 
 
-    this.push(this.template.waist)
-
-    // embed async chunks used in initial render
-    const asyncChunks = this.renderer.renderAsyncChunks(this.context)
-    if (asyncChunks) {
-      this.push(asyncChunks)
+    // embed scripts needed
+    const scripts = this.renderer.renderScripts(this.context)
+    if (scripts) {
+      this.push(scripts)
     }
     }
 
 
     this.push(this.template.tail)
     this.push(this.template.tail)