resolveScript.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. const { resolveCompiler } = require('./compiler')
  2. const clientCache = new WeakMap()
  3. const serverCache = new WeakMap()
  4. exports.resolveScript = function resolveScript(
  5. descriptor,
  6. scopeId,
  7. options,
  8. loaderContext
  9. ) {
  10. if (!descriptor.script && !descriptor.scriptSetup) {
  11. return null
  12. }
  13. const { compiler } = resolveCompiler(loaderContext.rootContext, loaderContext)
  14. if (!compiler.compileScript) {
  15. if (descriptor.scriptSetup) {
  16. loaderContext.emitError(
  17. 'The version of Vue you are using does not support <script setup>. ' +
  18. 'Please upgrade to 2.7 or above.'
  19. )
  20. }
  21. return descriptor.script
  22. }
  23. const isProd =
  24. loaderContext.mode === 'production' || process.env.NODE_ENV === 'production'
  25. const isServer = options.optimizeSSR || loaderContext.target === 'node'
  26. const cacheToUse = isServer ? serverCache : clientCache
  27. const cached = cacheToUse.get(descriptor)
  28. if (cached) {
  29. return cached
  30. }
  31. let resolved = null
  32. try {
  33. resolved = compiler.compileScript(descriptor, {
  34. id: scopeId,
  35. isProd,
  36. babelParserPlugins: options.babelParserPlugins
  37. })
  38. } catch (e) {
  39. loaderContext.emitError(e)
  40. }
  41. cacheToUse.set(descriptor, resolved)
  42. return resolved
  43. }