templateLoader.js 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. const qs = require('querystring')
  2. const loaderUtils = require('loader-utils')
  3. const { resolveCompiler } = require('../compiler')
  4. const { getDescriptor } = require('../descriptorCache')
  5. const { resolveScript } = require('../resolveScript')
  6. // Loader that compiles raw template into JavaScript functions.
  7. // This is injected by the global pitcher (../pitch) for template
  8. // selection requests initiated from vue files.
  9. module.exports = function (source) {
  10. const loaderContext = this
  11. const filename = this.resourcePath
  12. const ctx = this.rootContext
  13. const query = qs.parse(this.resourceQuery.slice(1))
  14. // although this is not the main vue-loader, we can get access to the same
  15. // vue-loader options because we've set an ident in the plugin and used that
  16. // ident to create the request for this loader in the pitcher.
  17. const options = loaderUtils.getOptions(loaderContext) || {}
  18. const { id } = query
  19. const isServer = loaderContext.target === 'node'
  20. const isProduction =
  21. options.productionMode ||
  22. loaderContext.minimize ||
  23. process.env.NODE_ENV === 'production'
  24. const isFunctional = query.functional
  25. const compilerOptions = Object.assign(
  26. {
  27. outputSourceRange: true
  28. },
  29. options.compilerOptions,
  30. {
  31. scopeId: query.scoped ? `data-v-${id}` : null,
  32. comments: query.comments
  33. }
  34. )
  35. const { compiler, templateCompiler } = resolveCompiler(ctx, loaderContext)
  36. const descriptor = getDescriptor(filename, options, loaderContext)
  37. const script = resolveScript(descriptor, id, options, loaderContext)
  38. // for vue/compiler-sfc OR @vue/component-compiler-utils
  39. const finalOptions = {
  40. source,
  41. filename: this.resourcePath,
  42. compiler: options.compiler || templateCompiler,
  43. compilerOptions,
  44. // allow customizing behavior of vue-template-es2015-compiler
  45. transpileOptions: options.transpileOptions,
  46. transformAssetUrls: options.transformAssetUrls || true,
  47. isProduction,
  48. isFunctional,
  49. optimizeSSR: isServer && options.optimizeSSR !== false,
  50. prettify: options.prettify,
  51. bindings: script ? script.bindings : undefined
  52. }
  53. const compiled = compiler.compileTemplate(finalOptions)
  54. // tips
  55. if (compiled.tips && compiled.tips.length) {
  56. compiled.tips.forEach((tip) => {
  57. loaderContext.emitWarning(typeof tip === 'object' ? tip.msg : tip)
  58. })
  59. }
  60. // errors
  61. if (compiled.errors && compiled.errors.length) {
  62. const generateCodeFrame =
  63. (templateCompiler && templateCompiler.generateCodeFrame) ||
  64. compiler.generateCodeFrame
  65. // 2.6 compiler outputs errors as objects with range
  66. if (generateCodeFrame && finalOptions.compilerOptions.outputSourceRange) {
  67. // TODO account for line offset in case template isn't placed at top
  68. // of the file
  69. loaderContext.emitError(
  70. `\n\n Errors compiling template:\n\n` +
  71. compiled.errors
  72. .map(({ msg, start, end }) => {
  73. const frame = generateCodeFrame(source, start, end)
  74. return ` ${msg}\n\n${pad(frame)}`
  75. })
  76. .join(`\n\n`) +
  77. '\n'
  78. )
  79. } else {
  80. loaderContext.emitError(
  81. `\n Error compiling template:\n${pad(compiled.source)}\n` +
  82. compiled.errors.map((e) => ` - ${e}`).join('\n') +
  83. '\n'
  84. )
  85. }
  86. }
  87. const { code } = compiled
  88. // finish with ESM exports
  89. return code + `\nexport { render, staticRenderFns }`
  90. }
  91. function pad(source) {
  92. return source
  93. .split(/\r?\n/)
  94. .map((line) => ` ${line}`)
  95. .join('\n')
  96. }