pre.js 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. exports.uniPrePlugin = void 0;
  7. const path_1 = __importDefault(require("path"));
  8. const debug_1 = __importDefault(require("debug"));
  9. const pluginutils_1 = require("@rollup/pluginutils");
  10. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  11. const debugPreJs = (0, debug_1.default)('uni:pre-js');
  12. const debugPreHtml = (0, debug_1.default)('uni:pre-html');
  13. const debugPreJsTry = (0, debug_1.default)('uni:pre-js-try');
  14. const PRE_HTML_EXTNAME = ['.vue', '.uvue'];
  15. const PRE_JS_EXTNAME = ['.json', '.css', '.uts', '.ts'].concat(PRE_HTML_EXTNAME);
  16. function uniPrePlugin(options = {}) {
  17. const filter = (0, pluginutils_1.createFilter)(options.include, options.exclude);
  18. const preJsFile = uni_cli_shared_1.preUVueJs;
  19. const preHtmlFile = uni_cli_shared_1.preUVueHtml;
  20. return {
  21. name: 'uni:pre',
  22. transform(code, id) {
  23. if (!filter(id)) {
  24. return;
  25. }
  26. const { filename } = (0, uni_cli_shared_1.parseVueRequest)(id);
  27. const extname = path_1.default.extname(filename);
  28. const isHtml = PRE_HTML_EXTNAME.includes(extname);
  29. const isJs = PRE_JS_EXTNAME.includes(extname);
  30. const isPre = isHtml || isJs;
  31. if (isPre) {
  32. debugPreJsTry(id);
  33. }
  34. const hasEndif = isPre && code.includes('#endif');
  35. if (!hasEndif) {
  36. return;
  37. }
  38. if (isHtml) {
  39. code = preHtmlFile(code);
  40. debugPreHtml(id);
  41. }
  42. if (isJs) {
  43. code = preJsFile(code);
  44. debugPreJs(id);
  45. }
  46. return {
  47. code,
  48. };
  49. },
  50. };
  51. }
  52. exports.uniPrePlugin = uniPrePlugin;