【问题标题】:webpack module loaders: exclude file in subfolderwebpack 模块加载器:排除子文件夹中的文件
【发布时间】:2017-11-27 19:05:17
【问题描述】:

我有一个 npm 库,正在为它构建一个示例。

项目看起来像

???? react-outline
├─???? example
│ └──???? webpack.config.js
│ └──???? .babelrc
│ └──???? index.js
├???? index.js????lib source 
└???? README.md

用户将cd在“example”文件夹中运行npm start

example/index.js 使用 React,但 /index.js(the lib) 没有。

  • 我正在通过 webpack > resolve > alias
  • 访问 lib

现在我可以修复错误了吗:Module build failed: Error: Couldn't find preset "react" relative to directory "/www/my-npm-lib"

webpack.config.js

var path = require("path");
module.exports = {
  target:"web",
  resolve:{
    alias:{
      'my-npm-lib':__dirname+'/../../index.js'
    }
  },
  module:{
      loaders:[{
                  test: /\.js$/,
                  loader:"babel-loader",
                  exclude:[path.resolve(__dirname,'/../../index.js'),"/node_modules/"],
                  query:{
                      presets:["react", "es2015"]
                  }
              }]
  }
};

.bablerc

{
  "presets": [
    [ "es2015"],
    "react"
  ],
  "env": {
      "test": {
        "presets": ["es2015","react"]
      }
  }
}

仅供参考:我使用 React Storybook 作为示例

【问题讨论】:

  • 您是否尝试配置外部? webpack.js.org/configuration/externals
  • 嗨,何塞,我将“resolve:{alias:”更改为“externals”,但得到invalid regular expression flag 真正奇怪的是在首页加载时显示此错误,但热重载修复了错误源更改,一切正常?我猜 webpack 可以工作,但 bable 是错误的?
  • @JoséQuintoZamora 外部组件在构建中包含 一个文件 .. 仅显示 module.exports = /home/user/www/my-npm-lib/index.js;,因为这不是有效的 javascript。我收到 错误 invalid regular expression flag 有什么想法吗?谢谢

标签: reactjs npm webpack babeljs


【解决方案1】:

试试

exclude: /node_modules\/(?!(index)\/).*/

【讨论】:

    猜你喜欢
    • 2017-10-12
    • 2020-05-05
    • 2018-06-30
    • 1970-01-01
    • 2016-03-25
    • 2020-07-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多