【问题标题】:webpack config that excludes all node_modules except one排除除一个以外的所有 node_modules 的 webpack 配置
【发布时间】:2017-12-24 02:27:29
【问题描述】:

我正在开发一个使用 skpmreact-sketchapp 的反应应用程序。我正在导入一个使用 ES.Next syntax 的节点模块,所以我需要使用 babel 来解析该模块目录(node_modules/react-native-calendars)。

skpm 充当应用程序的打包器,并将 webpack 与babel-loader 一起使用。 skpm 中的 webpack 配置文件设置为排除 node_modules 目录。因此,我在 node_modules/react-native-calendars 目录中遇到了构建错误。

我想我可以在我的项目的根目录中提供一个webpack.config.js 文件,该文件覆盖node_modules/react-native-calendarsnode_modules exlcude。

在我最初拥有的模块/规则中:

include: (/node_modules\/react-native-calendars/),
exclude: (/node_modules/),

但我想我需要类似的东西:

exclude: new RegExp (/node_modules\/(?!(react-native-calendars))/),

这是整个webpack.config.js

module.exports = {
  module: {
    rules: [
      {
        test: /\.jsx?$/,
        exclude: new RegExp (/node_modules\/(?!(react-native-calendars))/),
        use: {
          loader: 'babel-loader',
          options: {
            presets: ['react', 'es2015', 'stage-0'],
            plugins: ['transform-runtime', 'transform-class-properties']
          }
        }
      }
    ]
  }
};

这里是 repo 的链接(/src 中只有两个短文件和一个 webpack 配置):https://github.com/pcooney10/rn-calendar


编辑
我已将测试从/\.jsx$/ 更改为/\.jsx?$/,这是朝着正确方向迈出的一步,但现在看来/node_modules/react-native-calendars 中的所有导入都不起作用。我收到的日志页面显示Module not found: Error: Can't resolve 'SomeModule'Field 'browser' doesn't contain a valid alias configuration

编辑 2: 我需要某种path.resolve(__dirname,..) 声明吗?

【问题讨论】:

  • 您的test 明确用于.jsx 文件,但您说要处理.js。这是你的问题吗?
  • @loganfsmyth 我正在使用this example,它使用.jsx 而不是.js。当我将它切换到.js 时,我收到一大堆错误,说找不到我的模块。
  • 这是 github 上问题的discussion,它可能提供一些有用的上下文。 @loganfsmyth
  • 您的文件是.js 还是.jsx/\.jsx?$/ 将涵盖两者。我真的认为问题中没有足够的信息来回答这个问题。
  • 它们是 .js。这是指向repo 的链接,您可以在其中重现该问题,我还更新了我的原始问题,使其更具描述性。

标签: reactjs react-native webpack babeljs


【解决方案1】:

你可以这样做:

{  
  test: /\.jsx?$/,  
  exclude: /node_modules\/(?!THIS_MODULE|ANOTHER_MODULE)\/).*/
}

【讨论】:

  • 排除:/(node_modules|bower_components)/,
猜你喜欢
  • 1970-01-01
  • 2016-01-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2023-03-19
  • 1970-01-01
相关资源
最近更新 更多