{"id":342288,"date":"2022-12-07T15:02:45","date_gmt":"2022-12-07T15:02:45","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=342288"},"modified":"-0001-11-30T00:00:00","modified_gmt":"2022-12-07T15:02:45","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=342288","title":{"rendered":"<span>\u0421\u043e\u0437\u0434\u0430\u0435\u043c \u0441\u0432\u043e\u0439 babel \u043f\u043b\u0430\u0433\u0438\u043d \u0438 \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u0432 npm<\/span>"},"content":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0432\u043e\u0435\u0433\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043b\u0430\u0433\u0438\u043d\u0430 babel \u0438 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u0435\u0433\u043e \u0432 npm. <\/p>\n<ol>\n<li>\n<p><a href=\"#babel\" rel=\"noopener noreferrer nofollow\">\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 babel<\/a>?<\/p>\n<ul>\n<li>\n<p><a href=\"#ast\" rel=\"noopener noreferrer nofollow\">AST<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><a href=\"#write-own-plugin\" rel=\"noopener noreferrer nofollow\">\u041f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 \u043f\u043b\u0430\u0433\u0438\u043d<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#npm-publish\" rel=\"noopener noreferrer nofollow\">\u041f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d \u0432 npm<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#add-to-project\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d \u0432 \u043f\u0440\u043e\u0435\u043a\u0442<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#conclusion\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<\/ol>\n<h2>Babel &#8212; \u0447\u0442\u043e \u044d\u0442\u043e?<\/h2>\n<p><a class=\"anchor\" name=\"babel\" id=\"babel\"><\/a><\/p>\n<p>\u0418\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, <strong>babel<\/strong> &#8212; \u044d\u0442\u043e \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u0434 ECMAScript 2015+ \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e JavaScript.<br \/>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043b\u0430\u0433\u0438\u043d @babel\/plugin-transform-react-jsx \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442 *.jsx \u0432 *.js:<\/p>\n<p><strong>jsx<\/strong><\/p>\n<pre><code>const profile = (   &lt;div>     &lt;img src=\"avatar.png\" className=\"profile\" \/>     &lt;h3>{[user.firstName, user.lastName].join(\" \")}&lt;\/h3>   &lt;\/div> );<\/code><\/pre>\n<p><strong>js<\/strong><\/p>\n<pre><code>const profile = React.createElement(   \"div\",   null,   React.createElement(\"img\", { src: \"avatar.png\", className: \"profile\" }),   React.createElement(\"h3\", null, [user.firstName, user.lastName].join(\" \")) );<\/code><\/pre>\n<p>\u0412\u0441\u044f \u044d\u0442\u0430 \u043c\u0430\u0433\u0438\u044f \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f <a href=\"https:\/\/en.wikipedia.org\/wiki\/Abstract_syntax_tree\" rel=\"noopener noreferrer nofollow\">Abstract Syntax Tree (AST)<\/a>. \u0413\u0440\u0443\u0431\u043e \u0433\u043e\u0432\u043e\u0440\u044f, AST \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0430\u0448 \u043a\u043e\u0434 \u043a\u0430\u043a \u0434\u0435\u0440\u0435\u0432\u043e \u043d\u043e\u0434 (\u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432) \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438. <\/p>\n<p><a class=\"anchor\" name=\"ast\" id=\"ast\"><\/a><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/efa\/7ef\/04f\/efa7ef04f4b0ec289bce1d142422cc93.png\" alt=\"\u0412\u044b\u0437\u043e\u0432 greet('User') \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0432 AST \u0441\u043f\u0440\u0430\u0432\u0430\" title=\"\u0412\u044b\u0437\u043e\u0432 greet('User') \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0432 AST \u0441\u043f\u0440\u0430\u0432\u0430\" width=\"2880\" height=\"1440\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/efa\/7ef\/04f\/efa7ef04f4b0ec289bce1d142422cc93.png\"\/><figcaption>\u0412\u044b\u0437\u043e\u0432 greet(&#8216;User&#8217;) \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0432 AST \u0441\u043f\u0440\u0430\u0432\u0430<\/figcaption><\/figure>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f babel &#8212; \u044d\u0442\u043e \u0432\u0437\u044f\u0442\u044c \u0432\u0430\u0448 \u043a\u043e\u0434 -> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 AST -> \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0443\u0437\u043b\u043e\u0432 \u0434\u0435\u0440\u0435\u0432\u0430 -> \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0438\u0437 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e AST. <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/e47\/29d\/f92\/e4729df9287df10ec9561e955966d15c.png\" width=\"1634\" height=\"406\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/e47\/29d\/f92\/e4729df9287df10ec9561e955966d15c.png\"\/><figcaption><\/figcaption><\/figure>\n<h2>\u041f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 \u043f\u043b\u0430\u0433\u0438\u043d<\/h2>\n<p><a class=\"anchor\" name=\"write-own-plugin\" id=\"write-own-plugin\"><\/a><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043b\u0438\u0441\u044c \u0441 \u0442\u0435\u043c, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 babel \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043b\u0430\u0433\u0438\u043d\u0430. \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043b\u0430\u0433\u0438\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0431\u0440\u0430\u0442\u044c \u0441\u0442\u0438\u043b\u0438 \u0438\u0437 \u043a\u043e\u043d\u0444\u0438\u0433\u0430 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0438\u0445 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u043e\u0432 console. <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/cfa\/c25\/0dc\/cfac250dc58a961f03e8d30007e324ae.png\" alt=\"\u042d\u0442\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432 \u0438\u0442\u043e\u0433\u0435\" title=\"\u042d\u0442\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432 \u0438\u0442\u043e\u0433\u0435\" width=\"1114\" height=\"314\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/cfa\/c25\/0dc\/cfac250dc58a961f03e8d30007e324ae.png\"\/><figcaption>\u042d\u0442\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432 \u0438\u0442\u043e\u0433\u0435<\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code>mkdir babel-plugin-styled-console-output cd babel-plugin-styled-console-output touch index.js yarn add -D @babel\/core<\/code><\/pre>\n<p>\u0412 index.js \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code>const babel = require(\"@babel\/core\");  module.exports = function () {   const LogVisitor = {     CallExpression(path, state) {       const { callee, arguments } = path.node;        if (callee.object &amp;&amp; callee.object.name === \"console\") {         const styles = state.opts.types[callee.property.name];          if (styles) {           let parsedStylesToString = \"\";           let divider;            Object.keys(styles).forEach((key) => {             if (key === \"divider\") {               divider = styles[key];             } else {               parsedStylesToString += `${key}: ${styles[key]}; `;             }           });            const str = arguments             .map((arg) => {               if (arg.type === \"StringLiteral\") {                 return arg.value;               }             })             .join(divider || \" \");            const newArgs = [             babel.types.stringLiteral(`%c${str}`),             babel.types.stringLiteral(parsedStylesToString),           ];           path.node.arguments = newArgs;           path.node.callee.property.name = \"log\";         }       }     },   };    return { visitor: LogVisitor }; }; <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0439\u0434\u0435\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u043e \u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043c\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 \u0432\u044b\u0448\u0435:<\/p>\n<p>\u041d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 4 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 42. \u0422\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c visitor, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u043d\u0430\u0448\u0435\u043c\u0443 \u043a\u043e\u0434\u0443 \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 babel. <\/p>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 (\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 5) \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0434\u0438\u043d \u0438\u0437 \u0442\u0438\u043f\u043e\u0432 \u0432 AST<code>CallExpression<\/code>. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u043e\u0442\u043a\u0443\u0434\u0430 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u0437\u044f\u043b\u0441\u044f \u044d\u0442\u043e\u0442 CallExpression? \u0425\u043e\u0440\u043e\u0448\u0438\u0439 \u0432\u043e\u043f\u0440\u043e\u0441! <\/p>\n<p>\u0415\u0441\u0442\u044c \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 <a href=\"https:\/\/lihautan.com\/babel-ast-explorer\" rel=\"noopener noreferrer nofollow\">Babel AST explorer<\/a>. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u0442\u0443\u0434\u0430 \u0438 \u043d\u0430\u043f\u0438\u0448\u0435\u043c <code>console.log('text');<\/code><\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/85c\/fd5\/2aa\/85cfd52aaa0e0537ff263ba901b2829f.png\" width=\"2878\" height=\"1332\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/85c\/fd5\/2aa\/85cfd52aaa0e0537ff263ba901b2829f.png\"\/><figcaption><\/figcaption><\/figure>\n<p>\u041a\u043b\u0438\u043a\u043d\u0443\u0432 \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0443 1, \u0441\u043f\u0440\u0430\u0432\u0430 \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e AST. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0447\u0442\u043e \u043d\u0430\u0448 \u0432\u044b\u0437\u043e\u0432 <code>console.log<\/code> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 \u043a\u0430\u043a \u0443\u0437\u0435\u043b <code>CallExpression<\/code> \u0441 \u043f\u043e\u043b\u044f\u043c\u0438 <code>callee<\/code> \u0438 <code>arguments<\/code>.  \u042d\u0442\u0438 \u043f\u043e\u043b\u044f \u043c\u044b \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 6 \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>path.node<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c, \u0432\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u0432 \u043d\u0430\u0448 \u043a\u043e\u0434. \u041d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 8 \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>Console<\/code>. <br \/>\u041d\u0430 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 9 \u043c\u044b \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0430 (\u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>state.opts<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code>{   \"types\": {     \"error\": { \"color\": \"white\", \"background\": \"blue\", \"divider\": \"---\" },   } }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043d\u0430\u0448 \u043a\u043e\u043d\u0444\u0438\u0433 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443, \u0447\u0442\u043e \u0431\u044b \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u043a\u0430\u043a \u0432\u0442\u043e\u0440\u043e\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>console.log<\/code> (\u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>log<\/code>) <\/p>\n<pre><code>console.log('%csometext', 'color: red');<\/code><\/pre>\n<p>\u041d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 23 \u043c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0435 \u0432 <code>log<\/code> \u0432 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443 \u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c \u0438\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>divider<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0432 \u043a\u043e\u043d\u0444\u0438\u0433 (\u0438\u043b\u0438 <code>' '<\/code> \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e). <\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 31 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0438 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0441\u0442\u0430\u0440\u044b\u0435 \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 35. \u041d\u0430 36 \u0441\u0442\u0440\u043e\u043a\u0435 \u043c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043c <code>.warn<\/code>, <code>.error<\/code> \u0438 \u0442\u0434.  \u043d\u0430 <code>.log<\/code> (\u0442\u043a \u0441\u0442\u0438\u043b\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f <code>log<\/code> \u0444\u0443\u043d\u043a\u0446\u0438\u0438). <\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043c\u044b \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043c \u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u0438 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u0435\u0433\u043e \u0432 npm. <\/p>\n<h2>\u041f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d \u0432 npm<\/h2>\n<p><a class=\"anchor\" name=\"npm-publish\" id=\"npm-publish\"><\/a><\/p>\n<p>\u0414\u043b\u044f \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c yarn. \u0412 \u043a\u043e\u043d\u0441\u043e\u043b\u0435 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435: <\/p>\n<p><code>yarn init<\/code> \u0438 \u043e\u0442\u0432\u0435\u0442\u044c\u0442\u0435 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u044b. <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0441\u043b\u0435\u0434\u0443\u044f <a href=\"https:\/\/classic.yarnpkg.com\/en\/docs\/publishing-a-package#toc-logging-into-npm\" rel=\"noopener noreferrer nofollow\">\u044d\u0442\u043e\u0439<\/a> \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u043e\u0439\u0442\u0438\/\u0437\u0430\u0440\u0435\u0433\u0435\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 npm. <br \/>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0432\u044b \u0432\u043e\u0448\u043b\u0438, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435:<br \/><code>yarn publish<\/code>\u0438 \u0434\u043e\u0436\u0434\u0438\u0442\u0435\u0441\u044c \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438. <\/p>\n<p>\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u044e! \u041c\u044b \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b\u0438 \u043d\u0430\u0448 \u043f\u043b\u0430\u0433\u0438\u043d \u0432 npm. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0441\u0442\u0430\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c. <\/p>\n<h2>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d \u0432 \u043f\u0440\u043e\u0435\u043a\u0442<\/h2>\n<p><a class=\"anchor\" name=\"add-to-project\" id=\"add-to-project\"><\/a><\/p>\n<p>\u042f \u0432\u0437\u044f\u043b \u0441\u0432\u043e\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u043f\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430 React \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u043b\u0430\u0433\u0438\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e:<\/p>\n<pre><code>yarn add -D babel-formatted-log<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043e\u0442\u043a\u0440\u044b\u043b \u0444\u0430\u0439\u043b <code>.babelrc \/ babel.config.json<\/code> \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u0430:<\/p>\n<pre><code>{   \"plugins\": [     \/\/ ...     [       \"module:babel-formatted-log\",       {         \"types\": {           \"error\": { \"color\": \"white\", \"background\": \"blue\", \"divider\": \"---\" },           \"log\": {             \"border\": \"1px solid #d64021 \",             \"font-weight\": \"800\",             \"padding\": \"10px\"           }         }       }     ]   ] }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0433\u0434\u0435-\u043d\u0438\u0431\u0443\u0434\u044c:<\/p>\n<pre><code>  console.error(\"babel\", \"test\");    console.log(\"text1\", \"text2\", \"text3\");<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u044b\u0432\u043e\u0434 \u043f\u043e\u0445\u043e\u0436\u0438\u0439 \u043d\u0430 \u044d\u0442\u043e\u0442: <\/p>\n<figure class=\"full-width\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/habrastorage.org\/r\/w1560\/getpro\/habr\/upload_files\/bec\/165\/a61\/bec165a61a79300619df3402d85b1d6d.png\" width=\"1114\" height=\"314\" data-src=\"https:\/\/habrastorage.org\/getpro\/habr\/upload_files\/bec\/165\/a61\/bec165a61a79300619df3402d85b1d6d.png\"\/><figcaption><\/figcaption><\/figure>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p><a class=\"anchor\" name=\"conclusion\" id=\"conclusion\"><\/a><\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 \u0441\u0432\u043e\u0439 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 babel \u043f\u043b\u0430\u0433\u0438\u043d \u0438 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b\u0438 \u0435\u0433\u043e \u0432 npm. \u042d\u0442\u043e\u0442 \u043f\u043b\u0430\u0433\u0438\u043d \u043d\u0435 \u043d\u0435\u0441\u0435\u0442 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u043e\u043b\u044c\u0437\u044b, \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u043b\u0435\u043d\u0438\u044f \ud83d\ude42<\/p>\n<p>\u0412\u0435\u0441\u044c \u043a\u043e\u0434 \u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043f\u043e \u043f\u043b\u0430\u0433\u0438\u043d\u0443 \u043b\u0435\u0436\u0430\u0442 \u043d\u0430 <a href=\"https:\/\/github.com\/janglewood\/babel-formatted-log\" rel=\"noopener noreferrer nofollow\">Github<\/a>. <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0447\u0442\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u0438!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/704028\/\"> https:\/\/habr.com\/ru\/post\/704028\/<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<div><\/div>\n<div id=\"post-content-body\">\n<div>\n<div class=\"article-formatted-body article-formatted-body article-formatted-body_version-2\">\n<div xmlns=\"http:\/\/www.w3.org\/1999\/xhtml\">\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u0440\u0430\u0441\u0441\u043c\u043e\u0442\u0440\u0438\u043c \u043f\u0440\u043e\u0446\u0435\u0441\u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0432\u043e\u0435\u0433\u043e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043b\u0430\u0433\u0438\u043d\u0430 babel \u0438 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u0435\u0433\u043e \u0432 npm. <\/p>\n<ol>\n<li>\n<p><a href=\"#babel\" rel=\"noopener noreferrer nofollow\">\u0427\u0442\u043e \u0442\u0430\u043a\u043e\u0435 babel<\/a>?<\/p>\n<ul>\n<li>\n<p><a href=\"#ast\" rel=\"noopener noreferrer nofollow\">AST<\/a><\/p>\n<\/li>\n<\/ul>\n<\/li>\n<li>\n<p><a href=\"#write-own-plugin\" rel=\"noopener noreferrer nofollow\">\u041f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 \u043f\u043b\u0430\u0433\u0438\u043d<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#npm-publish\" rel=\"noopener noreferrer nofollow\">\u041f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d \u0432 npm<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#add-to-project\" rel=\"noopener noreferrer nofollow\">\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d \u0432 \u043f\u0440\u043e\u0435\u043a\u0442<\/a><\/p>\n<\/li>\n<li>\n<p><a href=\"#conclusion\" rel=\"noopener noreferrer nofollow\">\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/a><\/p>\n<\/li>\n<\/ol>\n<h2>Babel &#8212; \u0447\u0442\u043e \u044d\u0442\u043e?<\/h2>\n<p><a class=\"anchor\" name=\"babel\" id=\"babel\"><\/a><\/p>\n<p>\u0418\u0441\u0445\u043e\u0434\u044f \u0438\u0437 \u043e\u0444\u0438\u0446\u0438\u0430\u043b\u044c\u043d\u043e\u0439 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u0438, <strong>babel<\/strong> &#8212; \u044d\u0442\u043e \u043d\u0430\u0431\u043e\u0440 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u0435\u0442 \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0442\u044c \u043a\u043e\u0434 ECMAScript 2015+ \u0432 \u043e\u0431\u0440\u0430\u0442\u043d\u043e \u0441\u043e\u0432\u043c\u0435\u0441\u0442\u0438\u043c\u0443\u044e \u0432\u0435\u0440\u0441\u0438\u044e JavaScript.<br \/>\u041d\u0430\u043f\u0440\u0438\u043c\u0435\u0440, \u043f\u043b\u0430\u0433\u0438\u043d @babel\/plugin-transform-react-jsx \u043f\u0440\u0435\u043e\u0431\u0440\u0430\u0437\u043e\u0432\u044b\u0432\u0430\u0435\u0442 *.jsx \u0432 *.js:<\/p>\n<p><strong>jsx<\/strong><\/p>\n<pre><code>const profile = (   &lt;div>     &lt;img src=\"avatar.png\" className=\"profile\" \/>     &lt;h3>{[user.firstName, user.lastName].join(\" \")}&lt;\/h3>   &lt;\/div> );<\/code><\/pre>\n<p><strong>js<\/strong><\/p>\n<pre><code>const profile = React.createElement(   \"div\",   null,   React.createElement(\"img\", { src: \"avatar.png\", className: \"profile\" }),   React.createElement(\"h3\", null, [user.firstName, user.lastName].join(\" \")) );<\/code><\/pre>\n<p>\u0412\u0441\u044f \u044d\u0442\u0430 \u043c\u0430\u0433\u0438\u044f \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u0431\u043b\u0430\u0433\u043e\u0434\u0430\u0440\u044f <a href=\"https:\/\/en.wikipedia.org\/wiki\/Abstract_syntax_tree\" rel=\"noopener noreferrer nofollow\">Abstract Syntax Tree (AST)<\/a>. \u0413\u0440\u0443\u0431\u043e \u0433\u043e\u0432\u043e\u0440\u044f, AST \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u0432\u0430\u0448 \u043a\u043e\u0434 \u043a\u0430\u043a \u0434\u0435\u0440\u0435\u0432\u043e \u043d\u043e\u0434 (\u043e\u0431\u044a\u0435\u043a\u0442\u043e\u0432) \u0441 \u043e\u043f\u0440\u0435\u0434\u0435\u043b\u0435\u043d\u043d\u044b\u043c\u0438 \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430\u043c\u0438. <\/p>\n<p><a class=\"anchor\" name=\"ast\" id=\"ast\"><\/a><\/p>\n<figure class=\"full-width\"><figcaption>\u0412\u044b\u0437\u043e\u0432 greet(&#8216;User&#8217;) \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0432 AST \u0441\u043f\u0440\u0430\u0432\u0430<\/figcaption><\/figure>\n<p>\u041e\u0441\u043d\u043e\u0432\u043d\u0430\u044f \u0438\u0434\u0435\u044f babel &#8212; \u044d\u0442\u043e \u0432\u0437\u044f\u0442\u044c \u0432\u0430\u0448 \u043a\u043e\u0434 -> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 AST -> \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u0442\u0440\u0430\u043d\u0441\u0444\u043e\u0440\u043c\u0430\u0446\u0438\u044e \u0443\u0437\u043b\u043e\u0432 \u0434\u0435\u0440\u0435\u0432\u0430 -> \u0441\u0433\u0435\u043d\u0435\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u043a\u043e\u0434 \u0438\u0437 \u043e\u0431\u043d\u043e\u0432\u043b\u0435\u043d\u043d\u043e\u0433\u043e AST. <\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>\u041f\u0438\u0448\u0435\u043c \u0441\u0432\u043e\u0439 \u043f\u043b\u0430\u0433\u0438\u043d<\/h2>\n<p><a class=\"anchor\" name=\"write-own-plugin\" id=\"write-own-plugin\"><\/a><\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043f\u043e\u0437\u043d\u0430\u043a\u043e\u043c\u0438\u043b\u0438\u0441\u044c \u0441 \u0442\u0435\u043c, \u043a\u0430\u043a \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 babel \u043c\u044b \u043c\u043e\u0436\u0435\u043c \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u0442\u044c \u043a \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044e \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u043e\u0433\u043e \u043f\u043b\u0430\u0433\u0438\u043d\u0430. \u0412 \u044d\u0442\u043e\u043c \u043f\u0440\u0438\u043c\u0435\u0440\u0435 \u043f\u043b\u0430\u0433\u0438\u043d \u0431\u0443\u0434\u0435\u0442 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0442\u044c \u0431\u0440\u0430\u0442\u044c \u0441\u0442\u0438\u043b\u0438 \u0438\u0437 \u043a\u043e\u043d\u0444\u0438\u0433\u0430 \u0438 \u043f\u0440\u0438\u043c\u0435\u043d\u044f\u0442\u044c \u0438\u0445 \u0434\u043b\u044f \u0432\u044b\u0432\u043e\u0434\u043e\u0432 console. <\/p>\n<figure class=\"full-width\"><figcaption>\u042d\u0442\u043e \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432 \u0438\u0442\u043e\u0433\u0435<\/figcaption><\/figure>\n<p>\u0414\u043b\u044f \u043d\u0430\u0447\u0430\u043b \u0441\u043e\u0437\u0434\u0430\u0434\u0438\u043c \u043d\u043e\u0432\u044b\u0439 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u044b\u0435 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438:<\/p>\n<pre><code>mkdir babel-plugin-styled-console-output cd babel-plugin-styled-console-output touch index.js yarn add -D @babel\/core<\/code><\/pre>\n<p>\u0412 index.js \u0434\u043e\u0431\u0430\u0432\u0438\u043c \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u0439 \u043a\u043e\u0434:<\/p>\n<pre><code>const babel = require(\"@babel\/core\");  module.exports = function () {   const LogVisitor = {     CallExpression(path, state) {       const { callee, arguments } = path.node;        if (callee.object &amp;&amp; callee.object.name === \"console\") {         const styles = state.opts.types[callee.property.name];          if (styles) {           let parsedStylesToString = \"\";           let divider;            Object.keys(styles).forEach((key) => {             if (key === \"divider\") {               divider = styles[key];             } else {               parsedStylesToString += `${key}: ${styles[key]}; `;             }           });            const str = arguments             .map((arg) => {               if (arg.type === \"StringLiteral\") {                 return arg.value;               }             })             .join(divider || \" \");            const newArgs = [             babel.types.stringLiteral(`%c${str}`),             babel.types.stringLiteral(parsedStylesToString),           ];           path.node.arguments = newArgs;           path.node.callee.property.name = \"log\";         }       }     },   };    return { visitor: LogVisitor }; }; <\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u043f\u0440\u043e\u0439\u0434\u0435\u043c \u043f\u043e\u0434\u0440\u043e\u0431\u043d\u0435\u0435 \u043f\u043e \u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043c\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 \u0432\u044b\u0448\u0435:<\/p>\n<p>\u041d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 4 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043e\u0431\u044a\u0435\u043a\u0442, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u043e\u0437\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 42. \u0422\u0435\u043c \u0441\u0430\u043c\u044b\u043c \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c visitor, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0445\u043e\u0434\u0438\u0442\u044c \u043f\u043e \u043d\u0430\u0448\u0435\u043c\u0443 \u043a\u043e\u0434\u0443 \u0432 \u043c\u043e\u043c\u0435\u043d\u0442 \u0437\u0430\u043f\u0443\u0441\u043a\u0430 babel. <\/p>\n<p>\u0412\u043d\u0443\u0442\u0440\u0438 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 (\u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 5) \u043c\u044b \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u043c \u043e\u0434\u0438\u043d \u0438\u0437 \u0442\u0438\u043f\u043e\u0432 \u0432 AST<code>CallExpression<\/code>. \u0412\u044b \u043c\u043e\u0436\u0435\u0442\u0435 \u0441\u043f\u0440\u043e\u0441\u0438\u0442\u044c \u043e\u0442\u043a\u0443\u0434\u0430 \u0432\u043e\u043e\u0431\u0449\u0435 \u0432\u0437\u044f\u043b\u0441\u044f \u044d\u0442\u043e\u0442 CallExpression? \u0425\u043e\u0440\u043e\u0448\u0438\u0439 \u0432\u043e\u043f\u0440\u043e\u0441! <\/p>\n<p>\u0415\u0441\u0442\u044c \u0437\u0430\u043c\u0435\u0447\u0430\u0442\u0435\u043b\u044c\u043d\u044b\u0439 \u0438\u043d\u0441\u0442\u0440\u0443\u043c\u0435\u043d\u0442 <a href=\"https:\/\/lihautan.com\/babel-ast-explorer\" rel=\"noopener noreferrer nofollow\">Babel AST explorer<\/a>. \u0414\u0430\u0432\u0430\u0439\u0442\u0435 \u043f\u0435\u0440\u0435\u0439\u0434\u0435\u043c \u0442\u0443\u0434\u0430 \u0438 \u043d\u0430\u043f\u0438\u0448\u0435\u043c <code>console.log('text');<\/code><\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<p>\u041a\u043b\u0438\u043a\u043d\u0443\u0432 \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0443 1, \u0441\u043f\u0440\u0430\u0432\u0430 \u043c\u044b \u0443\u0432\u0438\u0434\u0438\u043c \u0432\u044b\u0448\u0435\u0443\u043f\u043e\u043c\u044f\u043d\u0443\u0442\u043e\u0435 \u0434\u0435\u0440\u0435\u0432\u043e AST. \u0422\u0435\u043f\u0435\u0440\u044c \u043c\u044b \u0432\u0438\u0434\u0438\u043c \u0447\u0442\u043e \u043d\u0430\u0448 \u0432\u044b\u0437\u043e\u0432 <code>console.log<\/code> \u043f\u0440\u0435\u0434\u0441\u0442\u0430\u0432\u043b\u0435\u043d \u0432 \u0434\u0435\u0440\u0435\u0432\u0435 \u043a\u0430\u043a \u0443\u0437\u0435\u043b <code>CallExpression<\/code> \u0441 \u043f\u043e\u043b\u044f\u043c\u0438 <code>callee<\/code> \u0438 <code>arguments<\/code>.  \u042d\u0442\u0438 \u043f\u043e\u043b\u044f \u043c\u044b \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 6 \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u0434\u0430 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>path.node<\/code>.<\/p>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c, \u043a\u043e\u0433\u0434\u0430 \u043c\u044b \u043d\u0435\u043c\u043d\u043e\u0433\u043e \u043f\u043e\u043d\u0438\u043c\u0430\u0435\u043c \u0447\u0442\u043e \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c, \u0432\u0435\u0440\u043d\u0435\u043c\u0441\u044f \u0432 \u043d\u0430\u0448 \u043a\u043e\u0434. \u041d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 8 \u043c\u044b \u043f\u0440\u043e\u0432\u0435\u0440\u044f\u0435\u043c \u0447\u0442\u043e \u0443 \u043d\u0430\u0441 \u0434\u0435\u0439\u0441\u0442\u0432\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0432\u044b\u0437\u044b\u0432\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u0442\u043e\u0434 \u043e\u0431\u044a\u0435\u043a\u0442\u0430 <code>Console<\/code>. <br \/>\u041d\u0430 \u0441\u0442\u0440\u043e\u0447\u043a\u0435 9 \u043c\u044b \u0434\u043e\u0441\u0442\u0430\u0435\u043c \u0434\u0430\u043d\u043d\u044b\u0435 \u0438\u0437 \u043d\u0430\u0448\u0435\u0433\u043e \u043a\u043e\u043d\u0444\u0438\u0433\u0430 (\u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>state.opts<\/code>), \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0432\u044b\u0433\u043b\u044f\u0434\u0438\u0442 \u0441\u043b\u0435\u0434\u0443\u044e\u0449\u0438\u043c \u043e\u0431\u0440\u0430\u0437\u043e\u043c:<\/p>\n<pre><code>{   \"types\": {     \"error\": { \"color\": \"white\", \"background\": \"blue\", \"divider\": \"---\" },   } }<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043c\u044b \u043f\u0440\u0435\u0432\u0440\u0430\u0449\u0430\u0435\u043c \u043d\u0430\u0448 \u043a\u043e\u043d\u0444\u0438\u0433 \u0432 \u0441\u0442\u0440\u043e\u043a\u0443, \u0447\u0442\u043e \u0431\u044b \u0432 \u0434\u0430\u043b\u044c\u043d\u0435\u0439\u0448\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c \u0435\u0435 \u043a\u0430\u043a \u0432\u0442\u043e\u0440\u043e\u0439 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u043f\u0440\u0438 \u0432\u044b\u0437\u043e\u0432\u0435 <code>console.log<\/code> (\u0432\u0441\u0442\u0440\u043e\u0435\u043d\u043d\u0430\u044f \u0432\u043e\u0437\u043c\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u0444\u0443\u043d\u043a\u0446\u0438\u0438 <code>log<\/code>) <\/p>\n<pre><code>console.log('%csometext', 'color: red');<\/code><\/pre>\n<p>\u041d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 23 \u043c\u044b \u0441\u043e\u0431\u0438\u0440\u0430\u0435\u043c \u0432\u0441\u0435 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u044b \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u044b\u0435 \u0432 <code>log<\/code> \u0432 \u043e\u0434\u043d\u0443 \u0441\u0442\u0440\u043e\u043a\u0443 \u0438 \u0440\u0430\u0437\u0434\u0435\u043b\u044f\u0435\u043c \u0438\u0445 \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e <code>divider<\/code> \u043f\u0435\u0440\u0435\u0434\u0430\u043d\u043d\u043e\u0433\u043e \u0432 \u043a\u043e\u043d\u0444\u0438\u0433 (\u0438\u043b\u0438 <code>' '<\/code> \u043f\u043e \u0443\u043c\u043e\u043b\u0447\u0430\u043d\u0438\u044e). <\/p>\n<p>\u0417\u0430\u0442\u0435\u043c \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 31 \u043c\u044b \u0441\u043e\u0437\u0434\u0430\u0435\u043c \u043d\u043e\u0432\u044b\u0439 \u043c\u0430\u0441\u0441\u0438\u0432 \u0441 \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u0430\u043c\u0438 \u0438 \u0437\u0430\u043c\u0435\u043d\u044f\u0435\u043c \u0441\u0442\u0430\u0440\u044b\u0435 \u043d\u0430 \u0441\u0442\u0440\u043e\u043a\u0435 35. \u041d\u0430 36 \u0441\u0442\u0440\u043e\u043a\u0435 \u043c\u044b \u0437\u0430\u043c\u0435\u043d\u0438\u043c <code>.warn<\/code>, <code>.error<\/code> \u0438 \u0442\u0434.  \u043d\u0430 <code>.log<\/code> (\u0442\u043a \u0441\u0442\u0438\u043b\u0438 \u0434\u043e\u0441\u0442\u0443\u043f\u043d\u044b \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f <code>log<\/code> \u0444\u0443\u043d\u043a\u0446\u0438\u0438). <\/p>\n<p>\u041d\u0430 \u044d\u0442\u043e\u043c \u043c\u044b \u0437\u0430\u043a\u043e\u043d\u0447\u0438\u043c \u0441 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u0438 \u043f\u0440\u0438\u0441\u0442\u0443\u043f\u0438\u043c \u043a \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u0435\u0433\u043e \u0432 npm. <\/p>\n<h2>\u041f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d \u0432 npm<\/h2>\n<p><a class=\"anchor\" name=\"npm-publish\" id=\"npm-publish\"><\/a><\/p>\n<p>\u0414\u043b\u044f \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u043c\u044b \u0431\u0443\u0434\u0435\u043c \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c yarn. \u0412 \u043a\u043e\u043d\u0441\u043e\u043b\u0435 \u0432\u0430\u0448\u0435\u0433\u043e \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043d\u0430\u043f\u0438\u0448\u0438\u0442\u0435: <\/p>\n<p><code>yarn init<\/code> \u0438 \u043e\u0442\u0432\u0435\u0442\u044c\u0442\u0435 \u043d\u0430 \u0432\u043e\u043f\u0440\u043e\u0441\u044b. <\/p>\n<p>\u0414\u0430\u043b\u0435\u0435, \u0441\u043b\u0435\u0434\u0443\u044f <a href=\"https:\/\/classic.yarnpkg.com\/en\/docs\/publishing-a-package#toc-logging-into-npm\" rel=\"noopener noreferrer nofollow\">\u044d\u0442\u043e\u0439<\/a> \u0438\u043d\u0441\u0442\u0440\u0443\u043a\u0446\u0438\u0438, \u043c\u044b \u0434\u043e\u043b\u0436\u043d\u044b \u0432\u043e\u0439\u0442\u0438\/\u0437\u0430\u0440\u0435\u0433\u0435\u0441\u0442\u0440\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 npm. <br \/>\u041f\u043e\u0441\u043b\u0435 \u0442\u043e\u0433\u043e, \u043a\u0430\u043a \u0432\u044b \u0432\u043e\u0448\u043b\u0438, \u0437\u0430\u043f\u0443\u0441\u0442\u0438\u0442\u0435:<br \/><code>yarn publish<\/code>\u0438 \u0434\u043e\u0436\u0434\u0438\u0442\u0435\u0441\u044c \u043e\u043a\u043e\u043d\u0447\u0430\u043d\u0438\u044f \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438. <\/p>\n<p>\u041f\u043e\u0437\u0434\u0440\u0430\u0432\u043b\u044f\u044e! \u041c\u044b \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b\u0438 \u043d\u0430\u0448 \u043f\u043b\u0430\u0433\u0438\u043d \u0432 npm. \u0422\u0435\u043f\u0435\u0440\u044c \u043d\u0430\u0441\u0442\u0430\u043b\u043e \u0432\u0440\u0435\u043c\u044f \u0434\u043e\u0431\u0430\u0432\u0438\u0442\u044c \u0435\u0433\u043e \u0432 \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u043d\u0430\u0441\u0442\u0440\u043e\u0438\u0442\u044c. <\/p>\n<h2>\u0414\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d \u0432 \u043f\u0440\u043e\u0435\u043a\u0442<\/h2>\n<p><a class=\"anchor\" name=\"add-to-project\" id=\"add-to-project\"><\/a><\/p>\n<p>\u042f \u0432\u0437\u044f\u043b \u0441\u0432\u043e\u0439 \u043f\u0435\u0440\u0432\u044b\u0439 \u043f\u043e\u043f\u0430\u0432\u0448\u0438\u0439\u0441\u044f \u043f\u0440\u043e\u0435\u043a\u0442 \u043d\u0430 React \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u0441\u043e\u0437\u0434\u0430\u043d\u043d\u044b\u0439 \u043f\u043b\u0430\u0433\u0438\u043d \u0441 \u043f\u043e\u043c\u043e\u0449\u044c\u044e:<\/p>\n<pre><code>yarn add -D babel-formatted-log<\/code><\/pre>\n<p>\u0414\u0430\u043b\u0435\u0435 \u043e\u0442\u043a\u0440\u044b\u043b \u0444\u0430\u0439\u043b <code>.babelrc \/ babel.config.json<\/code> \u0438 \u0434\u043e\u0431\u0430\u0432\u0438\u043b \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044e \u0434\u043b\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u0430:<\/p>\n<pre><code>{   \"plugins\": [     \/\/ ...     [       \"module:babel-formatted-log\",       {         \"types\": {           \"error\": { \"color\": \"white\", \"background\": \"blue\", \"divider\": \"---\" },           \"log\": {             \"border\": \"1px solid #d64021 \",             \"font-weight\": \"800\",             \"padding\": \"10px\"           }         }       }     ]   ] }<\/code><\/pre>\n<p>\u0422\u0435\u043f\u0435\u0440\u044c \u0441\u043e\u0431\u0435\u0440\u0435\u043c \u043f\u0440\u043e\u0435\u043a\u0442 \u0438 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u0433\u0434\u0435-\u043d\u0438\u0431\u0443\u0434\u044c:<\/p>\n<pre><code>  console.error(\"babel\", \"test\");    console.log(\"text1\", \"text2\", \"text3\");<\/code><\/pre>\n<p>\u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0432\u044b\u0432\u043e\u0434 \u043f\u043e\u0445\u043e\u0436\u0438\u0439 \u043d\u0430 \u044d\u0442\u043e\u0442: <\/p>\n<figure class=\"full-width\"><figcaption><\/figcaption><\/figure>\n<h2>\u0417\u0430\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435<\/h2>\n<p><a class=\"anchor\" name=\"conclusion\" id=\"conclusion\"><\/a><\/p>\n<p>\u0421\u0435\u0433\u043e\u0434\u043d\u044f \u043c\u044b \u043d\u0430\u043f\u0438\u0441\u0430\u043b\u0438 \u0441\u0432\u043e\u0439 \u0441\u043e\u0431\u0441\u0442\u0432\u0435\u043d\u043d\u044b\u0439 babel \u043f\u043b\u0430\u0433\u0438\u043d \u0438 \u043e\u043f\u0443\u0431\u043b\u0438\u043a\u043e\u0432\u0430\u043b\u0438 \u0435\u0433\u043e \u0432 npm. \u042d\u0442\u043e\u0442 \u043f\u043b\u0430\u0433\u0438\u043d \u043d\u0435 \u043d\u0435\u0441\u0435\u0442 \u043a\u0430\u043a\u043e\u0439-\u043b\u0438\u0431\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u043e\u0439 \u043f\u043e\u043b\u044c\u0437\u044b, \u043e\u043d \u0441\u043e\u0437\u0434\u0430\u043d \u0432 \u0440\u0430\u043c\u043a\u0430\u0445 \u043e\u0437\u043d\u0430\u043a\u043e\u043c\u043b\u0435\u043d\u0438\u044f \ud83d\ude42<\/p>\n<p>\u0412\u0435\u0441\u044c \u043a\u043e\u0434 \u0438 \u0434\u043e\u043a\u0443\u043c\u0435\u043d\u0442\u0430\u0446\u0438\u044f \u043f\u043e \u043f\u043b\u0430\u0433\u0438\u043d\u0443 \u043b\u0435\u0436\u0430\u0442 \u043d\u0430 <a href=\"https:\/\/github.com\/janglewood\/babel-formatted-log\" rel=\"noopener noreferrer nofollow\">Github<\/a>. <\/p>\n<p>\u0421\u043f\u0430\u0441\u0438\u0431\u043e \u0447\u0442\u043e \u043f\u0440\u043e\u0447\u0438\u0442\u0430\u043b\u0438!<\/p>\n<\/p>\n<\/div>\n<\/div>\n<\/div>\n<p> <!----> <!----><\/div>\n<p> <!----> <!----><br \/> \u0441\u0441\u044b\u043b\u043a\u0430 \u043d\u0430 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b \u0441\u0442\u0430\u0442\u044c\u0438 <a href=\"https:\/\/habr.com\/ru\/post\/704028\/\"> https:\/\/habr.com\/ru\/post\/704028\/<\/a><br \/><\/br><\/br><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[],"tags":[],"class_list":["post-342288","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/342288","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=342288"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/342288\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=342288"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=342288"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=342288"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}