requirejs + vue 项目搭建2
上篇是年后的项目搭建的,时间比较仓促,感觉有点low
1.gulp-vue 文件对公用js的有依赖,以后别的同事拿去搭其他项目,估计会被喷
2.不支持vue-loader一样写模版语言和es6语法
最近h5端的项目,用了webpack+vue-router,用jade+es6+stylus瞬间感觉自己高大上了,es6用起来,感觉也是爽爽哒。(其实语法用的也不多,也就import,一些简单的新方法,主要是箭头函数,再也不用self=this了)所以考虑进行一次升级,使在web端可以简单支持vue require的加载,因为webpack打包配置还是太麻烦了
vue的支持,template html字符串,我们可以通过模版nodejs模版编译生成html字符串,转成eqport的template属性进行支持.想了各种方式,没有实现css的支持,有啥好像方法,可以留言交流
下面是gulp插件的代码,参照的vue-loader
var through = require('through2');
var gutil = require('gulp-util');
var parse5 = require('parse5');
var deindent = require('de-indent');
var validateTemplate = require('vue-template-validator');
var jade = require('jade');
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);
}
var getHTML = {//暂时只做了jade模版的支持,需要别的模版对该对象进行扩展
'jade' : function (content) {
return jade.compile(content, {})({})
},
'default': function (content) {
return content;
}
};
var splitRE = /\r?\n/g
var emptyRE = /^\s*$/
var commentSymbols = {
'iced': '#',
'iced-jsx': '#',
'iced-redux': '#',
'coffee': '#',
'coffee-jsx': '#',
'coffee-redux': '#',
'purs': '--',
'ulmus': '--'
}
var vueWrite = function (file, content) {
var output = {
template: [],
style: [],
script: []
}
var fragment = parse5.parseFragment(content, {
locationInfo: true
});
fragment.childNodes.forEach(function (node) {
var type = node.tagName
var lang = (getAttribute(node, 'lang') || 'default').toLowerCase();
var warnings = null
if (!output[type]) {
return
}
// node count check
if ((type === 'script' || type === 'template') && output[type].length > 0) {
throw new Error(
'[glup-vue] Only one <script> or <template> tag is allowed inside a Vue component.'
)
}
// skip empty script/style tags
if (type !== 'template' && (!node.childNodes || !node.childNodes.length)) {
return
}
// template content is nested inside the content fragment
if (type === 'template') {
node = node.content
if (!lang) {
warnings = validateTemplate(node, content)
}
}
// extract part
var start = node.childNodes[0].__location.startOffset
var end = node.childNodes[node.childNodes.length - 1].__location.endOffset
var result
if (type === 'script') {
//将非script的内容进行当行注释,保持原文件行数,方便js错误查看
result = commentScript(content.slice(0, start), lang) +
deindent(content.slice(start, end)) +
commentScript(content.slice(end), lang)
} else {
result = deindent(content.slice(start, end))
}
output[type] = {
lang: lang,
content: result,
warnings: warnings
}
})
var lang = output.template.lang;
if (!getHTML[lang]) {
throw new Error(
'[glup-vue] ' + lang + ' html engine not support'
)
}
/*
return output.script.content
+ "\n;exports.default.template = '" + getHTML[lang](output.template.content, {}).replace(/(\\*)'/g, function (a, b) {
return (b||"").replace('\\', '\\\\') + "\\'"
}).replace(/\n/g, '\\\n') + "'";
*/
//try {
/*return output.script.content
+ "\n;exports.default.template = '" + getHTML[lang](output.template.content, {}).replace(/(\\*)'/g, function (a, b) {
return (b||"").replace('\\', '\\\\') + "\\'"
}).replace(/\n/g, '\\\n') + "'";*/
//对生成的html,'号进行替换,没有做多测试案例,可能有点小bug
return output.script.content
+ "\n;exports.default.template = '" + getHTML[lang](output.template.content, {}).replace(/(\\*)'/g, function (a, b) {
return (b||"").replace('\\', '\\\\') + "\\'"
}).replace(/\n/g, '\\\n') + "';";
//} catch (e) {
//console.log('message', e.message);
//}
}
function commentScript (content, lang) {
var symbol = getCommentSymbol(lang)
var lines = content.split(splitRE)
return lines.map(function (line, index) {
// preserve EOL
if (index === lines.length - 1 && emptyRE.test(line)) {
return ''
} else {
return symbol + (emptyRE.test(line) ? '' : ' ' + line)
}
})
.join('\n')
}
function getCommentSymbol (lang) {
return commentSymbols[lang] || '//'
}
function getAttribute (node, name) {
if (node.attrs) {
var i = node.attrs.length
var attr
while (i--) {
attr = node.attrs[i]
if (attr.name === name) {
return attr.value
}
}
}
}
本来想把requirejs加载的支持代码拼接在上面,但是测试发现,拼接后babel语法报错了,所以把这块逻辑放到了gulp构建文件里面,并加入了sourcemaps支持
var gulp = require('gulp');
var vuefile = require("./gulp-vue");
var sourcemaps = require('gulp-sourcemaps');
var babel = require('gulp-babel');
var inject = require("gulp-inject-string");
gulp.task('default', () => {
return gulp.src('src/js/**/*.vue')
.pipe(sourcemaps.init())
.pipe(vuefile())
.pipe(babel({
presets: ['es2015']
}))
.pipe(inject.wrap('define(function (require) {var exports = {};', ';return exports.default;\n});'))
.pipe(sourcemaps.write('.'))
.pipe(gulp.dest('dist/js/'));
});
这样我们就可以用requirejs+vue搭建项目了,这个方式缺陷就是css需要通过别的方式加载,也不支持写在vue文件当中。基于这个,我就可以修改我的 electron + vue项目了
requirejs + vue 项目搭建2的更多相关文章
- requirejs + vue 项目搭建
以前都是支持 司徒正美 的,毕竟咱们也是跟着 司徒正美 一起走进了前端的世界.所以一般MVVM都是用avalon的,当然也是考虑到项目需要支持IE6,7,8的考虑.当然在用的时候也有一些小坑和bug, ...
- Vue项目搭建完整剖析全过程
Vue项目搭建完整剖析全过程 项目源码地址:https://github.com/ballyalex 有帮助的话就加个星星呗~! 项目技术栈:vue+webpack+bower+sass+axios ...
- Vue项目搭建与部署
Vue项目搭建与部署 一,介绍与需求 1.1,介绍 Vue 是一套用于构建用户界面的渐进式框架.与其它大型框架不同的是,Vue 被设计为可以自底向上逐层应用.Vue两大核心思想:组件化和数据驱动.组 ...
- vue项目搭建 (二) axios 封装篇
vue项目搭建 (二) axios 封装篇 项目布局 vue-cli构建初始项目后,在src中进行增删修改 // 此处是模仿github上 bailicangdu 的 ├── src | ├── ap ...
- vue项目搭建 (一)
vue项目搭建 (一) 由于一直想要有自己的框架,因而一直在尝试搭建各类结构,结合vue官网及git上大神bailicangdu的项目,再看看网上一些意见,及个人思考,总结的一些,不到之处希望大家可以 ...
- Vue项目搭建流程 以及 目录结构构建
Vue项目搭建流程 以及 目录结构构建 一个小的Vue项目, 基于微信浏览器的移动端, 做了这么多的练习项目, 这一次准备记录下构建的过程, 以方便以后的调高效率 环境准备 操作系统 我的 windo ...
- vue项目搭建介绍01
目录 vue项目搭建介绍01 vue 项目框架环境搭建: 创建项目: vue 项目创建流程: vue项目搭建介绍01 vue 项目框架环境搭建: vue 项目框架: vue django(类似)(vu ...
- vue项目搭建介绍02
目录 vue项目搭建介绍02 python-pycharm设置: vue创建项目分类: vue-cli构建 自定义构建 基础的vue项目目录: vue项目搭建介绍02 python-pycharm设置 ...
- Vue项目搭建
1.环境搭建 安装node 官网下载安装包,傻瓜式安装:https://nodejs.org/zh-cn/ 安装cnpm npm install -g cnpm --registry=https:// ...
随机推荐
- REST总结
REST是Roy Thomas Fielding博士于2000年在他的博士论文中阐述的一种架构风格和设计原则.REST并非一种协议或者标准,事实上它只是阐述了HTTP协议的设计初衷:现在HTTP在网络 ...
- 求double类型的n次方
剑指offer系列面试题 package com.study; /* * 数值的整数次方 * 要求:实现函数 double Power(double base, int exponent) 求base ...
- C语言基础06
函数: 一组特定功能的代码段,之所以使用函数,为了在文件多处需要同一段代码时可以多次重复利用,减少代码冗余. //函数的声明 返回值类型 函数名称 ( 数据类型 形参1,数据类型 ,形参2 ) ; / ...
- TensorFlow 深度学习笔记 卷积神经网络
Convolutional Networks 转载请注明作者:梦里风林 Github工程地址:https://github.com/ahangchen/GDLnotes 欢迎star,有问题可以到Is ...
- U盘读写速度测试
1.ATTO Disk Benchmark 测U盘读写速度 ATTO Disk Benchmark 是一款简单易用的磁盘传输速率检测软件,可以用来检测硬盘.U盘.存储卡及其它可移动磁盘的读取及写 ...
- MEMS市场介绍
惠普第一.德州仪器第二 市场观察发展报告说,MEMS市场在2007年增长百分之九,达到70亿美元,其中前30名制造商的收入总和有56亿美元,平均增长7个百分点. 惠普(HP)打印机使用MEMS喷墨头, ...
- MFC窗口的父子关系和层级关系
一直对窗口之间的关系有些混乱,遇到需要指定父窗口的函数时常常要考虑很久,究竟父窗口是哪个窗口,遂上网查资料,略有所悟,简记如下: 对话框中的所有控件(比如Button等)都是其子窗口. ...
- Nim游戏博弈
Nim游戏的概述: 还记得这个游戏吗? 给出n列珍珠,两人轮流取珍珠,每次在某一列中取至少1颗珍珠,但不能在两列中取.最后拿光珍珠的人输. 后来,在一份资料上看到,这种游戏称为"拈(Nim) ...
- 【LeetCode练习题】Minimum Window Substring
找出包含子串的最小窗口 Given a string S and a string T, find the minimum window in S which will contain all the ...
- 模板应用--UI线程与worker线程同步 模仿c# invoke
由之前的一篇博文 <UI线程与worker线程><UI线程与worker线程>引出,UI线程与worker线程“串行化”在win32上实现是多么没有节操的事情,代码编写麻烦不说 ...