From 58c6b7422aedfd3fc4d5f3553b196da40eae1f4b Mon Sep 17 00:00:00 2001 From: Alexander Akait <4567934+alexander-akait@users.noreply.github.com> Date: Fri, 1 Nov 2024 03:46:06 +0300 Subject: [PATCH] fix: prefetch and preload runtime generation (#1116) --- src/index.js | 4 ++-- test/cases/prefetch-preload-hmr/a.css | 3 +++ test/cases/prefetch-preload-hmr/b.js | 0 .../prefetch-preload-hmr/expected/main.css | 4 ++++ test/cases/prefetch-preload-hmr/index.js | 3 +++ .../prefetch-preload-hmr/webpack.config.js | 21 +++++++++++++++++++ 6 files changed, 33 insertions(+), 2 deletions(-) create mode 100644 test/cases/prefetch-preload-hmr/a.css create mode 100644 test/cases/prefetch-preload-hmr/b.js create mode 100644 test/cases/prefetch-preload-hmr/expected/main.css create mode 100644 test/cases/prefetch-preload-hmr/index.js create mode 100644 test/cases/prefetch-preload-hmr/webpack.config.js diff --git a/src/index.js b/src/index.js index 7fc94b01..b7caa4bb 100644 --- a/src/index.js +++ b/src/index.js @@ -1123,7 +1123,7 @@ class MiniCssExtractPlugin { ]) : "// no hmr", "", - withPrefetch && hasCssMatcher !== false + withPrefetch && withLoading && hasCssMatcher !== false ? `${ RuntimeGlobals.prefetchChunkHandlers }.miniCss = ${runtimeTemplate.basicFunction("chunkId", [ @@ -1159,7 +1159,7 @@ class MiniCssExtractPlugin { ])};` : "// no prefetching", "", - withPreload && hasCssMatcher !== false + withPreload && withLoading && hasCssMatcher !== false ? `${ RuntimeGlobals.preloadChunkHandlers }.miniCss = ${runtimeTemplate.basicFunction("chunkId", [ diff --git a/test/cases/prefetch-preload-hmr/a.css b/test/cases/prefetch-preload-hmr/a.css new file mode 100644 index 00000000..5451a331 --- /dev/null +++ b/test/cases/prefetch-preload-hmr/a.css @@ -0,0 +1,3 @@ +.a { + color: red; +} diff --git a/test/cases/prefetch-preload-hmr/b.js b/test/cases/prefetch-preload-hmr/b.js new file mode 100644 index 00000000..e69de29b diff --git a/test/cases/prefetch-preload-hmr/expected/main.css b/test/cases/prefetch-preload-hmr/expected/main.css new file mode 100644 index 00000000..98f445d8 --- /dev/null +++ b/test/cases/prefetch-preload-hmr/expected/main.css @@ -0,0 +1,4 @@ +.a { + color: red; +} + diff --git a/test/cases/prefetch-preload-hmr/index.js b/test/cases/prefetch-preload-hmr/index.js new file mode 100644 index 00000000..e615a5ec --- /dev/null +++ b/test/cases/prefetch-preload-hmr/index.js @@ -0,0 +1,3 @@ +import "./a.css"; + +import(/* webpackPrefetch: true */ "./b"); diff --git a/test/cases/prefetch-preload-hmr/webpack.config.js b/test/cases/prefetch-preload-hmr/webpack.config.js new file mode 100644 index 00000000..d07a9495 --- /dev/null +++ b/test/cases/prefetch-preload-hmr/webpack.config.js @@ -0,0 +1,21 @@ +import webpack from "webpack"; + +import Self from "../../../src"; + +module.exports = { + entry: "./index.js", + module: { + rules: [ + { + test: /\.css$/, + use: [Self.loader, "css-loader"], + }, + ], + }, + plugins: [ + new Self({ + filename: "[name].css", + }), + new webpack.HotModuleReplacementPlugin(), + ], +};