You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

118 lines
4.8 KiB

4 years ago
  1. "use strict";
  2. var __importDefault = (this && this.__importDefault) || function (mod) {
  3. return (mod && mod.__esModule) ? mod : { "default": mod };
  4. };
  5. Object.defineProperty(exports, "__esModule", { value: true });
  6. const assetUrl_1 = __importDefault(require("./templateCompilerModules/assetUrl"));
  7. const srcset_1 = __importDefault(require("./templateCompilerModules/srcset"));
  8. const consolidate = require('consolidate');
  9. const transpile = require('vue-template-es2015-compiler');
  10. function compileTemplate(options) {
  11. const { preprocessLang } = options;
  12. const preprocessor = preprocessLang && consolidate[preprocessLang];
  13. if (preprocessor) {
  14. return actuallyCompile(Object.assign({}, options, {
  15. source: preprocess(options, preprocessor)
  16. }));
  17. }
  18. else if (preprocessLang) {
  19. return {
  20. code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
  21. source: options.source,
  22. tips: [
  23. `Component ${options.filename} uses lang ${preprocessLang} for template. Please install the language preprocessor.`
  24. ],
  25. errors: [
  26. `Component ${options.filename} uses lang ${preprocessLang} for template, however it is not installed.`
  27. ]
  28. };
  29. }
  30. else {
  31. return actuallyCompile(options);
  32. }
  33. }
  34. exports.compileTemplate = compileTemplate;
  35. function preprocess(options, preprocessor) {
  36. const { source, filename, preprocessOptions } = options;
  37. const finalPreprocessOptions = Object.assign({
  38. filename
  39. }, preprocessOptions);
  40. // Consolidate exposes a callback based API, but the callback is in fact
  41. // called synchronously for most templating engines. In our case, we have to
  42. // expose a synchronous API so that it is usable in Jest transforms (which
  43. // have to be sync because they are applied via Node.js require hooks)
  44. let res, err;
  45. preprocessor.render(source, finalPreprocessOptions, (_err, _res) => {
  46. if (_err)
  47. err = _err;
  48. res = _res;
  49. });
  50. if (err)
  51. throw err;
  52. return res;
  53. }
  54. function actuallyCompile(options) {
  55. const { source, compiler, compilerOptions = {}, transpileOptions = {}, transformAssetUrls, isProduction = process.env.NODE_ENV === 'production', isFunctional = false, optimizeSSR = false, prettify = true } = options;
  56. const compile = optimizeSSR && compiler.ssrCompile ? compiler.ssrCompile : compiler.compile;
  57. let finalCompilerOptions = compilerOptions;
  58. if (transformAssetUrls) {
  59. const builtInModules = [
  60. transformAssetUrls === true
  61. ? assetUrl_1.default()
  62. : assetUrl_1.default(transformAssetUrls),
  63. srcset_1.default()
  64. ];
  65. finalCompilerOptions = Object.assign({}, compilerOptions, {
  66. modules: [...builtInModules, ...(compilerOptions.modules || [])]
  67. });
  68. }
  69. const { render, staticRenderFns, tips, errors } = compile(source, finalCompilerOptions);
  70. if (errors && errors.length) {
  71. return {
  72. code: `var render = function () {}\n` + `var staticRenderFns = []\n`,
  73. source,
  74. tips,
  75. errors
  76. };
  77. }
  78. else {
  79. const finalTranspileOptions = Object.assign({}, transpileOptions, {
  80. transforms: Object.assign({}, transpileOptions.transforms, {
  81. stripWithFunctional: isFunctional
  82. })
  83. });
  84. const toFunction = (code) => {
  85. return `function (${isFunctional ? `_h,_vm` : ``}) {${code}}`;
  86. };
  87. // transpile code with vue-template-es2015-compiler, which is a forked
  88. // version of Buble that applies ES2015 transforms + stripping `with` usage
  89. let code = transpile(`var __render__ = ${toFunction(render)}\n` +
  90. `var __staticRenderFns__ = [${staticRenderFns.map(toFunction)}]`, finalTranspileOptions) + `\n`;
  91. // #23 we use __render__ to avoid `render` not being prefixed by the
  92. // transpiler when stripping with, but revert it back to `render` to
  93. // maintain backwards compat
  94. code = code.replace(/\s__(render|staticRenderFns)__\s/g, ' $1 ');
  95. if (!isProduction) {
  96. // mark with stripped (this enables Vue to use correct runtime proxy
  97. // detection)
  98. code += `render._withStripped = true`;
  99. if (prettify) {
  100. try {
  101. code = require('prettier').format(code, {
  102. semi: false,
  103. parser: 'babel'
  104. });
  105. }
  106. catch (e) {
  107. tips.push(`Failed to prettify component ${options.filename} template source after compilation.`);
  108. }
  109. }
  110. }
  111. return {
  112. code,
  113. source,
  114. tips,
  115. errors
  116. };
  117. }
  118. }