Skip to content
This repository was archived by the owner on May 31, 2021. It is now read-only.

Commit 5709b74

Browse files
committed
feat: add attrs option
1 parent 159ce3b commit 5709b74

File tree

3 files changed

+87
-0
lines changed

3 files changed

+87
-0
lines changed

README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -353,6 +353,20 @@ const miniCssExtractPlugin = new MiniCssExtractPlugin({
353353
});
354354
```
355355

356+
#### Add custom attributes
357+
358+
If you want to add some custom attributes to dynamically loaded css chunks, you can use `attrs` option
359+
360+
```javascript
361+
const miniCssExtractPlugin = new MiniCssExtractPlugin({
362+
attrs: {
363+
'data-id': 'unique-id',
364+
},
365+
});
366+
```
367+
368+
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)
369+
356370
#### Long Term Caching
357371

358372
For long term caching use `filename: "[contenthash].css"`. Optionally add `[name]`.

src/index.js

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -364,6 +364,8 @@ class MiniCssExtractPlugin {
364364
}
365365
);
366366

367+
const { attrs = {} } = this.options;
368+
367369
return Template.asString([
368370
source,
369371
'',
@@ -395,6 +397,11 @@ class MiniCssExtractPlugin {
395397
'var linkTag = document.createElement("link");',
396398
'linkTag.rel = "stylesheet";',
397399
'linkTag.type = "text/css";',
400+
...Object.keys(attrs).map((k) => {
401+
const key = JSON.stringify(k);
402+
const value = JSON.stringify(attrs[k] || '');
403+
return `linkTag.setAttribute(${key}, ${value});`;
404+
}),
398405
'linkTag.onload = resolve;',
399406
'linkTag.onerror = function(event) {',
400407
Template.indent([

test/attrs.test.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
import MiniCssExtractPlugin from '../src';
2+
3+
describe('Custom Attrs', () => {
4+
it('adds attrs', () => {
5+
const plugin = new MiniCssExtractPlugin({
6+
attrs: { 'my-attribute': 'my-value' },
7+
});
8+
9+
const compiler = {
10+
hooks: {
11+
thisCompilation: {
12+
tap: (_pluginName, _callback) => {
13+
const compilation = {
14+
hooks: {
15+
normalModuleLoader: {
16+
tap: () => {},
17+
},
18+
contentHash: {
19+
tap: () => {},
20+
},
21+
},
22+
dependencyFactories: { set: () => {} },
23+
dependencyTemplates: { set: () => {} },
24+
mainTemplate: {
25+
getAssetPath: () => {},
26+
renderCurrentHashCode: () => '',
27+
outputOptions: { crossOriginLoading: false },
28+
hooks: {
29+
renderManifest: { tap: () => {} },
30+
hashForChunk: { tap: () => {} },
31+
localVars: {
32+
tap: () => {},
33+
},
34+
requireEnsure: {
35+
tap: (pluginName, callback) => {
36+
const source = '';
37+
const chunk = {
38+
getAllAsyncChunks: () => [
39+
{ modulesIterable: [{ type: 'css/mini-extract' }] },
40+
],
41+
getChunkMaps: () => ({
42+
hash: '',
43+
contentHash: { 'css/mini-extract': {} },
44+
}),
45+
};
46+
const hash = '';
47+
const template = callback(source, chunk, hash);
48+
expect(
49+
template.indexOf(
50+
'linkTag.setAttribute("my-attribute", "my-value");'
51+
) > -1
52+
).toBe(true);
53+
},
54+
},
55+
},
56+
},
57+
chunkTemplate: { hooks: { renderManifest: { tap: () => {} } } },
58+
};
59+
_callback(compilation);
60+
},
61+
},
62+
},
63+
};
64+
plugin.apply(compiler);
65+
});
66+
});

0 commit comments

Comments
 (0)