Skip to content
Open
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,22 @@ For the async chunks mentioned earlier, the plugin would update your HTML to the
<link rel="prefetch" href="chunk.d15e7fdfc91b34bb78c4.js">
```

Usage with other bundlers with compatible HTML plugins
---------------------

If you are running a different bundler, such as [rspack](https://rspack.dev/), with a compatible API to webpack/html-webpack-plugin, you can pass the compatible plugin as `HtmlPlugin`.

Example with rspack:

```js
plugins: [
new rspack.HtmlRspackPlugin(),
new PreloadWebpackPlugin({
HtmlPlugin: rspack.HtmlRspackPlugin,
})
]
```

Demo
----------------------

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"node": ">=6.0.0"
},
"scripts": {
"init-project": "yarn && cd test/e2e/webpack4 && yarn",
"init-project": "yarn && cd test/e2e/webpack4 && yarn && cd ../rspack && yarn",
"lint": "eslint --format=codeframe .",
"test": "npm run lint && node test/copySrc.js && jasmine test/unit/* test/e2e/*/index.js",
"coverage": "nyc npm run test",
Expand Down
17 changes: 12 additions & 5 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const HtmlWebpackPlugin = require('html-webpack-plugin')
const defaultOptions = require('./lib/default-options')
const determineAsValue = require('./lib/determine-as-value')
const doesChunkBelongToHTML = require('./lib/does-chunk-belong-to-html')
Expand Down Expand Up @@ -102,7 +101,8 @@ class PreloadPlugin {

links.push({
tagName: 'link',
attributes
attributes,
voidTag: true
})
}

Expand All @@ -117,7 +117,7 @@ class PreloadPlugin {
}

const skip = data => {
const htmlFilename = data.plugin.options.filename
const htmlFilename = data.outputName
const exclude = this.options.excludeHtmlNames
const include = this.options.includeHtmlNames
return (
Expand All @@ -126,10 +126,17 @@ class PreloadPlugin {
)
}

// getHooks is deprecated in html-webpack-plugin and non-existent in rspack, so prefer new API
const getHtmlHooks = compilation => (
this.options.HtmlPlugin.getCompilationHooks
? this.options.HtmlPlugin.getCompilationHooks(compilation)
: this.options.HtmlPlugin.getHooks(compilation)
)

compiler.hooks.compilation.tap(
this.constructor.name,
compilation => {
HtmlWebpackPlugin.getHooks(compilation).beforeAssetTagGeneration.tapAsync(
getHtmlHooks(compilation).beforeAssetTagGeneration.tapAsync(
this.constructor.name,
(htmlPluginData, callback) => {
if (skip(htmlPluginData)) {
Expand All @@ -141,7 +148,7 @@ class PreloadPlugin {
}
)

HtmlWebpackPlugin.getHooks(compilation).alterAssetTags.tap(
getHtmlHooks(compilation).alterAssetTags.tap(
this.constructor.name,
(htmlPluginData) => {
if (skip(htmlPluginData)) {
Expand Down
10 changes: 9 additions & 1 deletion src/lib/default-options.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,20 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
let HtmlWebpackPlugin
try {
// eslint-disable-next-line global-require
HtmlWebpackPlugin = require('html-webpack-plugin')
} catch (error) {
// Ignore error, assume this is because a different HtmlPlugin, such as rspack's builtin, will be used
}

const defaultOptions = {
rel: 'preload',
include: 'asyncChunks',
excludeHtmlNames: [],
fileBlacklist: [/\.map/]
fileBlacklist: [/\.map/],
HtmlPlugin: HtmlWebpackPlugin
}

module.exports = defaultOptions
9 changes: 5 additions & 4 deletions src/lib/does-chunk-belong-to-html.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
* See the License for the specific language governing permissions and
* limitations under the License.
*/
const EntryPoint = require('webpack/lib/Entrypoint')

module.exports = function doesChunkBelongToHtml ({
chunk,
Expand Down Expand Up @@ -47,10 +46,12 @@ function recursiveChunkEntryName (chunk) {
}

function _recursiveChunkGroup (chunkGroup) {
if (chunkGroup instanceof EntryPoint) {
return chunkGroup.name
} else {
if (chunkGroup && chunkGroup.getParents) {
const [chunkParent] = chunkGroup.getParents()
return _recursiveChunkGroup(chunkParent)
} else if (chunkGroup && chunkGroup.name) {
return chunkGroup.name
} else {
return undefined
}
}
6 changes: 5 additions & 1 deletion src/lib/extract-chunks.js
Original file line number Diff line number Diff line change
Expand Up @@ -97,10 +97,14 @@ function extractChunks ({ compilation, optionsInclude }) {
}

if (includeType === 'allAssets') {
// rspack does not yet support assetsInfo, handle this
const assetsInfoValues = compilation.assetsInfo
? [...compilation.assetsInfo.values()]
: []
// Every asset, regardless of which chunk it's in.
// Wrap it in a single, "psuedo-chunk" return value.
// Note: webpack5 will extract license default, we do not need to preload them
const licenseAssets = [...compilation.assetsInfo.values()]
const licenseAssets = assetsInfoValues
.map((info) => {
if (info.related && info.related.license) {
return info.related.license
Expand Down
29 changes: 29 additions & 0 deletions test/e2e/rspack/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* @license
* Copyright 2018 Google Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* 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.
*/
const { rspack } = require('@rspack/core')

const PreloadPlugin = require('../../../src/index')
const testSpec = require('../../spec')

const descriptionPrefix = '[rspack]'

testSpec({
webpack: rspack,
HtmlWebpackPlugin: rspack.HtmlRspackPlugin,
PreloadPlugin,
descriptionPrefix
})
11 changes: 11 additions & 0 deletions test/e2e/rspack/package.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"name": "webpack3-tests",
"version": "1.0.0",
"description": "Test environment needed for rspack.",
"main": "index.js",
"author": "",
"license": "Apache-2.0",
"devDependencies": {
"@rspack/core": "^1.0.8"
}
}
111 changes: 111 additions & 0 deletions test/e2e/rspack/yarn.lock
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


"@module-federation/[email protected]":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@module-federation/runtime-tools/-/runtime-tools-0.5.1.tgz#1b1f93837159a6bf0c0ba78730d589a5a8f74aa3"
integrity sha512-nfBedkoZ3/SWyO0hnmaxuz0R0iGPSikHZOAZ0N/dVSQaIzlffUo35B5nlC2wgWIc0JdMZfkwkjZRrnuuDIJbzg==
dependencies:
"@module-federation/runtime" "0.5.1"
"@module-federation/webpack-bundler-runtime" "0.5.1"

"@module-federation/[email protected]":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@module-federation/runtime/-/runtime-0.5.1.tgz#b548a75e2068952ff66ad717cbf73fc921edd5d7"
integrity sha512-xgiMUWwGLWDrvZc9JibuEbXIbhXg6z2oUkemogSvQ4LKvrl/n0kbqP1Blk669mXzyWbqtSp6PpvNdwaE1aN5xQ==
dependencies:
"@module-federation/sdk" "0.5.1"

"@module-federation/[email protected]":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@module-federation/sdk/-/sdk-0.5.1.tgz#6c0a4053c23fa84db7aae7e4736496c541de7191"
integrity sha512-exvchtjNURJJkpqjQ3/opdbfeT2wPKvrbnGnyRkrwW5o3FH1LaST1tkiNviT6OXTexGaVc2DahbdniQHVtQ7pA==

"@module-federation/[email protected]":
version "0.5.1"
resolved "https://registry.yarnpkg.com/@module-federation/webpack-bundler-runtime/-/webpack-bundler-runtime-0.5.1.tgz#ef626af0d57e3568c474d66d7d3797366e09cafd"
integrity sha512-mMhRFH0k2VjwHt3Jol9JkUsmI/4XlrAoBG3E0o7HoyoPYv1UFOWyqAflfANcUPgbYpvqmyLzDcO+3IT36LXnrA==
dependencies:
"@module-federation/runtime" "0.5.1"
"@module-federation/sdk" "0.5.1"

"@rspack/[email protected]":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/binding-darwin-arm64/-/binding-darwin-arm64-1.0.8.tgz#ec66776dbad65f481b7dee93ad08a390fcbf7c17"
integrity sha512-1l8/eg3HNz53DHQO3fy5O5QKdYh8hSMZaWGtm3NR5IfdrTm2TaLL9tuR8oL2iHHtd87LEvVKHXdjlcuLV5IPNQ==

"@rspack/[email protected]":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/binding-darwin-x64/-/binding-darwin-x64-1.0.8.tgz#85d27372dd62e933ddd236d1f85ee6a97243886f"
integrity sha512-7BbG8gXVWjtqJegDpsObzM/B90Eig1piEtcahvPdvlC92uZz3/IwtKPpMaywGBrf5RSI3U0nQMSekwz0cO1SOw==

"@rspack/[email protected]":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/binding-linux-arm64-gnu/-/binding-linux-arm64-gnu-1.0.8.tgz#e958882680e6d95af19fbe73386d50f16ae524e6"
integrity sha512-QnqCL0wmwYqT/IFx5q0aw7DsIOr8oYUa4+7JI8iiqRf3RuuRJExesVW9VuWr0jS2UvChKgmb8PvRtDy/0tshFw==

"@rspack/[email protected]":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/binding-linux-arm64-musl/-/binding-linux-arm64-musl-1.0.8.tgz#7dc25acee1c414587d0fcad7f7a4cc08ceb442df"
integrity sha512-Ns9TsE7zdUjimW5HURRW08BaMyAh16MDh97PPsGEMeRPx9plnRO9aXvuUG6t+0gy4KwlQdeq3BvUsbBpIo5Tow==

"@rspack/[email protected]":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/binding-linux-x64-gnu/-/binding-linux-x64-gnu-1.0.8.tgz#86b81fe00c36fbb15123450dfc05dc202a49b910"
integrity sha512-lfqUuKCoyRN/gGeokhX/oNYqB6OpbtgQb57b0QuD8IaiH2a1ee0TtEVvRbyQNEDwht6lW4RTNg0RfMYu52LgXg==

"@rspack/[email protected]":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/binding-linux-x64-musl/-/binding-linux-x64-musl-1.0.8.tgz#6778f9a646674902d129a5bfcab5a2a8dc04c1e3"
integrity sha512-MgbHJWV5utVa1/U9skrXClydZ/eZw001++v4B6nb8myU6Ck1D02aMl9ESefb/sSA8TatLLxEXQ2VENG9stnPwQ==

"@rspack/[email protected]":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/binding-win32-arm64-msvc/-/binding-win32-arm64-msvc-1.0.8.tgz#b88bc3965d5ea508a571019dd5d497492f21bc67"
integrity sha512-3NN5VisnSOzhgqX77O/7NvcjPUueg1oIdMKoc5vElJCEu5FEXPqDhwZmr1PpBovaXshAcgExF3j54+20pwdg5g==

"@rspack/[email protected]":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/binding-win32-ia32-msvc/-/binding-win32-ia32-msvc-1.0.8.tgz#dd4dbd02027af60b2d1ed514466a76da701cb632"
integrity sha512-17VQNC7PSygzsipSVoukDM/SOcVueVNsk9bZiB0Swl20BaqrlBts2Dvlmo+L+ZGsxOYI97WvA/zomMDv860usg==

"@rspack/[email protected]":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/binding-win32-x64-msvc/-/binding-win32-x64-msvc-1.0.8.tgz#c58a9471da64a9cadc5ddccbc9ed6aeb9a3a70fe"
integrity sha512-Vtjt74Soh09XUsV5Nw0YjZVSk/qtsjtPnzbSZluncSAVUs8l+X1ALcM6n1Jrt3TLTfcqf7a+VIsWOXAMqkCGUg==

"@rspack/[email protected]":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/binding/-/binding-1.0.8.tgz#d0e09342fa9411ca88195c7d3ff414191cdf2bee"
integrity sha512-abRirbrjobcllLAamyeiWxT6Rb0wELUnITynQdqRbSweWm2lvnhm9YBv4BcOjvJBzhJtvRJo5JBtbKXjDTarug==
optionalDependencies:
"@rspack/binding-darwin-arm64" "1.0.8"
"@rspack/binding-darwin-x64" "1.0.8"
"@rspack/binding-linux-arm64-gnu" "1.0.8"
"@rspack/binding-linux-arm64-musl" "1.0.8"
"@rspack/binding-linux-x64-gnu" "1.0.8"
"@rspack/binding-linux-x64-musl" "1.0.8"
"@rspack/binding-win32-arm64-msvc" "1.0.8"
"@rspack/binding-win32-ia32-msvc" "1.0.8"
"@rspack/binding-win32-x64-msvc" "1.0.8"

"@rspack/core@^1.0.8":
version "1.0.8"
resolved "https://registry.yarnpkg.com/@rspack/core/-/core-1.0.8.tgz#49ea9d20427245b87a25188e60f820e256b202f4"
integrity sha512-pbXwXYb4WQwb0l35P5v3l/NpDJXy1WiVE4IcQ/6LxZYU5NyZuqtsK0trR88xIVRZb9qU0JUeCdQq7Xa6Q+c3Xw==
dependencies:
"@module-federation/runtime-tools" "0.5.1"
"@rspack/binding" "1.0.8"
"@rspack/lite-tapable" "1.0.1"
caniuse-lite "^1.0.30001616"

"@rspack/[email protected]":
version "1.0.1"
resolved "https://registry.yarnpkg.com/@rspack/lite-tapable/-/lite-tapable-1.0.1.tgz#d4540a5d28bd6177164bc0ba0bee4bdec0458591"
integrity sha512-VynGOEsVw2s8TAlLf/uESfrgfrq2+rcXB1muPJYBWbsm1Oa6r5qVQhjA5ggM6z/coYPrsVMgovl3Ff7Q7OCp1w==

caniuse-lite@^1.0.30001616:
version "1.0.30001667"
resolved "https://registry.yarnpkg.com/caniuse-lite/-/caniuse-lite-1.0.30001667.tgz#99fc5ea0d9c6e96897a104a8352604378377f949"
integrity sha512-7LTwJjcRkzKFmtqGsibMeuXmvFDfZq/nzIjnmgCGzKKRVzjD72selLDK1oPF/Oxzmt4fNcPvTDvGqSDG4tCALw==
5 changes: 1 addition & 4 deletions test/fixtures/file.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,4 @@
* limitations under the License.
*/
console.log('lol');
require.ensure(["./home.js"], function() {
// var a = require("module-a");
// console.log(a);
}, 'home');
import(/* webpackChunkName: "home" */ './home.js').then(function() { })
Loading