css.js 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  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.uniAppCssPlugin = void 0;
  7. const picocolors_1 = __importDefault(require("picocolors"));
  8. const uni_cli_shared_1 = require("@dcloudio/uni-cli-shared");
  9. const uni_nvue_styler_1 = require("@dcloudio/uni-nvue-styler");
  10. function uniAppCssPlugin(resolvedConfig) {
  11. return {
  12. name: 'vite:css-post',
  13. buildStart() {
  14. // 用于覆盖原始插件方法
  15. // noop
  16. },
  17. async transform(source, filename) {
  18. if (!uni_cli_shared_1.cssLangRE.test(filename) || uni_cli_shared_1.commonjsProxyRE.test(filename)) {
  19. return;
  20. }
  21. if (source.includes('#endif')) {
  22. source = (0, uni_cli_shared_1.preUVueCss)(source);
  23. }
  24. source = (0, uni_cli_shared_1.parseAssets)(resolvedConfig, source);
  25. // 仅做校验使用
  26. const { messages, code } = await (0, uni_nvue_styler_1.parse)(source, {
  27. filename,
  28. logLevel: 'WARNING',
  29. type: 'uvue',
  30. platform: process.env.UNI_UTS_PLATFORM,
  31. });
  32. messages.forEach((message) => {
  33. if (message.type === 'warning') {
  34. // 拆分成多行,第一行输出信息(有颜色),后续输出错误代码+文件行号
  35. resolvedConfig.logger.warn(picocolors_1.default.yellow(`[plugin:uni:app-uvue-css] ${message.text}`));
  36. let msg = '';
  37. if (message.line && message.column) {
  38. msg += `\n${(0, uni_cli_shared_1.generateCodeFrame)(source, {
  39. line: message.line,
  40. column: message.column,
  41. }).replace(/\t/g, ' ')}\n`;
  42. }
  43. msg += `${(0, uni_cli_shared_1.formatAtFilename)(filename)}`;
  44. resolvedConfig.logger.warn(msg);
  45. }
  46. });
  47. return { code: `export default ${code}`, map: { mappings: '' } };
  48. },
  49. generateBundle() {
  50. // 用于覆盖原始插件方法
  51. // noop
  52. },
  53. };
  54. }
  55. exports.uniAppCssPlugin = uniAppCssPlugin;