templateLoader.js 3.0 KB

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