requirejs + vue 项目搭建详解

2020-06-12 21:10:23易采站长站整理

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) {ntrequire('VueCommon'); ntvar Vue = require('Vue');ntvar exports;n" + appendJs + h + ";ntexports.template = '#" + id + "';ntexports = Vue.extend(exports);ntVue.component('" + match[1] + "', exports);ntreturn 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);
}