|
| 1 | +const prettier = require('prettier') |
| 2 | +const loaderUtils = require('loader-utils') |
| 3 | +const compiler = require('vue-template-compiler') |
| 4 | +const transpile = require('vue-template-es2015-compiler') |
| 5 | +const transformRequire = require('./modules/transform-require') |
| 6 | +const transformSrcset = require('./modules/transform-srcset') |
| 7 | +const hotReloadAPIPath = require.resolve('vue-hot-reload-api') |
| 8 | + |
| 9 | +module.exports = function compileTemplate (descriptor, loaderContext, id) { |
| 10 | + const sourceTemplate = descriptor.template.content |
| 11 | + const isServer = loaderContext.target === 'node' |
| 12 | + const isProduction = loaderContext.minimize || process.env.NODE_ENV === 'production' |
| 13 | + const options = loaderUtils.getOptions(loaderContext) || {} |
| 14 | + const needsHotReload = !isServer && !isProduction && options.hotReload !== false |
| 15 | + const defaultModules = [transformRequire(options.transformToRequire), transformSrcset()] |
| 16 | + const hasScoped = descriptor.styles.some(({ scoped }) => scoped) |
| 17 | + const templateAttrs = descriptor.template.attrs || {} |
| 18 | + const hasComment = !!templateAttrs.comments |
| 19 | + const hasFunctionalTemplate = !!templateAttrs.functional |
| 20 | + |
| 21 | + const compilerOptions = { |
| 22 | + preserveWhitespace: options.preserveWhitespace, |
| 23 | + modules: defaultModules.concat(options.compilerModules || []), |
| 24 | + directives: options.compilerDirectives || {}, |
| 25 | + scopeId: hasScoped ? id : null, |
| 26 | + comments: hasComment |
| 27 | + } |
| 28 | + |
| 29 | + const compile = |
| 30 | + isServer && compiler.ssrCompile && options.optimizeSSR !== false |
| 31 | + ? compiler.ssrCompile |
| 32 | + : compiler.compile |
| 33 | + |
| 34 | + const compiled = compile(sourceTemplate, compilerOptions) |
| 35 | + |
| 36 | + // tips |
| 37 | + if (compiled.tips && compiled.tips.length) { |
| 38 | + compiled.tips.forEach(tip => { |
| 39 | + loaderContext.emitWarning(tip) |
| 40 | + }) |
| 41 | + } |
| 42 | + |
| 43 | + let code |
| 44 | + if (compiled.errors && compiled.errors.length) { |
| 45 | + loaderContext.emitError( |
| 46 | + `\n Error compiling template:\n${pad(sourceTemplate)}\n` + |
| 47 | + compiled.errors.map(e => ` - ${e}`).join('\n') + |
| 48 | + '\n' |
| 49 | + ) |
| 50 | + code = |
| 51 | + `export var render = function () {}\n` + |
| 52 | + `export var staticRenderFns = []` |
| 53 | + } else { |
| 54 | + const bubleOptions = options.buble || { |
| 55 | + transforms: { |
| 56 | + stripWithFunctional: hasFunctionalTemplate |
| 57 | + } |
| 58 | + } |
| 59 | + const staticRenderFns = compiled.staticRenderFns.map(fn => |
| 60 | + toFunction(fn, hasFunctionalTemplate) |
| 61 | + ) |
| 62 | + |
| 63 | + code = |
| 64 | + transpile( |
| 65 | + 'var render = ' + |
| 66 | + toFunction(compiled.render, hasFunctionalTemplate) + |
| 67 | + '\n' + |
| 68 | + 'var staticRenderFns = [' + |
| 69 | + staticRenderFns.join(',') + |
| 70 | + ']', |
| 71 | + bubleOptions |
| 72 | + ) + '\n' |
| 73 | + |
| 74 | + // prettify render fn |
| 75 | + if (!isProduction) { |
| 76 | + code = prettier.format(code, { semi: false }) |
| 77 | + } |
| 78 | + |
| 79 | + // mark with stripped (this enables Vue to use correct runtime proxy detection) |
| 80 | + if (!isProduction && bubleOptions.transforms.stripWith !== false) { |
| 81 | + code += `render._withStripped = true\n` |
| 82 | + } |
| 83 | + code += `export { render, staticRenderFns }` |
| 84 | + } |
| 85 | + // hot-reload |
| 86 | + if (needsHotReload) { |
| 87 | + code += |
| 88 | + '\nif (module.hot) {\n' + |
| 89 | + ' module.hot.accept()\n' + |
| 90 | + ' if (module.hot.data) {\n' + |
| 91 | + ' require("' + hotReloadAPIPath + '")' + |
| 92 | + ' .rerender("' + options.id + '", { render: render, staticRenderFns: staticRenderFns })\n' + |
| 93 | + ' }\n' + |
| 94 | + '}' |
| 95 | + } |
| 96 | + |
| 97 | + return code |
| 98 | +} |
| 99 | + |
| 100 | +function toFunction (code, hasFunctionalTemplate) { |
| 101 | + return ( |
| 102 | + 'function (' + (hasFunctionalTemplate ? '_h,_vm' : '') + ') {' + code + '}' |
| 103 | + ) |
| 104 | +} |
| 105 | + |
| 106 | +function pad (sourceTemplate) { |
| 107 | + return sourceTemplate |
| 108 | + .split(/\r?\n/) |
| 109 | + .map(line => ` ${line}`) |
| 110 | + .join('\n') |
| 111 | +} |
0 commit comments