Webpack 1.13.2 实验 shell 下令办理 打印时没有配景致和笔墨颜色的题目

[复制链接]
发表于 2025-12-26 13:45:39 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×
这是由于 Webpack 1.13.2 不支持新的插件钩子 API。Webpack 1 的插件体系使用的是 plugin 方法,而不是 Webpack 4+ 中的 hooks。
在 Webpack 1 中,你可以使用以下代码来确保 sed 下令在打包完成后实验:
  1. const { exec } = require('child_process');
  2. module.exports = {
  3.   // 你的其他 Webpack 配置
  4.   plugins: [
  5.         function() {
  6.       this.plugin('compile', function() {
  7.         // `grep -F '@media print{*,:after,:before{color:#000!important;text-shadow:none!important;background:0 0!important;-webkit-box-shadow:none!important;box-shadow:none!important}' node_modules/bootstrap/dist/css/bootstrap.min.css`
  8.         // `sed -i 's|@media print{\\*,:after,:before{color:#000!important;text-shadow:none!important;background:0 0!important;-webkit-box-shadow:none!important;box-shadow:none!important}|@media print{|g' node_modules/bootstrap/dist/css/bootstrap.min.css`,
  9.         exec(
  10.           `grep -F '@media print{*,:after,:before{color:#000!important;text-shadow:none!important;background:0 0!important;-webkit-box-shadow:none!important;box-shadow:none!important}' node_modules/bootstrap/dist/css/bootstrap.min.css`,
  11.           (error, stdout, stderr) => {
  12.             if (error) {
  13.               // 如果处理过的 (code: 1), 就不再继续执行, 也不用打印错误信息
  14.               if (error.code !== 1) {
  15.                 console.error(`执行 grep 命令时出错: ${error.message}`);
  16.               }
  17.               return;
  18.             }
  19.             if (stderr) {
  20.               console.error(`grep 命令输出错误: ${stderr}`);
  21.               return;
  22.             }
  23.             console.log(`grep 命令执行完成`);
  24.             exec(
  25.               `sed -i 's|@media print{\\*,:after,:before{color:#000!important;text-shadow:none!important;background:0 0!important;-webkit-box-shadow:none!important;box-shadow:none!important}|@media print{|g' node_modules/bootstrap/dist/css/bootstrap.min.css`,
  26.               (error, stdout, stderr) => {
  27.                 if (error) {
  28.                   console.error(`执行 sed 命令时出错: ${error.message}`);
  29.                   return;
  30.                 }
  31.                 if (stderr) {
  32.                   console.error(`sed 命令输出错误: ${stderr}`);
  33.                   return;
  34.                 }
  35.                 console.log(`sed 命令执行完成`);
  36.               },
  37.             );
  38.           },
  39.         );
  40.       });
  41.     },
  42.   ],
  43. };
复制代码
阐明

  • 先通过 grep 全部是否须要修改, 假如不须要修改就不改了, 否则在 --watch 模式下会死循环的 build
  • this.plugin('compile', callback):compile 变乱在 Webpack 编译开始之前触发,因此 sed 下令会在打包之前实验。
  • exec 下令直接在 compile 钩子中实验 sed,确保更换操纵在打包完成后运行。
以上的 plugin 办理了引用 bootstrap 的 css 后, 打印时没有配景致和笔墨颜色的题目.
回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表