javascript - webpack 排除属性与 webpack-node-externals 包

标签 javascript node.js reactjs webpack server-side-rendering

在 webpack 服务器配置文件中,

module: {
    rules: [
      {
        test: /\.js?$/,
        loader: "babel-loader",
        exclude: /node_modules/,
      }
    ]
  }

exclude 表示不编译那些目录,在本例中它只是 node_modules。然而,还有 webpack-node-externals 包。

const nodeWebExternals = require("webpack-node-externals");
  externals: [nodeWebExternals()],

module.exports 对象中的外部属性将告诉 webpack 不要将任何库捆绑到服务器上的输出包中,如果该库存在于 node-modules 文件夹中的话。

这两者有什么区别?

最佳答案

exclude 选项告诉 babel-loader 不要在 node_modules 文件夹内转换(现代语法到 es5)。如果没有 externals 部分,webpack 将不会转换语法,但会将正在使用的每个 node_module bundle 到最终 bundle。

通常当捆绑服务器代码时,不需要捆绑 node_modules 因为它作为文件夹存在于服务器中,因此,您告诉 webpack 标记那些模块作为 externals,它将生成一个常规 Node require 到模块而不是模块代码。

关于javascript - webpack 排除属性与 webpack-node-externals 包,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60127709/

相关文章:

javascript - 防止 React 渲染内循环

javascript - underscore - 使用 underscore.js 将复杂对象转换为数组

javascript - 如何在 mongodb 中将嵌入式键值对向上移动一级

android - jpm-mobile 失败并出现 ReferenceError : cmd is not defined

javascript - 无法在函数外部访问数组元素

reactjs - 使用 Props React 填充下拉选择

javascript - 如何使用 JavaScript 按 block 读取任何本地文件?

javascript - 仅使用 Javascript,如何防止输出禁用的表单元素?

javascript - 为什么我的 gulp 无法工作

reactjs - https 在 React Native axios 中未定义