templateLoader.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. "use strict";
  2. var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
  3. if (k2 === undefined) k2 = k;
  4. Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
  5. }) : (function(o, m, k, k2) {
  6. if (k2 === undefined) k2 = k;
  7. o[k2] = m[k];
  8. }));
  9. var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
  10. Object.defineProperty(o, "default", { enumerable: true, value: v });
  11. }) : function(o, v) {
  12. o["default"] = v;
  13. });
  14. var __importStar = (this && this.__importStar) || function (mod) {
  15. if (mod && mod.__esModule) return mod;
  16. var result = {};
  17. if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
  18. __setModuleDefault(result, mod);
  19. return result;
  20. };
  21. Object.defineProperty(exports, "__esModule", { value: true });
  22. const qs = __importStar(require("querystring"));
  23. const formatError_1 = require("./formatError");
  24. const descriptorCache_1 = require("./descriptorCache");
  25. const resolveScript_1 = require("./resolveScript");
  26. const util_1 = require("./util");
  27. const compiler_1 = require("./compiler");
  28. const { compileTemplate } = compiler_1.compiler;
  29. // Loader that compiles raw template into JavaScript functions.
  30. // This is injected by the global pitcher (../pitch) for template
  31. // selection requests initiated from vue files.
  32. const TemplateLoader = function (source, inMap) {
  33. var _a;
  34. source = String(source);
  35. const loaderContext = this;
  36. // although this is not the main vue-loader, we can get access to the same
  37. // vue-loader options because we've set an ident in the plugin and used that
  38. // ident to create the request for this loader in the pitcher.
  39. const options = ((0, util_1.getOptions)(loaderContext) || {});
  40. const isServer = (_a = options.isServerBuild) !== null && _a !== void 0 ? _a : loaderContext.target === 'node';
  41. const isProd = loaderContext.mode === 'production' || process.env.NODE_ENV === 'production';
  42. const query = qs.parse(loaderContext.resourceQuery.slice(1));
  43. const scopeId = query.id;
  44. const descriptor = (0, descriptorCache_1.getDescriptor)(loaderContext.resourcePath);
  45. const script = (0, resolveScript_1.resolveScript)(descriptor, query.id, options, loaderContext);
  46. let templateCompiler;
  47. if (typeof options.compiler === 'string') {
  48. templateCompiler = require(options.compiler);
  49. }
  50. else {
  51. templateCompiler = options.compiler;
  52. }
  53. const compiled = compileTemplate({
  54. source,
  55. filename: loaderContext.resourcePath,
  56. inMap,
  57. id: scopeId,
  58. scoped: !!query.scoped,
  59. slotted: descriptor.slotted,
  60. isProd,
  61. ssr: isServer,
  62. ssrCssVars: descriptor.cssVars,
  63. compiler: templateCompiler,
  64. compilerOptions: Object.assign(Object.assign(Object.assign({}, options.compilerOptions), { scopeId: query.scoped ? `data-v-${scopeId}` : undefined, bindingMetadata: script ? script.bindings : undefined }), (0, util_1.resolveTemplateTSOptions)(descriptor, options)),
  65. transformAssetUrls: options.transformAssetUrls || true,
  66. });
  67. // tips
  68. if (compiled.tips.length) {
  69. compiled.tips.forEach((tip) => {
  70. loaderContext.emitWarning(new Error(tip));
  71. });
  72. }
  73. // errors
  74. if (compiled.errors && compiled.errors.length) {
  75. compiled.errors.forEach((err) => {
  76. if (typeof err === 'string') {
  77. loaderContext.emitError(new Error(err));
  78. }
  79. else {
  80. (0, formatError_1.formatError)(err, inMap ? inMap.sourcesContent[0] : source, loaderContext.resourcePath);
  81. loaderContext.emitError(err);
  82. }
  83. });
  84. }
  85. const { code, map } = compiled;
  86. loaderContext.callback(null, code, map);
  87. };
  88. exports.default = TemplateLoader;