UNPKG

bm-scaffold-test

Version:

本木前端脚手架

103 lines (97 loc) 3.05 kB
/** * @Author: songqi * @Date: 2016-10-13 * @Email: songqi@benmu-health.com * @Last modified by: songqi * @Last modified time: 2016-10-13 */ var path = require('path') var compiler = require('vue-template-compiler') var transpile = require('vue-template-es2015-compiler') var loaderUtils = require('loader-utils') var beautify = require('js-beautify').js_beautify var normalize = require('./normalize') var hotReloadAPIPath = path.resolve(__dirname, '../../../node_modules/', normalize.dep('vue-hot-reload-api')) // vue compiler module for using transforming `<tag>:<attribute>` to `require` var defaultTransformToRequire = { img: 'src' } var transformToRequire = defaultTransformToRequire var defaultCompileOptions = { modules: [{ postTransformNode (el) { for (var tag in transformToRequire) { if (el.tag === tag && el.attrs) { var attributes = transformToRequire[tag] if (typeof attributes === 'string') { el.attrs.some(attr => rewrite(attr, attributes)) } else if (Array.isArray(attributes)) { attributes.forEach(item => el.attrs.some(attr => rewrite(attr, item))) } } } } }] } function rewrite (attr, name) { if (attr.name === name) { var value = attr.value var isStatic = value.charAt(0) === '"' && value.charAt(value.length - 1) === '"' if (!isStatic) { return } var firstChar = value.charAt(1) if (firstChar === '.' || firstChar === '~') { if (firstChar === '~') { value = '"' + value.slice(2) } attr.value = `require(${value})` } return true } } module.exports = function (html) { this.cacheable() var query = loaderUtils.parseQuery(this.query) var isServer = this.options.target === 'node' var vueOptions = this.options.__vueOptions__ if (vueOptions.transformToRequire) { transformToRequire = Object.assign( {}, defaultTransformToRequire, vueOptions.transformToRequire ) } var compiled = compiler.compile(html, Object.assign({ preserveWhitespace: vueOptions.preserveWhitespace }, defaultCompileOptions)) var code if (compiled.errors.length) { var self = this compiled.errors.forEach(function (err) { self.emitError('template syntax error ' + err) }) code = 'module.exports={render:function(){},staticRenderFns:[]}' } else { code = transpile('module.exports={' + 'render:' + toFunction(compiled.render) + ',' + 'staticRenderFns: [' + compiled.staticRenderFns.map(toFunction).join(',') + ']' + '}') } // hot-reload if (!isServer && !this.minimize && process.env.NODE_ENV !== 'production') { code += '\nif (module.hot) {\n' + ' module.hot.accept()\n' + ' if (module.hot.data) {\n' + ' require("' + hotReloadAPIPath + '").rerender("' + query.id + '", module.exports)\n' + ' }\n' + '}' } return code } function toFunction (code) { return 'function (){' + beautify(code, { indent_size: 2 }) + '}' }