umi-plugin-sentry
Version:
umi plugin for sentry
51 lines (37 loc) • 2.18 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = _default;
var _fs = require("fs");
var _path = require("path");
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; var ownKeys = Object.keys(source); if (typeof Object.getOwnPropertySymbols === 'function') { ownKeys = ownKeys.concat(Object.getOwnPropertySymbols(source).filter(function (sym) { return Object.getOwnPropertyDescriptor(source, sym).enumerable; })); } ownKeys.forEach(function (key) { _defineProperty(target, key, source[key]); }); } return target; }
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
const defaultOptions = {
dsn: process.env.SENTRY_DSN,
log: process.env.NODE_ENV === 'development' ? true : false
};
function _default(api, opts = {}) {
const joinSentryPath = path => (0, _path.join)(api.paths.tmpDirPath, 'sentry', path);
const joinSentryTemplatePath = path => (0, _path.join)(__dirname, '../template/umi/sentry', path);
api.onGenerateFiles(() => {
const sentryPath = joinSentryPath('');
if (!(0, _fs.existsSync)(sentryPath)) {
(0, _fs.mkdirSync)(sentryPath);
}
const options = _objectSpread({}, defaultOptions, opts);
if (process.env.NODE_ENV === 'production' && !options.dsn) {
api.log.error(`In production the 'sentry dsn' option is required.`);
}
const loadSentryOptions = JSON.stringify(options);
const indexPath = joinSentryPath('index.js');
const templatePath = joinSentryTemplatePath('index.js');
const indexTemplate = (0, _fs.readFileSync)(templatePath, 'utf-8');
const indexContent = indexTemplate.replace('// <% LoadSentryOptions %>', loadSentryOptions);
(0, _fs.writeFileSync)(indexPath, indexContent);
});
api.addRendererWrapperWithComponent('./sentry/index');
const dependencies = ['@sentry/browser'];
api.addVersionInfo(dependencies.map(pkgName => `${pkgName}@${require(`${pkgName}/package`).version}`));
}
;