compiler.js 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. // resolve compilers to use.
  2. let cached
  3. exports.resolveCompiler = function (ctx, loaderContext) {
  4. if (cached) {
  5. return cached
  6. }
  7. // check 2.7
  8. try {
  9. const pkg = loadFromContext('vue/package.json', ctx)
  10. const [major, minor] = pkg.version.split('.')
  11. if (major === '2' && Number(minor) >= 7) {
  12. return (cached = {
  13. is27: true,
  14. compiler: loadFromContext('vue/compiler-sfc', ctx),
  15. templateCompiler: undefined
  16. })
  17. }
  18. } catch (e) {}
  19. return (cached = {
  20. compiler: require('@vue/component-compiler-utils'),
  21. templateCompiler: loadTemplateCompiler(ctx, loaderContext)
  22. })
  23. }
  24. function loadFromContext(path, ctx) {
  25. return require(require.resolve(path, {
  26. paths: [ctx]
  27. }))
  28. }
  29. function loadTemplateCompiler(ctx, loaderContext) {
  30. try {
  31. return loadFromContext('vue-template-compiler', ctx)
  32. } catch (e) {
  33. if (loaderContext) {
  34. if (/version mismatch/.test(e.toString())) {
  35. loaderContext.emitError(e)
  36. } else {
  37. loaderContext.emitError(
  38. new Error(
  39. `[vue-loader] vue-template-compiler must be installed as a peer dependency, ` +
  40. `or a compatible compiler implementation must be passed via options.`
  41. )
  42. )
  43. }
  44. } else {
  45. throw e
  46. }
  47. }
  48. }