Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/panghu-huang/html-attribute-webpack-plugin

Add custom attributes to HTML tags
https://github.com/panghu-huang/html-attribute-webpack-plugin

Last synced: about 1 month ago
JSON representation

Add custom attributes to HTML tags

Awesome Lists containing this project

README

        

# HTMLAttributeWebpackPlugin

A Webpack plugin for add custom attributes to HTML tags

## Example
[webpack.config.ts](./example/webpack.config.ts)

## Usage

* Use CommonJs
```javascript
const HTMLWebpackPlugin = require('html-webpack-plugin')
const { HTMLAttributeWebpackPlugin } = require('html-attribute-webpack-plugin')

const config = {
// ...
plugins: [
new HTMLWebpackPlugin({
template: 'path/to/your/template'
}),
new HTMLAttributeWebpackPlugin(
HTMLWebpackPlugin,
{
// getAttributes is a function that return updated attributes
getAttributes: tag => {
return {
...tag.attributes,
name: tag.tagName
}
},
// You can provide an object or a function that return an object
script: {
crossorigin: 'anonymous'
},
// Same as script
style: tag => {
return {
...tag.attributes
}
}
}
)
]
}
```

* Use TypeScript
```typescript
import webpack from 'webpack';
import HTMLWebpackPlugin from 'html-webpack-plugin';
import { HTMLAttributeWebpackPlugin, HtmlTagObject } from 'html-attribute-webpack-plugin';

const config: webpack.Configuration = {
// ...
plugins: [
new HTMLWebpackPlugin({
template: 'path/to/your/template'
}),
new HTMLAttributeWebpackPlugin(
HTMLWebpackPlugin,
{
getAttributes: (tag: HtmlTagObject) => {
return {
...tag.attributes,
name: tag.tagName
}
},
script: {
crossorigin: 'anonymous'
},
style: (tag: HtmlTagObject) => {
return {
...tag.attributes
}
}
}
)
]
}
```