Advertisement
Guest User

Untitled

a guest
Nov 25th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.89 KB | None | 0 0
  1. 'use strict';
  2.  
  3. var through = require('through2');
  4. var PluginError = require('gulp-util').PluginError;
  5.  
  6. var reTemplate = /<template>([\s\S]+)<\/template>/i;
  7. var reScript = /<script>([\s\S]+)<\/script>/i;
  8. var reNewLine = /[\n\r\t]+/g;
  9. var reComponent = /Vue\.component\([\s]*[\'\"\`]{1}[\w-]+[\'\"\`]{1},[\s]*\{/;
  10. var reInstance = /new[\s]+Vue[\s]*\([\s\n\r]*\{/;
  11.  
  12. module.exports = function() {
  13. return through.obj(function(file, encoding, callback) {
  14. if(file.isNull())
  15. return callback(null, file);
  16.  
  17. if(file.isStream())
  18. this.emit('error', new PluginError(PLUGIN_NAME, 'Streams not supported!'));
  19.  
  20. //if it's a .vue file
  21. if(file.path.length <= 4 || file.path.substring(file.path.length - 4, file.path.length) !== '.vue')
  22. return callback(null, file);
  23.  
  24. var strFileContent = String(file.contents);
  25.  
  26. //dealing with a template tag
  27. var templateContent = reTemplate.exec(strFileContent);
  28.  
  29. if(templateContent.length < 2) //if there is no template inside the file
  30. return callback(null, file);
  31.  
  32. templateContent = templateContent[1].replace(reNewLine, '');
  33.  
  34. //dealing with a script tag
  35. var scriptContent = reScript.exec(strFileContent);
  36.  
  37. if(scriptContent.length < 2) //if there is no script tag inside the file
  38. return callback(null, file);
  39.  
  40. scriptContent = scriptContent[1];
  41.  
  42. var componentMatch = reComponent.exec(scriptContent);
  43. if(!componentMatch) { //if there is no component definition
  44. componentMatch = reInstance.exec(scriptContent);
  45. if(!componentMatch)
  46. return callback(null, file);
  47. }
  48.  
  49. scriptContent = scriptContent.replace(
  50. componentMatch[0],
  51. componentMatch[0] + '\n\ttemplate: \'' + templateContent + '\',\n'
  52. );
  53. console.log(scriptContent);
  54.  
  55. //we're fine, return the modified file
  56. file.contents = new Buffer(scriptContent);
  57.  
  58. //change extension to .js
  59. file.path = file.path.replace(/\.vue$/, '.js');
  60.  
  61. console.log(file.path);
  62. callback(null, file);
  63. });
  64. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement