Skip to content

Update this fork of the plugin #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 63 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
63 commits
Select commit Hold shift + click to select a range
ef4f526
Merge remote-tracking branch 'refs/remotes/GoogleChrome/master'
laysent Sep 3, 2017
784b01d
add feature to include all assets
laysent Sep 4, 2017
487f77a
add `fileWhitelist` to include specific files
laysent Sep 4, 2017
cdb459e
Merge remote-tracking branch 'refs/remotes/GoogleChrome/master'
laysent Sep 6, 2017
e19777f
Merge pull request #1 from GoogleChromeLabs/master
toxic-johann Dec 21, 2017
11da208
Merge branch 'master' into master
laysent Jan 13, 2018
748dbfa
update README to point out potential issue
laysent Jan 29, 2018
4ff53e0
Merge branch 'master' of https://github.com/GoogleChromeLabs/preload-…
toxic-johann Feb 7, 2018
cf895ed
[update] add excludeHtmlNames to solve problem mentioned in https://g…
toxic-johann Feb 7, 2018
e29cdd4
Merge pull request #54 from toxic-johann/master
jeffposnick Feb 11, 2018
374ad6f
Fixed all-assets being treated as a real chunk in doesChunkBelongToHT…
alechill Feb 14, 2018
de90ae6
Merge pull request #1 from alechill/master
laysent Feb 18, 2018
99b7f3c
merge from upstream
laysent Feb 18, 2018
aca5288
rename option names
laysent Feb 28, 2018
40a1220
Merge branch 'master' into master
jeffposnick Feb 28, 2018
0f80892
Merge pull request #36 from laysent/master
jeffposnick Feb 28, 2018
f604879
2.3.0
jeffposnick Mar 1, 2018
cee07b9
[update] use chunkGroup in webpack v4 to pass PreloadPlugin preloads …
toxic-johann Mar 3, 2018
3a546f9
[update] pass all three PreloadPlugin preloads or prefetches async ch…
toxic-johann Mar 3, 2018
46dd607
[update] pass some more tests and change one test as require logic ch…
toxic-johann Mar 3, 2018
0e17c6e
[update] pass PreloadPlugin filters chunks
toxic-johann Mar 3, 2018
7eb8803
[update] pass filtering unwanted files
toxic-johann Mar 3, 2018
2605ce4
[update] implement the multiple html function
toxic-johann Mar 4, 2018
fe3e64d
[update] update some test package
toxic-johann Mar 4, 2018
947c2bb
[update] use latest extract-test-webpack-plugin to pass all 17 tests
toxic-johann Mar 4, 2018
2cff0b4
[update] remove jasmin
toxic-johann Mar 6, 2018
81ebc2a
[update] add webpack-v3 logic back
toxic-johann Mar 7, 2018
62b4271
[update] compatiable webpack v3
toxic-johann Mar 8, 2018
798fc67
Merge pull request #57 from toxic-johann/master
jeffposnick Mar 9, 2018
34fd443
Use node 6 as a minimum.
jeffposnick Jun 6, 2018
76fb29e
Use url-parse for node 6 support
jeffposnick Jun 6, 2018
89f6495
Add a runtime check for the HtmlWebpackPlugin
jeffposnick Jun 6, 2018
89438eb
README updates.
jeffposnick Jun 6, 2018
b6c7553
More README link tweaks.
jeffposnick Jun 6, 2018
5daaa8e
3.0.0-beta.1
jeffposnick Jun 6, 2018
23834d7
Some additional tweaks.
jeffposnick Jun 7, 2018
05cdffc
TravisCI config
jeffposnick Jun 7, 2018
6317aab
Better install step
jeffposnick Jun 7, 2018
3c02dde
Merge branch 'master' into v3
jeffposnick Jun 7, 2018
b1f8ec6
More tweaks.
jeffposnick Jun 7, 2018
d80459a
Updating dependencies.
jeffposnick Aug 6, 2018
e58a837
Stop using transform-runtime
jeffposnick Aug 6, 2018
678aae5
Another depecdency update.
jeffposnick Aug 13, 2018
8e1ff45
Package updates and linting.
jeffposnick Oct 3, 2018
72ce0f0
Fix missing polyfills.
jeffposnick Oct 3, 2018
7430a47
WIP.
jeffposnick Oct 25, 2018
c57b0b3
Debugging.
jeffposnick Oct 25, 2018
680620f
Require the same html-webpack-plugin instance
jeffposnick Oct 30, 2018
a7f97bc
Getting closer?
jeffposnick Oct 30, 2018
89c8d42
Tweaks to README
jeffposnick Oct 30, 2018
8bda4ad
Added a package lock.
jeffposnick Oct 31, 2018
7905a9c
Merge pull request #82 from GoogleChromeLabs/html-plugin-updates
jeffposnick Oct 31, 2018
d007cf6
Get html-webpack-plugin instance from compilation.
jeffposnick Oct 31, 2018
52a3595
Modernization
jeffposnick Aug 21, 2019
fa21468
Travis tweak
jeffposnick Aug 21, 2019
5480093
Merge pull request #72 from GoogleChromeLabs/v3
jeffposnick Aug 21, 2019
6ca6ccd
README cleanup
jeffposnick Sep 5, 2019
81e83a2
Remove the demo.
jeffposnick Sep 5, 2019
082beca
Update year.
jeffposnick Sep 5, 2019
e4f4f68
Link to list of as values
jeffposnick Sep 5, 2019
799c757
Support for the media option
jeffposnick Sep 11, 2019
171d8fe
Merge pull request #99 from decrek/master
jeffposnick Oct 1, 2019
d55548c
Add deprecation notice
jeffposnick Jan 20, 2021
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
5 changes: 2 additions & 3 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
{
"plugins": ["@babel/transform-runtime"],
"presets": [
["@babel/preset-env", {
"targets": {
"node": "4.0.0"
}
"node": "8.0.0"
},
}]
]
}
17 changes: 5 additions & 12 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,7 @@
language: node_js

node_js:
- "7"
- "6"
- "5"
- "4"
env:
- CXX=g++-4.8
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-4.8
- 8

install:
- npm ci && cd test/webpack4 && npm ci && cd ../webpack4-htmlplugin4 && npm ci && cd ../..
4 changes: 2 additions & 2 deletions LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@
same "printed page" as the copyright notice for easier
identification within third-party archives.

Copyright 2014 Google Inc.
Copyright 2019 Google Inc.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
Expand All @@ -199,4 +199,4 @@
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
limitations under the License.
167 changes: 94 additions & 73 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,22 +1,14 @@
preload-webpack-plugin
============
[![NPM version][npm-img]][npm-url]
[![NPM downloads][npm-downloads-img]][npm-url]
[![Dependency Status][daviddm-img]][daviddm-url]
# preload-webpack-plugin

![preloads-plugin-compressor](https://cloud.githubusercontent.com/assets/110953/22451103/7700b812-e720-11e6-89e8-a6d4e3533159.png)
> **DEPRECATED: A fork of this project, https://github.com/vuejs/preload-webpack-plugin can be used instead.**

A Webpack plugin for automatically wiring up asynchronous (and other types) of JavaScript
A webpack plugin for automatically wiring up asynchronous (and other types) of JavaScript
chunks using `<link rel='preload'>`. This helps with lazy-loading.

Note: This is an extension plugin for [html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin) - a plugin that
Note: This is an extension plugin for [`html-webpack-plugin`](https://github.com/jantimon/html-webpack-plugin) - a plugin that
simplifies the creation of HTML files to serve your webpack bundles.

This plugin is a stop-gap until we add support for asynchronous chunk wiring to
[script-ext-html-webpack-plugin](https://github.com/numical/script-ext-html-webpack-plugin/pull/9).

Introduction
------------
## Introduction

[Preload](https://w3c.github.io/preload/) is a web standard aimed at improving performance
and granular loading of resources. It is a declarative fetch that can tell a browser to start fetching a
Expand All @@ -33,36 +25,28 @@ For example, `chunk.31132ae6680e598f8879.js`.
To make it easier to wire up async chunks for lazy-loading, this plugin offers a drop-in way to wire them up
using `<link rel='preload'>`.

Pre-requisites
--------------
This module requires Webpack 2.2.0 and above. It also requires that you're using
[html-webpack-plugin](https://github.com/ampedandwired/html-webpack-plugin) in your Webpack project.

Installation
---------------
## Prerequisites

First, install the package as a dependency in your package.json:
This module requires webpack v4 and above. It also requires that you're using
[`html-webpack-plugin`](https://github.com/ampedandwired/html-webpack-plugin) in your webpack project.

```sh
$ npm install --save-dev preload-webpack-plugin
```
## Installation

Alternatively, using yarn:
First, install the package as a dependency in your `package.json`:

```sh
yarn add -D preload-webpack-plugin
$ npm install --save-dev preload-webpack-plugin
```

Usage
-----------------
## Usage

Next, in your Webpack config, `require()` the preload plugin as follows:
In your webpack config, `require()` the preload plugin as follows:

```js
const PreloadWebpackPlugin = require('preload-webpack-plugin');
```

and finally, configure the plugin in your Webpack `plugins` array after `HtmlWebpackPlugin`:
and finally, add the plugin to your webpack configuration's `plugins` array after `HtmlWebpackPlugin`:

```js
plugins: [
Expand All @@ -89,9 +73,9 @@ plugins: [
]
```

In case you need more fine-grained control of the `as` atribute, you could also provide a function here.
When using it, entry name will be provided as the parameter, and function itself should return a
string for `as` attribute:
In case you need more fine-grained control of the `as` attribute, you could also
provide a function here. When using it, entry name will be provided as the
parameter, and function itself should return a string for `as` attribute:

```javascript
plugins: [
Expand All @@ -108,8 +92,11 @@ plugins: [
]
```

Notice that if `as=font` is used in preload, crossorigin will be added, otherwise the font resource
might be double fetched. Explains can be found in [this article](https://medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf).
Notice that if `as=font` is used in preload, the `crossorigin` will also be
added. Explains can be found in
[this article](https://medium.com/reloading/preload-prefetch-and-priorities-in-chrome-776165961bbf),
and a list of common `as` values can be
[found on MDN](https://developer.mozilla.org/en-US/docs/Web/HTML/Preloading_content#What_types_of_content_can_be_preloaded).

By default, the plugin will assume async script chunks will be preloaded. This is the equivalent of:

Expand All @@ -132,19 +119,25 @@ will be injected into the document `<head>`:
<link rel="preload" as="script" href="chunk.d15e7fdfc91b34bb78c4.js">
```

You can also configure the plugin to preload all chunks (vendor, async, normal chunks) using `include: 'all'`, or only preload initial chunks with `include: 'initial'`:
You can also configure the plugin to preload all chunks (vendor, async, and normal chunks) using
`include: 'allChunks'`, or only preload initial chunks with `include: 'initial'`.

It is very common in webpack to use loaders such as `file-loader` to generate assets for specific
types, such as fonts or images. If you wish to preload these files as well, even if they don't
belong to a chunk, you can use `include: 'allAssets'`.

```js
plugins: [
new HtmlWebpackPlugin(),
new PreloadWebpackPlugin({
rel: 'preload',
include: 'all' // or 'initial'
include: 'allChunks' // or 'initial', or 'allAssets'
})
]
```

In case you work with named chunks, you can explicitly specify which ones to `include` by passing an array:

```js
plugins: [
new HtmlWebpackPlugin(),
Expand All @@ -161,8 +154,7 @@ will inject just this:
<link rel="preload" as="script" href="home.31132ae6680e598f8879.js">
```

Filtering chunks
---------------------
### Filtering chunks

There may be chunks that you don't want to have preloaded (sourcemaps, for example). Before preloading each chunk, this plugin checks that the file does not match any regex in the `fileBlacklist` option. The default value of this blacklist is `[/\.map/]`, meaning no sourcemaps will be preloaded. You can easily override this:

Expand All @@ -180,8 +172,30 @@ new PreloadWebpackPlugin({
})
```

Resource Hints
---------------------
## Filtering HTML

You may not want to preload resources in some of your HTML files. You can use `excludeHtmlNames` to
tell this plugin to ignore one or more files.

```javascript
plugins: [
new HtmlWebpackPlugin({
filename: 'index.html',
template: 'src/index.html',
chunks: ['main']
}),
new HtmlWebpackPlugin({
filename: 'example.html',
template: 'src/example.html',
chunks: ['exampleEntry']
}),
// Only apply the plugin to index.html, not example.html.
new PreloadWebpackPlugin({
excludeHtmlNames: ['example.html'],
})
```

### Resource hints

Should you wish to use Resource Hints (such as `prefetch`) instead of `preload`, this plugin also supports wiring those up.

Expand All @@ -203,25 +217,33 @@ For the async chunks mentioned earlier, the plugin would update your HTML to the
<link rel="prefetch" href="chunk.d15e7fdfc91b34bb78c4.js">
```

Demo
----------------------
## Including media

A demo application implementing the [PRPL pattern](https://developers.google.com/web/fundamentals/performance/prpl-pattern/) with React that uses this plugin can be found in the `demo`
directory.
`<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.

Support
-------
You can pass the value for the media attribute in the `media` option:

If you've found an error in this sample, please file an issue:
[https://github.com/googlechrome/preload-webpack-plugin/issues](https://github.com/googlechrome/preload-webpack-plugin/issues)
```javascript
plugins: [
new HtmlWebpackPlugin(),
new PreloadWebpackPlugin({
rel: 'preload',
media: '(min-width: 600px)'
})
]
```

## Support

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

Patches are encouraged, and may be submitted by forking this project and
submitting a pull request through GitHub.

Contributing workflow
---------------------
## Contributing workflow

`index.js` contains the primary source for the plugin, `test` contains tests and `demo` contains demo code.
[`src/index.js`](src/index.js) and [`src/lib/`](src/lib/) contains the primary source for the plugin.
[`test/`](test/) contains tests.

Test the plugin:

Expand All @@ -230,35 +252,34 @@ $ npm install
$ npm run test
```

Lint the plugin:

```sh
$ npm run lint
$ npm run lint-fix # fix linting issues
```

The project is written in ES2015, but does not use a build-step. This may change depending on
any Node version support requests posted to the issue tracker.
The project is written in ES2015, and is transpiled to support node 6 and above.

Additional Notes
---------------------------
## Additional notes

* Be careful not to `preload` resources a user is unlikely to need. This can waste their bandwidth.
* Use `preload` for the current session if you think a user is likely to visit the next page. There is no
- Be careful not to `preload` resources a user is unlikely to need. This can waste their bandwidth.
- Use `preload` for the current session if you think a user is likely to visit the next page. There is no
100% guarantee preloaded items will end up in the HTTP Cache and read locally beyond this session.
* If optimising for future sessions, use `prefetch` and `preconnect`. Prefetched resources are maintained
- If optimizing for future sessions, use `prefetch` and `preconnect`. Prefetched resources are maintained
in the HTTP Cache for at least 5 minutes (in Chrome) regardless of the resource's cachability.

Related plugins
--------------------------
## Alternative tools

- webpack's native support:
As of the [v4.6.0 release](https://github.com/webpack/webpack/releases/tag/v4.6.0)
of webpack, there is native support for generating both prefetch and preload `<link>`s via ["magic" comments in your `import()` statements](https://medium.com/webpack/link-rel-prefetch-preload-in-webpack-51a52358f84c).

- [script-ext-html-webpack-plugin](https://github.com/numical/script-ext-html-webpack-plugin):
Enhances `html-webpack-plugin` with options including 'async', 'defer', 'module' and 'preload'.
As of [v1.7.0](https://github.com/numical/script-ext-html-webpack-plugin/pull/9#issuecomment-278239875),
it supports async chunks.

* [script-ext-html-webpack-plugin](https://github.com/numical/script-ext-html-webpack-plugin) - Enhances html-webpack-plugin with options including 'async', 'defer', 'module' and preload (no async chunk support yet)
* [resource-hints-webpack-plugin](https://github.com/jantimon/resource-hints-webpack-plugin) - Automatically wires resource hints for your resources (similarly no async chunk support)
- [resource-hints-webpack-plugin](https://github.com/jantimon/resource-hints-webpack-plugin):
Automatically wires resource hints for your resources. This plugin does does not currently
support async chunks.

License
-------
## License

Copyright 2017 Google, Inc.
Copyright 2019 Google, Inc.

Licensed to the Apache Software Foundation (ASF) under one or more contributor
license agreements. See the NOTICE file distributed with this work for
Expand Down
4 changes: 0 additions & 4 deletions demo/.babelrc

This file was deleted.

3 changes: 0 additions & 3 deletions demo/.editorconfig

This file was deleted.

28 changes: 0 additions & 28 deletions demo/.eslintrc

This file was deleted.

Loading