Webpack chunk的url增加查询字符串

第一次写文章,分享一点 baidu 得不到的东西。

遇到的问题

使用Webpack做SPA项目,项目稍微大了之后,大家都会想到按需加载,比如 React 使用 React.Lazy 。然后我们的 webpack.config.js 这样写:

javascript 复制代码
module.export = {
  output: {
    path: "dist",
    filename: "[name].[contenthash:6].js",
    chunkFilename: "[name]-chunk.[contenthash:6].js",
  },
}

最后浏览器里我们的加载url可能就变成了这样的:

入口文件 https://site.example.com/static/js/index.f67ab1.js

chunk文件 https://site.example.com/static/js/settings-chunk.af21e1.js

入口文件是通过 HtmlWebpackPlugin 挂载到 html 上的,入口文件已经做到随意配置querystring了,但是 chunk 文件是动态加载的,也想把它改成如下:
https://site.example.com/static/js/settings-chunk.af21e1.js?h=[某动态参数]

解决方案

javascript 复制代码
// 解决 js 文件 querystring 问题
// eslint-disable-next-line prefer-const, no-var
declare var __webpack_get_script_filename__: (chunkId: string) => string; // NOSONAR
const oldFn = __webpack_get_script_filename__;
__webpack_get_script_filename__ = (chunkId) => {
  const filename = oldFn(chunkId);
  if (filename.includes("?")) {
    return filename + `&ts=${Date.now()}`;
  }
  return filename + `?ts=${Date.now()}`;
};

// 解决 css 文件 querystring 问题
declare namespace __webpack_require__ {
  // eslint-disable-next-line prefer-const, no-var
  var miniCssF: (chunkId: string) => string; // NOSONAR
}
const oldMiniCssF = __webpack_require__.miniCssF;
__webpack_require__.miniCssF = (chunkId) => {
  const filename = oldMiniCssF(chunkId);
  if (filename.includes("?")) {
    return filename + `&ts=${Date.now()}`;
  }
  return filename + `?ts=${Date.now()}`;
};

嘿嘿嘿

chunk js文件的方法,百度能搜到,但是chunk css 文件的方法,百度没搜到,拿去用吧。如果还是没生效,那就换个位置写一下,比如:

javascript 复制代码
import { somethingA } from './something-a';

// 解决 js 文件 querystring 问题
// eslint-disable-next-line prefer-const, no-var
declare var __webpack_get_script_filename__: (chunkId: string) => string; // NOSONAR
const oldFn = __webpack_get_script_filename__;
__webpack_get_script_filename__ = (chunkId) => {
  const filename = oldFn(chunkId);
  if (filename.includes("?")) {
    return filename + `&ts=${Date.now()}`;
  }
  return filename + `?ts=${Date.now()}`;
};

// 解决 css 文件 querystring 问题
declare namespace __webpack_require__ {
  // eslint-disable-next-line prefer-const, no-var
  var miniCssF: (chunkId: string) => string; // NOSONAR
}
const oldMiniCssF = __webpack_require__.miniCssF;
__webpack_require__.miniCssF = (chunkId) => {
  const filename = oldMiniCssF(chunkId);
  if (filename.includes("?")) {
    return filename + `&ts=${Date.now()}`;
  }
  return filename + `?ts=${Date.now()}`;
};

if (somethingA()) {
  import('../other-theme.less');
}

课后作业

__webpack_require__.miniCssF 是哪个plugin暴露出来的方法呢? 嗯~ o( ̄▽ ̄)o

相关推荐
Bellafu6663 小时前
selenium 常用xpath写法
前端·selenium·测试工具
blackorbird5 小时前
Edge 浏览器 IE 模式成攻击突破口:黑客借仿冒网站诱导攻击
前端·edge
谷歌开发者6 小时前
Web 开发指向标 | Chrome 开发者工具学习资源 (一)
前端·chrome·学习
名字越长技术越强6 小时前
Chrome和IE获取本机ip地址
前端
天***88966 小时前
Chrome 安装失败且提示“无可用的更新” 或 “与服务器的连接意外终止”,Chrome 离线版下载安装教程
前端·chrome
半梦半醒*6 小时前
zabbix安装
linux·运维·前端·网络·zabbix
清羽_ls7 小时前
React Hooks 核心规则&自定义 Hooks
前端·react.js·hooks
你的人类朋友7 小时前
“签名”这个概念是非对称加密独有的吗?
前端·后端·安全
西陵7 小时前
Nx带来极致的前端开发体验——任务缓存
前端·javascript·架构
10年前端老司机8 小时前
Promise 常见面试题(持续更新中)
前端·javascript