This commit is contained in:
chrosey
2017-09-13 07:52:34 +02:00
parent a1f16c37f4
commit 2340b0226b
24621 changed files with 2912161 additions and 149 deletions
+81
View File
@@ -0,0 +1,81 @@
var loaderUtils = require('loader-utils')
var normalize = require('../utils/normalize')
var compiler = require('vue-template-compiler')
var beautify = require('js-beautify').js_beautify
var transpile = require('vue-template-es2015-compiler')
var hotReloadAPIPath = normalize.dep('vue-hot-reload-api')
var transformRequire = require('./modules/transform-require')
module.exports = function (html) {
this.cacheable()
var isServer = this.target === 'node'
var isProduction = this.minimize || process.env.NODE_ENV === 'production'
var vueOptions = this.options.__vueOptions__ || {}
var options = loaderUtils.getOptions(this) || {}
var defaultModules = [transformRequire(options.transformToRequire)]
var userModules = vueOptions.compilerModules || options.compilerModules
// for HappyPack cross-process use cases
if (typeof userModules === 'string') {
userModules = require(userModules)
}
var compilerOptions = {
preserveWhitespace: options.preserveWhitespace,
modules: defaultModules.concat(userModules || [])
}
var compiled = compiler.compile(html, compilerOptions)
// tips
if (compiled.tips && compiled.tips.length) {
compiled.tips.forEach(tip => {
this.emitWarning(tip)
})
}
var code
if (compiled.errors && compiled.errors.length) {
this.emitError(
`\n Error compiling template:\n${pad(html)}\n` +
compiled.errors.map(e => ` - ${e}`).join('\n') + '\n'
)
code = 'module.exports={render:function(){},staticRenderFns:[]}'
} else {
var bubleOptions = options.buble
code = transpile('module.exports={' +
'render:' + toFunction(compiled.render) + ',' +
'staticRenderFns: [' + compiled.staticRenderFns.map(toFunction).join(',') + ']' +
'}', bubleOptions)
// mark with stripped (this enables Vue to use correct runtime proxy detection)
if (!isProduction && (
!bubleOptions ||
!bubleOptions.transforms ||
bubleOptions.transforms.stripWith !== false
)) {
code += `\nmodule.exports.render._withStripped = true`
}
}
// hot-reload
if (!isServer && !isProduction) {
code +=
'\nif (module.hot) {\n' +
' module.hot.accept()\n' +
' if (module.hot.data) {\n' +
' require("' + hotReloadAPIPath + '").rerender("' + options.id + '", module.exports)\n' +
' }\n' +
'}'
}
return code
}
function toFunction (code) {
return 'function (){' + beautify(code, {
indent_size: 2 // eslint-disable-line camelcase
}) + '}'
}
function pad (html) {
return html.split(/\r?\n/).map(line => ` ${line}`).join('\n')
}
@@ -0,0 +1,49 @@
// vue compiler module for transforming `<tag>:<attribute>` to `require`
var defaultOptions = {
img: 'src',
image: 'xlink:href'
}
module.exports = userOptions => {
var options = userOptions
? Object.assign({}, defaultOptions, userOptions)
: defaultOptions
return {
postTransformNode: node => {
transform(node, options)
}
}
}
function transform (node, options) {
for (var tag in options) {
if (node.tag === tag && node.attrs) {
var attributes = options[tag]
if (typeof attributes === 'string') {
node.attrs.some(attr => rewrite(attr, attributes))
} else if (Array.isArray(attributes)) {
attributes.forEach(item => node.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
}
}
+50
View File
@@ -0,0 +1,50 @@
// loader for pre-processing templates with e.g. pug
var cons = require('consolidate')
var loaderUtils = require('loader-utils')
var extname = require('path').extname
module.exports = function (content) {
this.cacheable && this.cacheable()
var callback = this.async()
var opt = loaderUtils.getOptions(this) || {}
// this is never documented and should be deprecated
// but we'll keep it so we don't break stuff
var vue = this.options.__vueOptions__
if (vue && vue.template) {
for (var key in vue.template) {
opt[key] = vue.template[key]
}
}
function exportContent (content) {
if (opt.raw) {
callback(null, content)
} else {
callback(null, 'module.exports = ' + JSON.stringify(content))
}
}
// with no engine given, use the file extension as engine
if (!opt.engine) {
opt.engine = extname(this.request).substr(1).toLowerCase()
}
if (!cons[opt.engine]) {
return callback(new Error(
'Template engine \'' + opt.engine + '\' ' +
'isn\'t available in Consolidate.js'
))
}
// for relative includes
opt.filename = this.resourcePath
cons[opt.engine].render(content, opt, function (err, html) {
if (err) {
return callback(err)
}
exportContent(html)
})
}