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

Commit 799c757

Browse files
committed
Support for the media option
1 parent e4f4f68 commit 799c757

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

README.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,22 @@ For the async chunks mentioned earlier, the plugin would update your HTML to the
221221
<link rel="prefetch" href="chunk.d15e7fdfc91b34bb78c4.js">
222222
```
223223

224+
## Including media
225+
226+
`<link>` elements have the ability to accept media attributes. These can accept media types or full-blown media queries, allowing you to do responsive preloading.
227+
228+
You can pass the value for the media attribute in the `media` option:
229+
230+
```javascript
231+
plugins: [
232+
new HtmlWebpackPlugin(),
233+
new PreloadWebpackPlugin({
234+
rel: 'preload',
235+
media: '(min-width: 600px)'
236+
})
237+
]
238+
```
239+
224240
## Support
225241

226242
If you've found an error or run into problems, please [file an issue](https://github.com/googlechrome/preload-webpack-plugin/issues).

src/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ class PreloadPlugin {
7070
rel: options.rel,
7171
};
7272

73+
// See https://github.com/GoogleChromeLabs/preload-webpack-plugin/issues/69
74+
if (options.media) {
75+
attributes.media = options.media;
76+
}
77+
7378
// If we're preloading this resource (as opposed to prefetching),
7479
// then we need to set the 'as' attribute correctly.
7580
if (options.rel === 'preload') {

test/spec.js

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,45 @@ module.exports = ({descriptionPrefix, webpack, HtmlWebpackPlugin}) => {
329329
});
330330
compiler.outputFileSystem = new MemoryFileSystem();
331331
});
332+
333+
it('should use the value for the media attribute passed in the configuration', (done) => {
334+
const compiler = webpack({
335+
entry: path.join(__dirname, 'fixtures', 'file.js'),
336+
output: {
337+
path: OUTPUT_DIR,
338+
filename: 'bundle.js',
339+
chunkFilename: 'chunk.[chunkhash].css',
340+
publicPath: '/',
341+
},
342+
plugins: [
343+
new HtmlWebpackPlugin(),
344+
new PreloadPlugin({
345+
rel: 'preload',
346+
media: '(min-width: 600px)',
347+
include: 'allChunks'
348+
}),
349+
]
350+
}, function(err, result) {
351+
expect(err).toBeFalsy(err);
352+
expect(result.compilation.errors.length).toBe(0,
353+
result.compilation.errors.join('\n=========\n'));
354+
355+
const html = result.compilation.assets['index.html'].source();
356+
const dom = new JSDOM(html);
357+
358+
const links = dom.window.document.head.querySelectorAll('link');
359+
expect(links.length).toBe(2);
360+
expect(links[0].getAttribute('rel')).toBe('preload');
361+
expect(links[0].getAttribute('media')).toBe('(min-width: 600px)');
362+
expect(links[0].getAttribute('href')).toBe('/bundle.js');
363+
expect(links[1].getAttribute('rel')).toBe('preload');
364+
expect(links[1].getAttribute('media')).toBe('(min-width: 600px)');
365+
expect(links[1].getAttribute('href')).toMatch(new RegExp('^/chunk\\.'));
366+
367+
done();
368+
});
369+
compiler.outputFileSystem = new MemoryFileSystem();
370+
});
332371
});
333372

334373
describe(`${descriptionPrefix} When passed normal chunks, it`, function() {

0 commit comments

Comments
 (0)