feat: webpack优化

This commit is contained in:
hackrobot
2025-04-02 04:34:52 -05:00
parent 281780b54f
commit 87fffa4f37
2 changed files with 111 additions and 40 deletions

View File

@@ -1,5 +1,5 @@
import path from 'path'
const { GenerateSW } = require("workbox-webpack-plugin");
import path from "path";
// const { GenerateSW } = require("workbox-webpack-plugin");
const config = {
projectName: "cityh5",
@@ -27,42 +27,53 @@ const config = {
h5: {
webpackChain(chain) {
if (
process.env.NODE_ENV === "production" &&
!chain.plugins.has("workbox")
) {
chain.plugin("workbox").use(GenerateSW, [
chain.output.filename("static/js/[name].[contenthash:8].js");
chain.output.chunkFilename("static/js/[name].[contenthash:8].js");
chain
.plugin("MiniCssExtractPlugin")
.use(require("mini-css-extract-plugin"), [
{
clientsClaim: true,
skipWaiting: true,
maximumFileSizeToCacheInBytes: 10 * 1024 * 1024, // 10MB
runtimeCaching: [
{
urlPattern: /.*\.(?:png|jpg|jpeg|svg|gif|ico)/,
handler: "CacheFirst",
options: {
cacheName: "images-cache",
expiration: {
maxEntries: 50,
maxAgeSeconds: 30 * 24 * 60 * 60,
},
},
},
{
urlPattern: /.*\.(?:js|css)/,
handler: "StaleWhileRevalidate",
options: {
cacheName: "static-resources",
expiration: {
maxEntries: 50,
maxAgeSeconds: 7 * 24 * 60 * 60,
},
},
},
],
filename: "static/css/[name].[contenthash:8].css",
chunkFilename: "static/css/[name].[contenthash:8].css",
},
]);
}
// if (
// process.env.NODE_ENV === "production" &&
// !chain.plugins.has("workbox")
// ) {
// chain.plugin("workbox").use(GenerateSW, [
// {
// clientsClaim: true,
// skipWaiting: true,
// maximumFileSizeToCacheInBytes: 10 * 1024 * 1024, // 10MB
// runtimeCaching: [
// {
// urlPattern: /.*\.(?:png|jpg|jpeg|svg|gif|ico)/,
// handler: "CacheFirst",
// options: {
// cacheName: "images-cache",
// expiration: {
// maxEntries: 50,
// maxAgeSeconds: 30 * 24 * 60 * 60,
// },
// },
// },
// {
// urlPattern: /.*\.(?:js|css)/,
// handler: "StaleWhileRevalidate",
// options: {
// cacheName: "static-resources",
// expiration: {
// maxEntries: 50,
// maxAgeSeconds: 7 * 24 * 60 * 60,
// },
// },
// },
// ],
// },
// ]);
// }
chain.module
.rule("markdown")
.test(/\.md$/)
@@ -80,7 +91,9 @@ const config = {
},
esnextModules: ["taro-ui"],
publicPath: "/",
// publicPath: "/",
publicPath: `/v${new Date().getTime()}/`,
staticDirectory: "static",
postcss: {
autoprefixer: {
@@ -125,13 +138,15 @@ const config = {
},
},
// 允许特定域名访问
allowedHosts: [
// 'bot.hackrobot.cn'
"nomadyt.com",
],
allowedHosts: ["nomadyt.com", "localhost", ".nomadyt.com"], // 允许所有 *.nomadyt.com 子域访问
},
htmlPluginOption: {
favicon: path.resolve(__dirname, "..", "src", "images", "youtube.ico"), // 同样处理 favicon 路径
meta: {
"cache-control": "no-cache, no-store, must-revalidate",
pragma: "no-cache",
expires: "0",
},
},
target: ["web", "browserslist:> 0.25%, not dead"],