{"id":21471135,"url":"https://github.com/im-open/inline-html-template-plugin","last_synced_at":"2025-03-17T06:46:06.704Z","repository":{"id":38175043,"uuid":"251676766","full_name":"im-open/inline-html-template-plugin","owner":"im-open","description":"A Webpack plugin for inlining fully rendered HTML as a template string.","archived":false,"fork":false,"pushed_at":"2023-05-10T03:29:49.000Z","size":1287,"stargazers_count":0,"open_issues_count":8,"forks_count":0,"subscribers_count":15,"default_branch":"master","last_synced_at":"2025-01-23T16:14:52.384Z","etag":null,"topics":["customization-team","exclude-from-actions-list"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/im-open.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2020-03-31T17:17:18.000Z","updated_at":"2024-08-21T14:18:41.000Z","dependencies_parsed_at":"2024-01-02T09:10:30.817Z","dependency_job_id":"b72f1e19-496c-4992-a413-f2c610c48dda","html_url":"https://github.com/im-open/inline-html-template-plugin","commit_stats":null,"previous_names":["wtw-im/inline-html-template-plugin"],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/im-open%2Finline-html-template-plugin","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/im-open%2Finline-html-template-plugin/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/im-open%2Finline-html-template-plugin/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/im-open%2Finline-html-template-plugin/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/im-open","download_url":"https://codeload.github.com/im-open/inline-html-template-plugin/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243988959,"owners_count":20379649,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["customization-team","exclude-from-actions-list"],"created_at":"2024-11-23T09:31:46.316Z","updated_at":"2025-03-17T06:46:06.672Z","avatar_url":"https://github.com/im-open.png","language":"TypeScript","readme":"# inline-html-template-plugin\n\n[![Build Status](https://travis-ci.com/WTW-IM/inline-html-template-plugin.svg?branch=master)](https://travis-ci.com/github/WTW-IM/inline-html-template-plugin)\n[![npm version](https://badge.fury.io/js/inline-html-template-plugin.svg)](https://badge.fury.io/js/inline-html-template-plugin)\n\n## Installation\n\nTo install, simply run:\n\n```bash\nnpm install --save inline-html-template-plugin\n```\n\n## The Problem\n\nSometimes, you need to inject javascript into an HTML file. Webpack handles this beautifully using HtmlWebpackPlugin. HtmlWebpackPlugin can also do a number of other dynamic things to generate an HTML file that's complete and ready for production.\n\nIn some cases, particularly with Web Components, you might need to inject HTML into your javascript. In the case of Web Components, your CSS will need to be inlined, and you'll want the CSS to be dynamically generated based on all the CSS imported throughout your app.\n\n## The Solution\n\nThe InlineHTMLTemplatePlugin allows you to inline your finalized HTML as a template into any Javascript file. In Web Components, this means that you can use the completed HTML as a template, and your entire Web Component will be packaged into a single javascript file.\n\n## Usage\n\nA case for Web Components, as described above, might look like the following:\n\n### webpack.config.js\n\n```javascript\nconst HtmlWebpackPlugin = require('html-webpack-plugin');\nconst MiniCssExtractPlugin = require('mini-css-extract-plugin');\nconst HTMLInlineCSSWebpackPlugin = require('html-inline-css-webpack-plugin')\n  .default;\nconst InlineHTMLTemplatePlugin = require('inline-html-template-plugin').default;\n\nmodule.exports = {\n  mode: 'development',\n  entry: {\n    component: __dirname + '/src/component.js'\n  },\n  module: {\n    rules: [\n      {\n        test: /\\.js$/,\n        exclude: /(node_modules)/,\n        use: {\n          loader: 'babel-loader',\n          options: {\n            presets: ['@babel/preset-env'],\n            plugins: [\n              '@babel/plugin-proposal-object-rest-spread',\n              '@babel/plugin-transform-runtime',\n              '@babel/plugin-proposal-class-properties'\n            ]\n          }\n        }\n      },\n      {\n        test: /\\.css$/i,\n        use: [\n          {\n            loader: MiniCssExtractPlugin.loader,\n            options: {\n              sourceMap: false\n            }\n          },\n          {\n            loader: 'css-loader',\n            options: {\n              modules: true\n            }\n          }\n        ]\n      }\n    ]\n  },\n  plugins: [\n    new HtmlWebpackPlugin({\n      template: 'src/component-template.html',\n      filename: 'component-template.html',\n      inject: false\n    }),\n    new MiniCssExtractPlugin(),\n    new HTMLInlineCSSWebpackPlugin({\n      replace: {\n        target: '\u003c!-- inline css --\u003e',\n        removeTarget: true\n      }\n    }),\n    new InlineHTMLTemplatePlugin()\n  ]\n};\n```\n\nThe key portion is this:\n\n```javascript\nplugins: [\n  // load the template; give it a filename\n  new HtmlWebpackPlugin({\n    template: 'src/component-template.html',\n    filename: 'component-template.html',\n    // to avoid a circular reference, do not inject javascript\n    inject: false\n  }),\n  // extract the css into a file\n  new MiniCssExtractPlugin(),\n  // inline that CSS into your HTML\n  new HTMLInlineCSSWebpackPlugin({\n    replace: {\n      target: '\u003c!-- inline css --\u003e',\n      removeTarget: true\n    }\n  }),\n  // inject the finalized HTML as a string into your javascript\n  new InlineHTMLTemplatePlugin()\n];\n```\n\n### component-template.html\n\nYour html might look like this:\n\n```html\n\u003cdiv id=\"app-container\"\u003e\n  \u003c!-- inline css --\u003e\n  \u003cdiv id=\"mount\"\u003e\u003c/div\u003e\n\u003c/div\u003e\n```\n\n### component.js\n\nYour component javascript might look something like this, where `myApp` is a javascript solution for initializing an application on a DOM node.\n\n```javascript\nimport myApp from './myApp';\nconst loadedTemplate = '/* InlineHTML: component-template.html */';\n\nclass WebpackComponent extends HTMLElement {\n  connectedCallback() {\n    this.attachShadow({ open: true });\n    this.shadowRoot.innerHTML = loadedTemplate;\n    myApp.initialize(this.shadowRoot.querySelector('#mount'));\n  }\n}\ncustomElements.define('webpack-component', WebpackComponent);\n```\n\nIn this javascript, the InlineHtmlTemplatePlugin looks for `\"/* InlineHTML: component-template.html */\"`, parses the filename from this string, and replaces the string with your finalized HTML Template.\n\n## Contributing\n\nThis package uses `semantic-release`. Changes will be compiled into a changelog and the package versioned, tagged and published automatically.\nPlease ensure your commit messages adhere to the following structure:\n\n```\n\u003ctype\u003e: \u003csubject\u003e\n\u003cBLANK LINE\u003e\n\u003cbody\u003e\n```\n\nOnly the header is mandatory. The supported types are based off of the [ESLint Convention](https://github.com/conventional-changelog/conventional-changelog/tree/35e279d40603b0969c6d622514f5c0984c5bf309/packages/conventional-changelog-eslint).\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fim-open%2Finline-html-template-plugin","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fim-open%2Finline-html-template-plugin","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fim-open%2Finline-html-template-plugin/lists"}