惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Project Zero
Project Zero
Engineering at Meta
Engineering at Meta
Microsoft Azure Blog
Microsoft Azure Blog
小众软件
小众软件
T
The Blog of Author Tim Ferriss
S
SegmentFault 最新的问题
量子位
V
Visual Studio Blog
F
Full Disclosure
博客园 - 叶小钗
Recent Announcements
Recent Announcements
G
Google Developers Blog
博客园 - Franky
F
Fortinet All Blogs
有赞技术团队
有赞技术团队
B
Blog
aimingoo的专栏
aimingoo的专栏
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
N
Netflix TechBlog - Medium
L
LINUX DO - 最新话题
S
Security @ Cisco Blogs
Google DeepMind News
Google DeepMind News
B
Blog RSS Feed
Cloudbric
Cloudbric
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
Last Week in AI
Last Week in AI
SecWiki News
SecWiki News
H
Hackread – Cybersecurity News, Data Breaches, AI and More
云风的 BLOG
云风的 BLOG
阮一峰的网络日志
阮一峰的网络日志
N
News and Events Feed by Topic
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
D
DataBreaches.Net
人人都是产品经理
人人都是产品经理
Schneier on Security
Schneier on Security
Webroot Blog
Webroot Blog
T
Troy Hunt's Blog
P
Proofpoint News Feed
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Jina AI
Jina AI
PCI Perspectives
PCI Perspectives
Martin Fowler
Martin Fowler
O
OpenAI News
Hacker News: Ask HN
Hacker News: Ask HN
S
Secure Thoughts
P
Privacy International News Feed
I
InfoQ
C
Cyber Attacks, Cyber Crime and Cyber Security

博客园 - 饭特稠

浪浪山老前端的2025 useAttrs 是响应式的吗(by 豆包) 实时互动教育版的自定义 UI 如何开发 2024,在路上 当蓝牙键盘连不上电脑:一次意外的debug之旅 前端技术选型时有用的网站 好用的zsh插件,打造好用的命令行 2023年终总结:怀孕,装修,还贷和其他 分享一个URL正则 使用 scriptable 实现每日诗词小组件 小程序中实现图片旋转后保存 Custom Elements 和 Shadow DOM了解一下? CSS 原生嵌套来了 聊聊CSS 缓动函数的新成员linear() chatgpt: 在ts中如何声明一个全局类型 Workbox -- 为serviceWorker量身定做的工具 cache API简介 重新学车 魔幻2022
如何使webpack编译 node_modules 中的 npm 包
饭特稠 · 2023-09-15 · via 博客园 - 饭特稠

What

在项目开发过程中,我们会使用到大量第三方的npm包,这些包大部分使用了 es中新的语法编写 ,但是在发布的时候,它们有些是经过 babel,tsc, esbuild 等工具转换后发布的,有的则没有转换直接发布到 npm 中,

所以当我们在 webpack 中使用这样的包时,可能会看到如下报错:

Why

上面的截图中使用了一个叫 screenfull 的包,点进去一看,原来是代码中使用了?.运算符,而在 webpack.config.js 中,由于 exclude 被设置为整个 node_modules, 从而使 babel 跳过了对 screenfull 的转换:

module: {
  rules: [
    {
      test: /\.(?:js|mjs|cjs)$/,
      exclude: /node_modules/,
      use: {
        loader: 'babel-loader',
        options: {
          presets: [
            ['@babel/preset-env', { targets: "defaults" }]
          ]
        }
      }
    }
  ]
}

How

这时,我们需要修改 exclude 的配置,确保报错的 npm 中的包的代码被转换。exclude,include 作为一个 Condition, 可以是下面的形式:

  • 字符串:匹配输入必须以提供的字符串开始。是的。目录绝对路径或文件绝对路径。
  • 正则表达式:test 输入值,如/node_modules/
  • 函数:调用输入的函数,必须返回一个真值(truthy value)以匹配。
  • 条件数组:至少满足一个匹配条件, 如[/node_modules/, /lib/]
  • 对象:所有属性对应的 condition 都要满足。每个属性都有一个定义行为。

所以上面的问题可以这样来配置:

module: {
  rules: [
    {
      test: /\.(?:js|mjs|cjs)$/,
+      exclude: {and: [/node_modules/], not: [/screenfull/] }, //screenfull是报错的npm 包
-      exclude: /node_modules/,
    }
  ]
}

除此以外,我们也可以定义一个condition函数,来完成同样的功能:


module: {
  rules: [
    {
      test: /\.(?:js|mjs|cjs)$/,
      exclude: (filepath) => {
        if (/node_modules/.test(filepath)) {
          if (/screenfull/.test(filepath)) {
             return false;
          }
          return true;
        }
        return false
      }
    }
  ]
}