Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,20 @@ const miniCssExtractPlugin = new MiniCssExtractPlugin({
})
```

#### Add custom attributes

If you want to add some custom attributes to dynamically loaded css chunks, you can use `attrs` option

```javascript
const miniCssExtractPlugin = new MiniCssExtractPlugin({
attrs: {
'data-id': 'unique-id'
}
})
```

Note: It's only applied to dynamically loaded css chunks, if you want to modify link attributes inside html file, please using [html-webpack-plugin](https://github.com/jantimon/html-webpack-plugin)

#### Long Term Caching

For long term caching use `filename: "[contenthash].css"`. Optionally add `[name]`.
Expand Down
7 changes: 7 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -363,6 +363,8 @@ class MiniCssExtractPlugin {
}
);

const { attrs = {} } = this.options;

return Template.asString([
source,
'',
Expand Down Expand Up @@ -394,6 +396,11 @@ class MiniCssExtractPlugin {
'var linkTag = document.createElement("link");',
'linkTag.rel = "stylesheet";',
'linkTag.type = "text/css";',
...Object.keys(attrs).map((k) => {
const key = JSON.stringify(k);
const value = JSON.stringify(attrs[k] || '');
return `linkTag.setAttribute(${key}, ${value});`;
}),
'linkTag.onload = resolve;',
'linkTag.onerror = function(event) {',
Template.indent([
Expand Down