{"id":351814,"date":"2024-05-20T21:56:29","date_gmt":"2024-05-20T21:56:29","guid":{"rendered":"http:\/\/savepearlharbor.com\/?p=351814"},"modified":"-0001-11-30T00:00:00","modified_gmt":"-0001-11-29T21:00:00","slug":"","status":"publish","type":"post","link":"https:\/\/savepearlharbor.com\/?p=351814","title":{"rendered":"<span>\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 Vite \u043f\u043b\u0430\u0433\u0438\u043d\u0430<\/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\u0431\u043e\u0440\u0449\u0438\u043a Vite \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b, \u043d\u043e \u0438 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 API \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0445 \u043a\u0430\u0441\u0442\u043e\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u0434 \u043b\u044e\u0431\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443. \u0422\u043e \u0435\u0441\u0442\u044c, \u043f\u043b\u0430\u0433\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0445 \u0432 <code>npmjs.com<\/code> \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, \u043d\u043e \u0438 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0432\u043e\u0438\u0445 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<p>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u0441\u0440\u0430\u0432\u043d\u0438\u043c\u0430 \u0441\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f \u0434\u043b\u044f Gulp \u0438\u043b\u0438 GitHub Actions. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0434\u0430 \u0432 \u0444\u0430\u0439\u043b index.html. \u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u043a\u043e\u0434 Google Analytics, \u043c\u0435\u0442\u0430\u0442\u044d\u0433\u0438 Open Graph \u0438 Twitter, \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 Service worker-a, \u0432\u0438\u0434\u0436\u0435\u0442\u0430 \u0447\u0430\u0442\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438, \u0441\u043f\u043b\u044d\u0448 \u0441\u043a\u0440\u0438\u043d \u0438 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 index.html \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043d\u0435\u043c \u0438 \u0431\u043b\u043e\u043a\u0430\u0445 \u043a\u043e\u0434\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<p>\u041d\u0430\u0448 \u043f\u043b\u0430\u0433\u0438\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442\u044c \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0434\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u0430\u0445, \u0430 \u043f\u0440\u0438 \u0441\u0431\u043e\u0440\u043a\u0435 \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c\u0441\u044f \u0432 <code>index.html<\/code>. \u041f\u0440\u0438\u0447\u0435\u043c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u0431\u043e\u0440\u043a\u0435 ( npm build ), \u043d\u043e \u0438 \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 Vite dev \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 HRM (Hot Module Replacement).<\/p>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 &#8212; <a href=\"https:\/\/github.com\/altrusl\/vite-plugin-html-injection\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/altrusl\/vite-plugin-html-injection<\/a><br \/>NPM &#8212; <a href=\"https:\/\/www.npmjs.com\/package\/vite-plugin-html-injection\" rel=\"noopener noreferrer nofollow\">https:\/\/www.npmjs.com\/package\/vite-plugin-html-injection<\/a><\/p>\n<h3>\u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432 Vite<\/h3>\n<p>\u041f\u043b\u0430\u0433\u0438\u043d\u044b Vite \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432 \u0441\u0431\u043e\u0440\u0449\u0438\u043a\u0430 Rollup, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u043a\u0438 Vite \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c. \u0417\u0430 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d\u044b Rollup \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432 Vite, \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u0443\u043a\u043e\u0432 \u0432 API \u0434\u043b\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043f\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u043c\u0443 \u0441\u0447\u0435\u0442\u0443 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0445\u0443\u043a\u043e\u0432. \u041c\u044b \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u043c \u0434\u0432\u0430 \u0438\u0437 \u043d\u0438\u0445. <\/p>\n<h3>vite-plugin-html-injection<\/h3>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0445\u0443\u043a &#8212; <code>configResolved.<\/code> \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433 Vite, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043f\u043e\u0437\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u043a \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 &#8212; <code>config.root<\/code><\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0445\u0443\u043a &#8212; <code>transformIndexHtml<\/code>.  \u0412 \u043d\u0435\u043c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e <code>index.html<\/code>. \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c <code>index.html<\/code>, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u0430\u0434\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043d\u0442.<\/p>\n<pre><code class=\"typescript\">import { Plugin, ResolvedConfig } from \"vite\"; import path from \"path\"; import fs from \"fs\"; import { IHtmlInjectionConfig, IHtmlInjectionConfigInjection } from \".\/types\";  export function htmlInjectionPlugin(   htmlInjectionConfig: IHtmlInjectionConfig ): Plugin {   let config: undefined | ResolvedConfig;    return {     name: \"html-injection\",      configResolved(resolvedConfig) {       config = resolvedConfig;     },      transformIndexHtml(html: string) {       let out = html;       for (let i = 0; i &lt; htmlInjectionConfig.injections.length; i++) {         const injection: IHtmlInjectionConfigInjection =           htmlInjectionConfig.injections[i];          let root = (config as ResolvedConfig).root;         const filePath = path.resolve(root, injection.path);         let data = fs.readFileSync(filePath, \"utf8\");         if (injection.type === \"js\") {           data = `&lt;script>\\n${data}\\n&lt;\/script>\\n`;         } else if (injection.type === \"css\") {           data = `&lt;style>\\n${data}\\n&lt;\/style>\\n`;         }         switch (injection.injectTo) {           case \"head\":             out = out.replace(\"&lt;\/head>\", `${data}\\n&lt;\/head>`);             break;           case \"head-prepend\":             out = out.replace(\/&lt;head(.*)>\/i, `$&amp;\\n${data}`);             break;           case \"body\":             out = out.replace(\"&lt;\/body>\", `${data}\\n&lt;\/body>`);             break;           case \"body-prepend\":             out = out.replace(\/&lt;body(.*)>\/i, `$&amp;\\n${data}`);             break;            default:             break;         }       }        return out;     },   }; } <\/code><\/pre>\n<p>\u0412 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 <code>htmlInjectionConfig.injections<\/code> \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u0434\u0430 &#8212; \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u0430, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u0430\u044f \u0435\u043c\u0443 \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0432 <code>vite.config.js<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0435\u0433\u043e \u043d\u0430\u0448 \u043f\u043b\u0430\u0433\u0438\u043d.<\/p>\n<pre><code class=\"javascript\">\/\/ vite.config.js \/\/ example for Vue project  import { defineConfig } from \"vite\"; import vue from \"@vitejs\/plugin-vue\"; import { htmlInjectionPlugin } from \"vite-plugin-html-injection\";  export default defineConfig({   plugins: [     vue(),     htmlInjectionPlugin({       \/\/ Example configuration. Can be stored in a separate json file.       injections: [         {           name: \"Open Graph\",           path: \".\/src\/injections\/open-graph.html\",           type: \"raw\",           injectTo: \"head\",         },         {           name: \"Splash screen\",           path: \".\/src\/injections\/splash-screen.html\",           type: \"raw\",           injectTo: \"body-prepend\",         },         {           name: \"Service worker\",           path: \".\/src\/injections\/sw.js\",           type: \"js\",           injectTo: \"head\",         },       ],     }),   ] }); <\/code><\/pre>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f &#171;\u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e&#187; \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u0434\u043b\u044f \u0441\u0435\u0431\u044f, \u0432\u043c\u0435\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0435\u0433\u043e \u0432 <code>devDependencies<\/code> \u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u0438\u0437 <code>node_modules<\/code>:<\/p>\n<pre><code class=\"bash\">import { htmlInjectionPlugin } from \"vite-plugin-html-injection\";<\/code><\/pre>\n<p>\u043c\u043e\u0436\u043d\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e:<\/p>\n<pre><code class=\"bash\">import { htmlInjectionPlugin } from \".\/src\/plugins\/vite-plugin-html-injection\";<\/code><\/pre>\n<h3>\u0422\u0438\u043f\u044b \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u0434\u0430 <\/h3>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u0440\u0438 \u0442\u0438\u043f\u0430 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u0434\u0430, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043b\u0430\u0433\u0438\u043d \u2014 <code>raw<\/code>, <code>js<\/code> \u0438 <code>css<\/code>. <\/p>\n<p><code>Raw<\/code> \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u0435\u0441\u0442\u044c, <code>js<\/code> \u0438 <code>css<\/code> \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0442\u0435\u0433\u0438 <code>&lt;script><\/code> \u0438 <code>&lt;style><\/code> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u0447\u0435\u0442\u044b\u0440\u0435 \u043c\u0435\u0441\u0442\u0430, \u043a\u0443\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430: \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u0442\u0435\u0433\u0430 <code>head<\/code> \u0438 \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 <code>body<\/code>. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <code>injectTo<\/code>: <code>head-prepend<\/code>, <code>head<\/code>, <code>body-prepend<\/code> \u0438 <code>body<\/code>.<\/p>\n<p>\u0424\u0430\u0439\u043b <code>.\/src\/injections\/ga.html<\/code> \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"javascript\">&lt;!-- Google tag (gtag.js) --> &lt;script async src=\"https:\/\/www.googletagmanager.com\/gtag\/js?id=G-8W4X32XXXX\" \/> &lt;script>   window.dataLayer = window.dataLayer || [];   function gtag() {     dataLayer.push(arguments);   }   gtag(\"js\", new Date());    gtag(\"config\", \"G-8W4X32XXXX\"); &lt;\/script><\/code><\/pre>\n<h3>\u0421\u0431\u043e\u0440\u043a\u0430 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043a\u0430\u043a \u043f\u0430\u043a\u0435\u0442\u0430<\/h3>\n<p>\u0412 \u043a\u043e\u0434\u0435 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>fs<\/code> \u0438 <code>path<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0441 \u0441\u0431\u043e\u0440\u043a\u0443 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0441\u0430\u043c\u044b\u043c Vite \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u0430. \u0412 <code>vite.config.js<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u044f\u0432\u043d\u043e.<\/p>\n<pre><code class=\"javascript\">\/\/ vite.config.js  import { defineConfig } from \"vite\"; import { resolve } from \"path\";  export default defineConfig({   plugins: [],   build: {     lib: {       entry: resolve(__dirname, \".\/index.ts\"),       name: \"HtmlInjection\",       fileName: \"index\",       formats: [\"es\", \"cjs\"],     },     rollupOptions: {       external: [\"fs\", \"path\"],     },   }, }); <\/code><\/pre>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u043d\u044c\u0448\u0435 1\u041a\u0411.<\/p>\n<p>\u041d\u0443 \u0438 <code>package.json<\/code> \u043f\u043b\u0430\u0433\u0438\u043d\u0430:<\/p>\n<pre><code>{   \"name\": \"vite-plugin-html-injection\",   \"version\": \"1.1.9\",   \"description\": \"Vite plugin for injecting html, js, css code snippets into index.html\",   \"license\": \"MIT\",   \"homepage\": \"https:\/\/github.com\/altrusl\/vite-plugin-html-injection\",   \"type\": \"module\",   \"main\": \".\/dist\/index.cjs\",   \"module\": \".\/dist\/index.js\",   \"types\": \".\/dist\/index.d.ts\",   \"exports\": {     \".\": {       \"import\": \".\/dist\/index.js\",       \"require\": \".\/dist\/index.cjs\",       \"types\": \".\/dist\/index.d.ts\"     }   },   \"files\": [     \"dist\"   ],   \"scripts\": {     \"build\": \"vite build &amp;&amp; copy types.d.ts dist\\\\index.d.ts\"   },   \"devDependencies\": {     \"@antfu\/eslint-config\": \"^0.39.8\",     \"@types\/node\": \"^20.4.5\",     \"eslint\": \"^8.46.0\",     \"typescript\": \"^5.1.6\",     \"vite\": \"^4.4.7\"   },   \"peerDependencies\": {     \"vite\": \">= 2.0.0\"   } }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0441\u0431\u043e\u0440\u043a\u0438 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u0435\u0433\u043e \u043d\u0430 npmjs.com:<\/p>\n<pre><code class=\"bash\">npm publish --access public<\/code><\/pre>\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\/articles\/753788\/\"> https:\/\/habr.com\/ru\/articles\/753788\/<\/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\u0431\u043e\u0440\u0449\u0438\u043a Vite \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0445\u043e\u0440\u043e\u0448\u0438\u0439 \u0444\u0443\u043d\u043a\u0446\u0438\u043e\u043d\u0430\u043b, \u043d\u043e \u0438 \u0443\u0434\u043e\u0431\u043d\u044b\u0439 API \u0434\u043b\u044f \u0441\u043e\u0437\u0434\u0430\u043d\u0438\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432, \u043f\u043e\u0437\u0432\u043e\u043b\u044f\u044e\u0449\u0438\u0445 \u043a\u0430\u0441\u0442\u043e\u043c\u0438\u0437\u0438\u0440\u043e\u0432\u0430\u0442\u044c \u0435\u0433\u043e \u043f\u0440\u0430\u043a\u0442\u0438\u0447\u0435\u0441\u043a\u0438 \u043f\u043e\u0434 \u043b\u044e\u0431\u0443\u044e \u0437\u0430\u0434\u0430\u0447\u0443. \u0422\u043e \u0435\u0441\u0442\u044c, \u043f\u043b\u0430\u0433\u0438\u043d\u044b \u043c\u043e\u0436\u043d\u043e \u043f\u0438\u0441\u0430\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u0434\u043b\u044f \u043f\u0443\u0431\u043b\u0438\u043a\u0430\u0446\u0438\u0438 \u0438\u0445 \u0432 <code>npmjs.com<\/code> \u0440\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0438, \u043d\u043e \u0438 \u0434\u043b\u044f \u0430\u0432\u0442\u043e\u043c\u0430\u0442\u0438\u0437\u0430\u0446\u0438\u0438 \u0438\u0441\u043a\u043b\u044e\u0447\u0438\u0442\u0435\u043b\u044c\u043d\u043e \u0441\u0432\u043e\u0438\u0445 \u0437\u0430\u0434\u0430\u0447.<\/p>\n<p>\u0421\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u0441\u0440\u0430\u0432\u043d\u0438\u043c\u0430 \u0441\u043e \u0441\u043b\u043e\u0436\u043d\u043e\u0441\u0442\u044c\u044e \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0441\u0446\u0435\u043d\u0430\u0440\u0438\u044f \u0434\u043b\u044f Gulp \u0438\u043b\u0438 GitHub Actions. \u0414\u043b\u044f \u043f\u0440\u0438\u043c\u0435\u0440\u0430 \u043d\u0430\u043f\u0438\u0448\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0431\u0443\u0434\u0435\u0442 \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0442\u044c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0434\u0430 \u0432 \u0444\u0430\u0439\u043b index.html. \u0412 \u0437\u0430\u0432\u0438\u0441\u0438\u043c\u043e\u0441\u0442\u0438 \u043e\u0442 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u0432 \u0434\u0430\u043d\u043d\u044b\u0439 \u0444\u0430\u0439\u043b \u043d\u0435\u043e\u0431\u0445\u043e\u0434\u0438\u043c\u043e \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u043a\u043e\u0434 Google Analytics, \u043c\u0435\u0442\u0430\u0442\u044d\u0433\u0438 Open Graph \u0438 Twitter, \u043f\u043e\u0434\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435 Service worker-a, \u0432\u0438\u0434\u0436\u0435\u0442\u0430 \u0447\u0430\u0442\u0430 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u0438, \u0441\u043f\u043b\u044d\u0448 \u0441\u043a\u0440\u0438\u043d \u0438 \u043c\u043d\u043e\u0433\u043e\u0435 \u0434\u0440\u0443\u0433\u043e\u0435. \u0412 \u0440\u0435\u0437\u0443\u043b\u044c\u0442\u0430\u0442\u0435 index.html \u0441\u0442\u0430\u043d\u043e\u0432\u0438\u0442\u0441\u044f \u043e\u0447\u0435\u043d\u044c \u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0438 \u043e\u0440\u0438\u0435\u043d\u0442\u0438\u0440\u043e\u0432\u0430\u0442\u044c\u0441\u044f \u0432 \u043d\u0435\u043c \u0438 \u0431\u043b\u043e\u043a\u0430\u0445 \u043a\u043e\u0434\u0430 \u0434\u043e\u0432\u043e\u043b\u044c\u043d\u043e \u0441\u043b\u043e\u0436\u043d\u043e.<\/p>\n<p>\u041d\u0430\u0448 \u043f\u043b\u0430\u0433\u0438\u043d \u043f\u043e\u0437\u0432\u043e\u043b\u0438\u0442\u044c \u0434\u0435\u0440\u0436\u0430\u0442\u044c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u043a\u043e\u0434\u0430 \u0432 \u043e\u0442\u0434\u0435\u043b\u044c\u043d\u044b\u0445 \u0444\u0430\u0439\u043b\u0430\u0445, \u0430 \u043f\u0440\u0438 \u0441\u0431\u043e\u0440\u043a\u0435 \u0432\u0441\u0435 \u0431\u0443\u0434\u0435\u0442 \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c\u0441\u044f \u0432 <code>index.html<\/code>. \u041f\u0440\u0438\u0447\u0435\u043c \u044d\u0442\u043e \u0431\u0443\u0434\u0435\u0442 \u043f\u0440\u043e\u0438\u0441\u0445\u043e\u0434\u0438\u0442\u044c \u043d\u0435 \u0442\u043e\u043b\u044c\u043a\u043e \u043f\u0440\u0438 \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u0441\u0431\u043e\u0440\u043a\u0435 ( npm build ), \u043d\u043e \u0438 \u043f\u0440\u0438 \u0437\u0430\u043f\u0443\u0441\u043a\u0435 Vite dev \u0441\u0435\u0440\u0432\u0435\u0440\u0430 \u0441 \u043f\u043e\u0434\u0434\u0435\u0440\u0436\u043a\u043e\u0439 HRM (Hot Module Replacement).<\/p>\n<p>\u0420\u0435\u043f\u043e\u0437\u0438\u0442\u043e\u0440\u0438\u0439 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 &#8212; <a href=\"https:\/\/github.com\/altrusl\/vite-plugin-html-injection\" rel=\"noopener noreferrer nofollow\">https:\/\/github.com\/altrusl\/vite-plugin-html-injection<\/a><br \/>NPM &#8212; <a href=\"https:\/\/www.npmjs.com\/package\/vite-plugin-html-injection\" rel=\"noopener noreferrer nofollow\">https:\/\/www.npmjs.com\/package\/vite-plugin-html-injection<\/a><\/p>\n<h3>\u0421\u0438\u0441\u0442\u0435\u043c\u0430 \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432 Vite<\/h3>\n<p>\u041f\u043b\u0430\u0433\u0438\u043d\u044b Vite \u044f\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u0440\u0430\u0441\u0448\u0438\u0440\u0435\u043d\u0438\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432 \u0441\u0431\u043e\u0440\u0449\u0438\u043a\u0430 Rollup, \u043a\u043e\u0442\u043e\u0440\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u0442\u0441\u044f \u0434\u043b\u044f \u0441\u0431\u043e\u0440\u043a\u0438 Vite \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043f\u043e\u0434 \u043a\u0430\u043f\u043e\u0442\u043e\u043c. \u0417\u0430 \u043d\u0435\u0431\u043e\u043b\u044c\u0448\u0438\u043c \u0438\u0441\u043a\u043b\u044e\u0447\u0435\u043d\u0438\u0435\u043c \u043f\u043b\u0430\u0433\u0438\u043d\u044b Rollup \u0440\u0430\u0431\u043e\u0442\u0430\u044e\u0442 \u0432 Vite, \u0432 \u0442\u043e \u0436\u0435 \u0432\u0440\u0435\u043c\u044f \u043f\u043e\u0441\u043b\u0435\u0434\u043d\u0438\u0439 \u0434\u043e\u0431\u0430\u0432\u043b\u044f\u0435\u0442 \u043d\u0435\u0441\u043a\u043e\u043b\u044c\u043a\u043e \u0445\u0443\u043a\u043e\u0432 \u0432 API \u0434\u043b\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u043e\u0432, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043c\u044b \u0438 \u0431\u0443\u0434\u0435\u0442 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u043e\u0432\u0430\u0442\u044c.<\/p>\n<p>\u041d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043f\u043e \u0431\u043e\u043b\u044c\u0448\u043e\u043c\u0443 \u0441\u0447\u0435\u0442\u0443 \u044f\u0432\u043b\u044f\u0435\u0442\u0441\u044f \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u0435\u043c \u043a\u043e\u0434\u0430 \u0434\u043b\u044f \u0434\u0430\u043d\u043d\u044b\u0445 \u0445\u0443\u043a\u043e\u0432. \u041c\u044b \u0437\u0430\u0434\u0435\u0439\u0441\u0442\u0432\u0443\u0435\u043c \u0434\u0432\u0430 \u0438\u0437 \u043d\u0438\u0445. <\/p>\n<h3>vite-plugin-html-injection<\/h3>\n<p>\u041f\u0435\u0440\u0432\u044b\u0439 \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c\u044b\u0439 \u0445\u0443\u043a &#8212; <code>configResolved.<\/code> \u041e\u043d \u043d\u0443\u0436\u0435\u043d \u0434\u043b\u044f \u0442\u043e\u0433\u043e, \u0447\u0442\u043e\u0431\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u0442\u044c \u043a\u043e\u043d\u0444\u0438\u0433 Vite, \u0438\u0437 \u043a\u043e\u0442\u043e\u0440\u043e\u0433\u043e \u043c\u044b \u043f\u043e\u0437\u0436\u0435 \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0430\u0431\u0441\u043e\u043b\u044e\u0442\u043d\u044b\u0439 \u043f\u0443\u0442\u044c \u043a \u0434\u0438\u0440\u0435\u043a\u0442\u043e\u0440\u0438\u0438 \u043f\u0440\u043e\u0435\u043a\u0442\u0430 &#8212; <code>config.root<\/code><\/p>\n<p>\u0412\u0442\u043e\u0440\u043e\u0439 \u0445\u0443\u043a &#8212; <code>transformIndexHtml<\/code>.  \u0412 \u043d\u0435\u043c \u043d\u0435\u043f\u043e\u0441\u0440\u0435\u0434\u0441\u0442\u0432\u0435\u043d\u043d\u043e \u043d\u0443\u0436\u043d\u043e \u043f\u0440\u043e\u0438\u0437\u0432\u0435\u0441\u0442\u0438 \u0438\u0437\u043c\u0435\u043d\u0435\u043d\u0438\u0435 \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u043e\u0433\u043e <code>index.html<\/code>. \u0410\u0440\u0433\u0443\u043c\u0435\u043d\u0442\u043e\u043c \u043c\u044b \u043f\u043e\u043b\u0443\u0447\u0438\u043c \u0441\u0442\u0440\u043e\u043a\u0443 \u0441 \u043e\u0440\u0438\u0433\u0438\u043d\u0430\u043b\u044c\u043d\u044b\u043c \u0441\u043e\u0434\u0435\u0440\u0436\u0438\u043c\u044b\u043c <code>index.html<\/code>, \u0432\u0435\u0440\u043d\u0443\u0442\u044c \u043d\u0430\u0434\u043e \u043c\u043e\u0434\u0438\u0444\u0438\u0446\u0438\u0440\u043e\u0432\u0430\u043d\u043d\u044b\u0439 \u043a\u043e\u043d\u0442\u0435\u043d\u0442.<\/p>\n<pre><code class=\"typescript\">import { Plugin, ResolvedConfig } from \"vite\"; import path from \"path\"; import fs from \"fs\"; import { IHtmlInjectionConfig, IHtmlInjectionConfigInjection } from \".\/types\";  export function htmlInjectionPlugin(   htmlInjectionConfig: IHtmlInjectionConfig ): Plugin {   let config: undefined | ResolvedConfig;    return {     name: \"html-injection\",      configResolved(resolvedConfig) {       config = resolvedConfig;     },      transformIndexHtml(html: string) {       let out = html;       for (let i = 0; i &lt; htmlInjectionConfig.injections.length; i++) {         const injection: IHtmlInjectionConfigInjection =           htmlInjectionConfig.injections[i];          let root = (config as ResolvedConfig).root;         const filePath = path.resolve(root, injection.path);         let data = fs.readFileSync(filePath, \"utf8\");         if (injection.type === \"js\") {           data = `&lt;script>\\n${data}\\n&lt;\/script>\\n`;         } else if (injection.type === \"css\") {           data = `&lt;style>\\n${data}\\n&lt;\/style>\\n`;         }         switch (injection.injectTo) {           case \"head\":             out = out.replace(\"&lt;\/head>\", `${data}\\n&lt;\/head>`);             break;           case \"head-prepend\":             out = out.replace(\/&lt;head(.*)>\/i, `$&amp;\\n${data}`);             break;           case \"body\":             out = out.replace(\"&lt;\/body>\", `${data}\\n&lt;\/body>`);             break;           case \"body-prepend\":             out = out.replace(\/&lt;body(.*)>\/i, `$&amp;\\n${data}`);             break;            default:             break;         }       }        return out;     },   }; } <\/code><\/pre>\n<p>\u0412 \u043c\u0430\u0441\u0441\u0438\u0432\u0435 <code>htmlInjectionConfig.injections<\/code> \u0441\u043e\u0434\u0435\u0440\u0436\u0430\u0442\u0441\u044f \u043e\u043f\u0438\u0441\u0430\u043d\u0438\u044f \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u0434\u0430 &#8212; \u043a\u043e\u043d\u0444\u0438\u0433\u0443\u0440\u0430\u0446\u0438\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u0430, \u043f\u0435\u0440\u0435\u0434\u0430\u0432\u0430\u0435\u043c\u0430\u044f \u0435\u043c\u0443 \u043a\u0430\u043a \u0430\u0440\u0433\u0443\u043c\u0435\u043d\u0442 \u0432 <code>vite.config.js<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430, \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u044e\u0449\u0435\u0433\u043e \u043d\u0430\u0448 \u043f\u043b\u0430\u0433\u0438\u043d.<\/p>\n<pre><code class=\"javascript\">\/\/ vite.config.js \/\/ example for Vue project  import { defineConfig } from \"vite\"; import vue from \"@vitejs\/plugin-vue\"; import { htmlInjectionPlugin } from \"vite-plugin-html-injection\";  export default defineConfig({   plugins: [     vue(),     htmlInjectionPlugin({       \/\/ Example configuration. Can be stored in a separate json file.       injections: [         {           name: \"Open Graph\",           path: \".\/src\/injections\/open-graph.html\",           type: \"raw\",           injectTo: \"head\",         },         {           name: \"Splash screen\",           path: \".\/src\/injections\/splash-screen.html\",           type: \"raw\",           injectTo: \"body-prepend\",         },         {           name: \"Service worker\",           path: \".\/src\/injections\/sw.js\",           type: \"js\",           injectTo: \"head\",         },       ],     }),   ] }); <\/code><\/pre>\n<p>\u0412 \u0441\u043b\u0443\u0447\u0430\u0435 \u043d\u0430\u043f\u0438\u0441\u0430\u043d\u0438\u044f &#171;\u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e\u0433\u043e&#187; \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u0434\u043b\u044f \u0441\u0435\u0431\u044f, \u0432\u043c\u0435\u0441\u0442\u043e \u0434\u043e\u0431\u0430\u0432\u043b\u0435\u043d\u0438\u044f \u0435\u0433\u043e \u0432 <code>devDependencies<\/code> \u0438 \u0438\u043c\u043f\u043e\u0440\u0442\u0430 \u0438\u0437 <code>node_modules<\/code>:<\/p>\n<pre><code class=\"bash\">import { htmlInjectionPlugin } from \"vite-plugin-html-injection\";<\/code><\/pre>\n<p>\u043c\u043e\u0436\u043d\u043e \u0438\u043c\u043f\u043e\u0440\u0442\u0438\u0442\u044c \u0435\u0433\u043e \u043b\u043e\u043a\u0430\u043b\u044c\u043d\u043e:<\/p>\n<pre><code class=\"bash\">import { htmlInjectionPlugin } from \".\/src\/plugins\/vite-plugin-html-injection\";<\/code><\/pre>\n<h3>\u0422\u0438\u043f\u044b \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u0435\u043c\u044b\u0445 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u0434\u0430 <\/h3>\n<p>\u0421\u0443\u0449\u0435\u0441\u0442\u0432\u0443\u0435\u0442 \u0442\u0440\u0438 \u0442\u0438\u043f\u0430 \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u043e\u0432 \u043a\u043e\u0434\u0430, \u0441 \u043a\u043e\u0442\u043e\u0440\u044b\u043c\u0438 \u0440\u0430\u0431\u043e\u0442\u0430\u0435\u0442 \u043f\u043b\u0430\u0433\u0438\u043d \u2014 <code>raw<\/code>, <code>js<\/code> \u0438 <code>css<\/code>. <\/p>\n<p><code>Raw<\/code> \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442\u044b \u0432\u0441\u0442\u0430\u0432\u043b\u044f\u044e\u0442\u0441\u044f \u043a\u0430\u043a \u0435\u0441\u0442\u044c, <code>js<\/code> \u0438 <code>css<\/code> \u043e\u0431\u043e\u0440\u0430\u0447\u0438\u0432\u0430\u044e\u0442\u0441\u044f \u0432 \u0442\u0435\u0433\u0438 <code>&lt;script><\/code> \u0438 <code>&lt;style><\/code> \u0441\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0435\u043d\u043d\u043e.<\/p>\n<p>\u0422\u0430\u043a\u0436\u0435 \u0435\u0441\u0442\u044c \u0447\u0435\u0442\u044b\u0440\u0435 \u043c\u0435\u0441\u0442\u0430, \u043a\u0443\u0434\u0430 \u043c\u043e\u0436\u043d\u043e \u0432\u0441\u0442\u0430\u0432\u0438\u0442\u044c \u0444\u0440\u0430\u0433\u043c\u0435\u043d\u0442 \u043a\u043e\u0434\u0430: \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 \u0442\u0435\u0433\u0430 <code>head<\/code> \u0438 \u043d\u0430\u0447\u0430\u043b\u043e \u0438 \u043a\u043e\u043d\u0435\u0446 <code>body<\/code>. \u0421\u043e\u043e\u0442\u0432\u0435\u0442\u0441\u0442\u0432\u0443\u044e\u0449\u0438\u0435 \u0437\u043d\u0430\u0447\u0435\u043d\u0438\u044f \u0441\u0432\u043e\u0439\u0441\u0442\u0432\u0430 <code>injectTo<\/code>: <code>head-prepend<\/code>, <code>head<\/code>, <code>body-prepend<\/code> \u0438 <code>body<\/code>.<\/p>\n<p>\u0424\u0430\u0439\u043b <code>.\/src\/injections\/ga.html<\/code> \u043c\u043e\u0436\u0435\u0442 \u0432\u044b\u0433\u043b\u044f\u0434\u0435\u0442\u044c \u043a\u0430\u043a-\u0442\u043e \u0442\u0430\u043a:<\/p>\n<pre><code class=\"javascript\">&lt;!-- Google tag (gtag.js) --> &lt;script async src=\"https:\/\/www.googletagmanager.com\/gtag\/js?id=G-8W4X32XXXX\" \/> &lt;script>   window.dataLayer = window.dataLayer || [];   function gtag() {     dataLayer.push(arguments);   }   gtag(\"js\", new Date());    gtag(\"config\", \"G-8W4X32XXXX\"); &lt;\/script><\/code><\/pre>\n<h3>\u0421\u0431\u043e\u0440\u043a\u0430 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043a\u0430\u043a \u043f\u0430\u043a\u0435\u0442\u0430<\/h3>\n<p>\u0412 \u043a\u043e\u0434\u0435 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043c\u044b \u0438\u0441\u043f\u043e\u043b\u044c\u0437\u0443\u0435\u043c \u0431\u0438\u0431\u043b\u0438\u043e\u0442\u0435\u043a\u0438 <code>fs<\/code> \u0438 <code>path<\/code>, \u043a\u043e\u0442\u043e\u0440\u044b\u0435 \u043f\u043e\u043c\u0435\u0449\u0430\u0442\u044c \u0441 \u0441\u0431\u043e\u0440\u043a\u0443 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043d\u0435 \u043d\u0443\u0436\u043d\u043e, \u043f\u043e\u0442\u043e\u043c\u0443 \u0447\u0442\u043e \u043e\u043d\u0438 \u0431\u0443\u0434\u0443\u0442 \u043f\u0440\u0435\u0434\u043e\u0441\u0442\u0430\u0432\u043b\u0435\u043d\u044b \u0441\u0430\u043c\u044b\u043c Vite \u0432\u043e \u0432\u0440\u0435\u043c\u044f \u0432\u044b\u043f\u043e\u043b\u043d\u0435\u043d\u0438\u044f \u043f\u043b\u0430\u0433\u0438\u043d\u0430. \u0412 <code>vite.config.js<\/code> \u043f\u0440\u043e\u0435\u043a\u0442\u0430 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u044d\u0442\u043e \u043d\u0443\u0436\u043d\u043e \u0443\u043a\u0430\u0437\u0430\u0442\u044c \u044f\u0432\u043d\u043e.<\/p>\n<pre><code class=\"javascript\">\/\/ vite.config.js  import { defineConfig } from \"vite\"; import { resolve } from \"path\";  export default defineConfig({   plugins: [],   build: {     lib: {       entry: resolve(__dirname, \".\/index.ts\"),       name: \"HtmlInjection\",       fileName: \"index\",       formats: [\"es\", \"cjs\"],     },     rollupOptions: {       external: [\"fs\", \"path\"],     },   }, }); <\/code><\/pre>\n<p>\u0412 \u0438\u0442\u043e\u0433\u0435 \u0440\u0430\u0437\u043c\u0435\u0440 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043f\u043e\u043b\u0443\u0447\u0430\u0435\u0442\u0441\u044f \u043c\u0435\u043d\u044c\u0448\u0435 1\u041a\u0411.<\/p>\n<p>\u041d\u0443 \u0438 <code>package.json<\/code> \u043f\u043b\u0430\u0433\u0438\u043d\u0430:<\/p>\n<pre><code>{   \"name\": \"vite-plugin-html-injection\",   \"version\": \"1.1.9\",   \"description\": \"Vite plugin for injecting html, js, css code snippets into index.html\",   \"license\": \"MIT\",   \"homepage\": \"https:\/\/github.com\/altrusl\/vite-plugin-html-injection\",   \"type\": \"module\",   \"main\": \".\/dist\/index.cjs\",   \"module\": \".\/dist\/index.js\",   \"types\": \".\/dist\/index.d.ts\",   \"exports\": {     \".\": {       \"import\": \".\/dist\/index.js\",       \"require\": \".\/dist\/index.cjs\",       \"types\": \".\/dist\/index.d.ts\"     }   },   \"files\": [     \"dist\"   ],   \"scripts\": {     \"build\": \"vite build &amp;&amp; copy types.d.ts dist\\\\index.d.ts\"   },   \"devDependencies\": {     \"@antfu\/eslint-config\": \"^0.39.8\",     \"@types\/node\": \"^20.4.5\",     \"eslint\": \"^8.46.0\",     \"typescript\": \"^5.1.6\",     \"vite\": \"^4.4.7\"   },   \"peerDependencies\": {     \"vite\": \">= 2.0.0\"   } }<\/code><\/pre>\n<p>\u041f\u043e\u0441\u043b\u0435 \u0441\u0431\u043e\u0440\u043a\u0438 \u043f\u043b\u0430\u0433\u0438\u043d\u0430 \u043f\u0443\u0431\u043b\u0438\u043a\u0443\u0435\u043c \u0435\u0433\u043e \u043d\u0430 npmjs.com:<\/p>\n<pre><code class=\"bash\">npm publish --access public<\/code><\/pre>\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\/articles\/753788\/\"> https:\/\/habr.com\/ru\/articles\/753788\/<\/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-351814","post","type-post","status-publish","format-standard","hentry"],"_links":{"self":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/351814","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=351814"}],"version-history":[{"count":0,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=\/wp\/v2\/posts\/351814\/revisions"}],"wp:attachment":[{"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=351814"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=351814"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/savepearlharbor.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=351814"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}