resolveScript.js 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.resolveScript = exports.canInlineTemplate = void 0;
  4. const util_1 = require("./util");
  5. const compiler_1 = require("./compiler");
  6. const clientCache = new WeakMap();
  7. const serverCache = new WeakMap();
  8. /**
  9. * inline template mode can only be enabled if:
  10. * - is production (separate compilation needed for HMR during dev)
  11. * - template has no pre-processor (separate loader chain required)
  12. * - template is not using src
  13. */
  14. function canInlineTemplate(descriptor, isProd) {
  15. const templateLang = descriptor.template && descriptor.template.lang;
  16. const templateSrc = descriptor.template && descriptor.template.src;
  17. return isProd && !!descriptor.scriptSetup && !templateLang && !templateSrc;
  18. }
  19. exports.canInlineTemplate = canInlineTemplate;
  20. function resolveScript(descriptor, scopeId, options, loaderContext) {
  21. var _a;
  22. if (!descriptor.script && !descriptor.scriptSetup) {
  23. return null;
  24. }
  25. const isProd = loaderContext.mode === 'production' || process.env.NODE_ENV === 'production';
  26. const isServer = (_a = options.isServerBuild) !== null && _a !== void 0 ? _a : loaderContext.target === 'node';
  27. const enableInline = canInlineTemplate(descriptor, isProd);
  28. const cacheToUse = isServer ? serverCache : clientCache;
  29. const cached = cacheToUse.get(descriptor);
  30. if (cached) {
  31. return cached;
  32. }
  33. let resolved = null;
  34. let templateCompiler;
  35. if (typeof options.compiler === 'string') {
  36. templateCompiler = require(options.compiler);
  37. }
  38. else {
  39. templateCompiler = options.compiler;
  40. }
  41. if (compiler_1.compiler.compileScript) {
  42. try {
  43. resolved = compiler_1.compiler.compileScript(descriptor, {
  44. id: scopeId,
  45. isProd,
  46. inlineTemplate: enableInline,
  47. refSugar: options.refSugar,
  48. babelParserPlugins: options.babelParserPlugins,
  49. templateOptions: {
  50. ssr: isServer,
  51. compiler: templateCompiler,
  52. compilerOptions: Object.assign(Object.assign({}, options.compilerOptions), (0, util_1.resolveTemplateTSOptions)(descriptor, options)),
  53. transformAssetUrls: options.transformAssetUrls || true,
  54. },
  55. });
  56. }
  57. catch (e) {
  58. loaderContext.emitError(e);
  59. }
  60. }
  61. else if (descriptor.scriptSetup) {
  62. loaderContext.emitError(`<script setup> is not supported by the installed version of ` +
  63. `@vue/compiler-sfc - please upgrade.`);
  64. }
  65. else {
  66. resolved = descriptor.script;
  67. }
  68. cacheToUse.set(descriptor, resolved);
  69. return resolved;
  70. }
  71. exports.resolveScript = resolveScript;