utils.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", { value: true });
  3. exports.generateCodeFrameColumns = exports.createRollupError = exports.isSsr = exports.isInHybridNVue = exports.withSourcemap = void 0;
  4. const shared_1 = require("@vue/shared");
  5. const code_frame_1 = require("@babel/code-frame");
  6. const utils_1 = require("../plugins/vitejs/utils");
  7. function withSourcemap(config) {
  8. if (config.command === 'serve') {
  9. return true;
  10. }
  11. return !!config.build.sourcemap;
  12. }
  13. exports.withSourcemap = withSourcemap;
  14. function isInHybridNVue(config) {
  15. return config.nvue && process.env.UNI_RENDERER !== 'native';
  16. }
  17. exports.isInHybridNVue = isInHybridNVue;
  18. function isSsr(command, config) {
  19. if (command === 'serve') {
  20. return !!(config.server && config.server.middlewareMode);
  21. }
  22. if (command === 'build') {
  23. return !!(config.build && config.build.ssr);
  24. }
  25. return false;
  26. }
  27. exports.isSsr = isSsr;
  28. function createRollupError(plugin, id, error, source) {
  29. const { message, name, stack } = error;
  30. const rollupError = (0, shared_1.extend)(new Error(message), {
  31. id,
  32. plugin,
  33. name,
  34. stack,
  35. });
  36. if ('code' in error && error.loc) {
  37. rollupError.loc = {
  38. file: id,
  39. line: error.loc.start.line,
  40. column: error.loc.start.column,
  41. };
  42. if (source && source.length > 0) {
  43. if ('offsetStart' in error && 'offsetEnd' in error) {
  44. rollupError.frame = (0, code_frame_1.codeFrameColumns)(source, (0, utils_1.offsetToStartAndEnd)(source, error.offsetStart, error.offsetEnd));
  45. }
  46. else {
  47. rollupError.frame = (0, code_frame_1.codeFrameColumns)(source, error.loc);
  48. }
  49. }
  50. }
  51. if (id) {
  52. // 指定了id后,不让后续的rollup重写
  53. Object.defineProperty(rollupError, 'id', {
  54. get() {
  55. return id;
  56. },
  57. set(_v) { },
  58. });
  59. }
  60. return rollupError;
  61. }
  62. exports.createRollupError = createRollupError;
  63. exports.generateCodeFrameColumns = code_frame_1.codeFrameColumns;