All files / src index.js

72.31% Statements 47/65
63.33% Branches 19/30
75% Functions 3/4
72.31% Lines 47/65

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 1571x 1x 1x 1x 1x   1x 1x   1x                 1x     1x       1x     1x                     1x     1x     1x 1x       1x     1x 2x       1x 1x 1x                     1x         1x 1x 1x 1x 1x 1x   1x 1x         1x 1x 1x 1x 1x               1x           1x 2x         2x 1x       1x         1x   1x 1x 1x                                               1x          
const path = require('path')
const hashSum = require('hash-sum')
const compiler = require('vue-template-compiler')
const genId = require('./lib/gen-id')
const rewriteStyle = require('./lib/style-rewriter')
// 编译模板
const compileTemplate = require('./lib/template-compiler')
const insertCSS = require('./lib/insert-css')
// 替换旧的scoped标识为唯一hashid标志
const replaceScopedFlag = require('./lib/replace-scoped-flag')
 
/**
 * FIS3 Compile 编译阶段插件接口
 * @param  {string} content  文件内容
 * @param  {File} file       fis的File对象 [fis3/lib/file.js]
 * @param  {object} conf     插件配置属性
 * @return {string]}         处理后的文件内容
 */
module.exports = function(content, file, conf) {
 
  // 不处理空文件
  Iif (!content || !content.trim()) {
    return content;
  }
 
  var scriptStr = '';
  var output, configs, jsLang;
 
  configs = Object.assign({
    cssScopedFlag: '__vuec__',
    extractCSS: true,
    cssScopedIdPrefix: '_v-',
    cssScopedHashType: 'sum',
    cssScopedHashLength: 8,
    styleNameJoin: '',
    runtimeOnly: false
  }, conf);
 
  // 兼容content为buffer的情况
  content = content.toString();
 
  // generate css scope id
  const id = configs.cssScopedIdPrefix + genId(file, configs);
 
  // 兼容旧的cssScopedFlag
  Eif (configs.cssScopedFlag) {
    content = replaceScopedFlag(content, configs.cssScopedFlag, id);
  }
 
  // 解析组件
  var output = compiler.parseComponent(content.toString(), { pad: true });
 
  // check for scoped style nodes
  var hasScopedStyle = output.styles.some(function (style) {
    return style.scoped
  });
 
  // script
  Eif (output.script) {
    scriptStr = output.script.content;
    jsLang = output.script.lang || 'js';
  } else {
    scriptStr += 'module.exports = {}';
    jsLang = 'js';
  }
 
  // 部分内容以 js 的方式编译一次。如果要支持 cooffe 需要这么配置。
  // 只针对预编译语言
  // fis.match('*.vue:cooffe', {
  //   parser: fis.plugin('cooffe')
  // })
  scriptStr = fis.compile.partial(scriptStr, file, {
    ext: jsLang,
    isJsLike: true
  });
 
  scriptStr += '\nvar __vue__options__;\n';
  scriptStr += 'if(exports && exports.__esModule && exports.default){\n';
  scriptStr += '  __vue__options__ = exports.default;\n';
  scriptStr += '}else{\n';
  scriptStr += '  __vue__options__ = module.exports;\n';
  scriptStr += '}\n';
 
  Eif (output.template) {
    var templateContent = fis.compile.partial(output.template.content, file, {
      ext: output.template.lang || 'html',
      isHtmlLike: true
    });
    // runtimeOnly 运行模式
    Eif (configs.runtimeOnly) {
      var result = compileTemplate(templateContent);
      Eif(result){
        scriptStr += '__vue__options__.render =' + result.render + '\n';
        scriptStr += '__vue__options__.staticRenderFns =' + result.staticRenderFns + '\n';
      }
    } else {
      // template
      scriptStr += '__vue__options__.template = ' + JSON.stringify(templateContent) + '\n';
    }
  }
 
  Iif (hasScopedStyle) {
    // template
    scriptStr += '__vue__options__._scopeId = ' + JSON.stringify(id) + '\n';
  }
 
  // style
  output.styles.forEach(function(item, index) {
    Iif (!item.content) {
      return;
    }
 
    // empty string, or all space line
    if(/^\s*$/.test(item.content)){
      return;
    }
 
    // css也采用片段编译,更好的支持less、sass等其他语言
    var styleContent = fis.compile.partial(item.content, file, {
      ext: item.lang || 'css',
      isCssLike: true
    });
 
    styleContent = rewriteStyle(id, styleContent, item.scoped, {})
 
    Eif (!configs.extractCSS) {
      scriptStr += '\n;(' + insertCSS + ')(' + JSON.stringify(styleContent) + ');\n';
      return;
    }
 
    var styleFileName, styleFile;
 
    if (output['styles'].length == 1) {
      styleFileName = file.realpathNoExt + configs.styleNameJoin + '.css';
    } else {
      styleFileName = file.realpathNoExt + configs.styleNameJoin + '-' + index + '.css';
    }
 
    styleFile = fis.file.wrap(styleFileName);
 
    styleFile.cache = file.cache;
    styleFile.isCssLike = true;
    styleFile.setContent(styleContent);
    fis.compile.process(styleFile);
    styleFile.links.forEach(function(derived) {
      file.addLink(derived);
    });
    file.derived.push(styleFile);
    file.addRequire(styleFile.getId());
  });
 
  return scriptStr;
};