DIV CSS 佈局教程網

requirejs + vue 專案建置實戰詳解
編輯:關於JavaScript     

以前一直是司徒正美的支持者,畢竟當年我們也是跟著他一起走進前端世界的。所以一般做 MVVM 都習慣用 avalon,一方面也是考量到專案得支援 IE6、7、8。使用過程中難免遇到一些小坑和 bug,但都一一處理掉了。今年正美正好把 avalon 升級到 2.0,加入虛擬 DOM 之後反而不太穩定,於是我開始物色其他的 MVVM 框架。

看過幾個比較流行的框架後,最後我選了 Vue。原因很簡單:文檔齊全,而且有中文版(你懂的);生態圈成熟,有 vux、vue-loader、vue-router;元件化設計也做得很到位。

建置專案時,我主要是透過 requirejs 來載入 JS 模組(還沒接觸 webpack,過去都是 avalon + requirejs,算是慣性思維吧)。下面就把這段心路歷程寫出來。

方案一:寫一個 requirejs 外掛來載入 vue 元件檔

失敗。vue 元件檔含有 template、script、style 三種標籤,得靠 requirejs 讀取檔案字串、再用正則分析轉成 JS,有點捨近求遠;而且這種方式只能做同網域的 ajax 請求。

方案二:寫一個 gulp 外掛,把 vue 檔轉成能用 requirejs 載入的 JS 檔

這個方案可行。只是 template、script、style 的資訊需要用正則比對,再動態注入到當前文檔中,只是有些公用方法被頻繁呼叫。於是又加了一個 vueComment 檔,把動態注入的方法整理在一起:

 define(['Vue'], function (Vue) { Vue.appendHTML = function (text) { document.body.insertAdjacentHTML('beforeEnd', text); }; var style; var doc = document; Vue.appendCSS = function (text) { text = text + " "; if (!style) { var head = doc.getElementsByTagName("head")[0]; var elms = head.getElementsByTagName("style"); if (elms.length == 0) { if (doc.createStyleSheet) { doc.createStyleSheet(); } else { var tmp = doc.createElement('style'); tmp.setAttribute("type", "text/css"); head.appendChild(tmp); } elms[0].setAttribute("media", "screen"); } style = elms[0]; } if (style.styleSheet) { style.styleSheet.cssText += text; } else if(doc.getBoxObjectFor) { style.innerHTML += text; } else { style.appendChild(doc.createTextNode(text)) } }; }); 

gulp-vue 的 Node.js 主要程式碼如下,透過檔名來決定元件名稱:

 var through = require('through2'); var gutil = require('gulp-util'); var regTpl = /<template>([\s\S]+?)<\/template>/; var regStyle = /<style>([\s\S]+?)<\/style>/; var regJs = /<script>([\s\S]+?)<\/script>/; var reg = [/'/g, /\n/g, /([^\\]+)\.vue$/]; var vueWrite = function (file, str) { var match = file.path.match(reg[2]); var id = "vue-tpl-" + match[1]; var appendJs = ""; var res = ""; str = str.replace(regTpl, function (t, h) { appendJs += "\tVue.appendHTML(\n'<template id=\"" + id + "\">" + h.replace(reg[0], "\\'").replace(reg[1], "\\\n") + "<\/template>');\n"; return ""; }).replace(regStyle, function (t, h) { appendJs += "\tVue.appendCSS(\n'" + h.replace(reg[0], "\\'").trim().replace(reg[1], "\\\n") + "');\n" return ""; }).replace(regJs, function (t, h) { res = "define(function (require) {\n\trequire('VueCommon'); \n\tvar Vue = require('Vue');\n\tvar exports;\n" + appendJs + h + ";\n\texports.template = '#" + id + "';\n\texports = Vue.extend(exports);\n\tVue.component('" + match[1] + "', exports);\n\treturn exports;\n});" return ; }) return res; }; module.exports = function(opt){ function run (file, encoding, callback) { if (file.isNull()) { return callback(null, file); } if (file.isStream()) { return callback(new gutil.PluginError('gulp-vue', 'doesn\'t support Streams')); } file.contents = new Buffer(vueWrite(file, file.contents.toString())); file.path = file.path + '.js'; callback(null, file); } return through.obj(run); } 
XML學習教程| jQuery入門知識| AJAX入門| Dreamweaver教程| Fireworks入門知識| SEO技巧| SEO優化集錦|
Copyright © DIV+CSS佈局教程網 All Rights Reserved