81378fb2c03f15070c80877848aa2b036afdaae95fb53410e8a5f94e16ea18a4ed18b887715984d1ab37f71c1b1aa559749b5364ebd23562e6d3c9e1fb05ba 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. /* @flow */
  2. import { extend } from 'shared/util'
  3. import { detectErrors } from './error-detector'
  4. import { createCompileToFunctionFn } from './to-function'
  5. export function createCompilerCreator (baseCompile: Function): Function {
  6. return function createCompiler (baseOptions: CompilerOptions) {
  7. function compile (
  8. template: string,
  9. options?: CompilerOptions
  10. ): CompiledResult {
  11. const finalOptions = Object.create(baseOptions)
  12. const errors = []
  13. const tips = []
  14. let warn = (msg, range, tip) => {
  15. (tip ? tips : errors).push(msg)
  16. }
  17. if (options) {
  18. if (process.env.NODE_ENV !== 'production' && options.outputSourceRange) {
  19. // $flow-disable-line
  20. const leadingSpaceLength = template.match(/^\s*/)[0].length
  21. warn = (msg, range, tip) => {
  22. const data: WarningMessage = { msg }
  23. if (range) {
  24. if (range.start != null) {
  25. data.start = range.start + leadingSpaceLength
  26. }
  27. if (range.end != null) {
  28. data.end = range.end + leadingSpaceLength
  29. }
  30. }
  31. (tip ? tips : errors).push(data)
  32. }
  33. }
  34. // merge custom modules
  35. if (options.modules) {
  36. finalOptions.modules =
  37. (baseOptions.modules || []).concat(options.modules)
  38. }
  39. // merge custom directives
  40. if (options.directives) {
  41. finalOptions.directives = extend(
  42. Object.create(baseOptions.directives || null),
  43. options.directives
  44. )
  45. }
  46. // copy other options
  47. for (const key in options) {
  48. if (key !== 'modules' && key !== 'directives') {
  49. finalOptions[key] = options[key]
  50. }
  51. }
  52. }
  53. finalOptions.warn = warn
  54. const compiled = baseCompile(template.trim(), finalOptions)
  55. if (process.env.NODE_ENV !== 'production') {
  56. detectErrors(compiled.ast, warn)
  57. }
  58. compiled.errors = errors
  59. compiled.tips = tips
  60. return compiled
  61. }
  62. return {
  63. compile,
  64. compileToFunctions: createCompileToFunctionFn(compile)
  65. }
  66. }
  67. }