resolveScript.js 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.resolveScript = exports.canInlineTemplate = exports.typeDepToSFCMap = exports.clientCache = void 0;
  4. const util_1 = require("./util");
  5. const compiler_1 = require("./compiler");
  6. const { compileScript } = compiler_1.compiler;
  7. exports.clientCache = new WeakMap();
  8. const serverCache = new WeakMap();
  9. exports.typeDepToSFCMap = new Map();
  10. /**
  11. * inline template mode can only be enabled if:
  12. * - is production (separate compilation needed for HMR during dev)
  13. * - template has no pre-processor (separate loader chain required)
  14. * - template is not using src
  15. */
  16. function canInlineTemplate(descriptor, isProd) {
  17. const templateLang = descriptor.template && descriptor.template.lang;
  18. const templateSrc = descriptor.template && descriptor.template.src;
  19. return isProd && !!descriptor.scriptSetup && !templateLang && !templateSrc;
  20. }
  21. exports.canInlineTemplate = canInlineTemplate;
  22. function resolveScript(descriptor, scopeId, options, loaderContext) {
  23. var _a;
  24. if (!descriptor.script && !descriptor.scriptSetup) {
  25. return null;
  26. }
  27. const isProd = loaderContext.mode === 'production' || process.env.NODE_ENV === 'production';
  28. const isServer = (_a = options.isServerBuild) !== null && _a !== void 0 ? _a : loaderContext.target === 'node';
  29. const enableInline = canInlineTemplate(descriptor, isProd);
  30. const cacheToUse = isServer ? serverCache : exports.clientCache;
  31. const cached = cacheToUse.get(descriptor);
  32. if (cached) {
  33. return cached;
  34. }
  35. let resolved = null;
  36. let templateCompiler;
  37. if (typeof options.compiler === 'string') {
  38. templateCompiler = require(options.compiler);
  39. }
  40. else {
  41. templateCompiler = options.compiler;
  42. }
  43. try {
  44. resolved = compileScript(descriptor, {
  45. id: scopeId,
  46. isProd,
  47. inlineTemplate: enableInline,
  48. // @ts-ignore this has been removed in 3.4
  49. reactivityTransform: options.reactivityTransform,
  50. propsDestructure: options.propsDestructure,
  51. defineModel: options.defineModel,
  52. babelParserPlugins: options.babelParserPlugins,
  53. templateOptions: {
  54. ssr: isServer,
  55. compiler: templateCompiler,
  56. compilerOptions: Object.assign(Object.assign({}, options.compilerOptions), (0, util_1.resolveTemplateTSOptions)(descriptor, options)),
  57. transformAssetUrls: options.transformAssetUrls || true,
  58. },
  59. });
  60. }
  61. catch (e) {
  62. loaderContext.emitError(e);
  63. }
  64. if (!isProd && (resolved === null || resolved === void 0 ? void 0 : resolved.deps)) {
  65. for (const [key, sfcs] of exports.typeDepToSFCMap) {
  66. if (sfcs.has(descriptor.filename) && !resolved.deps.includes(key)) {
  67. sfcs.delete(descriptor.filename);
  68. if (!sfcs.size) {
  69. exports.typeDepToSFCMap.delete(key);
  70. }
  71. }
  72. }
  73. for (const dep of resolved.deps) {
  74. const existingSet = exports.typeDepToSFCMap.get(dep);
  75. if (!existingSet) {
  76. exports.typeDepToSFCMap.set(dep, new Set([descriptor.filename]));
  77. }
  78. else {
  79. existingSet.add(descriptor.filename);
  80. }
  81. }
  82. }
  83. cacheToUse.set(descriptor, resolved);
  84. return resolved;
  85. }
  86. exports.resolveScript = resolveScript;