【问题标题】:babel-loader not transpiling packages in node_modules even after specifying in exclude block to ignore the package (lerna)即使在排除块中指定忽略包(lerna)后,babel-loader 也不会在 node_modules 中转译包
【发布时间】:2019-02-13 11:44:47
【问题描述】:

所以我正在为我们的 React 应用程序尝试使用 lerna 的 monorepo 设计。 这个想法是创建一个 repo,它将所有的 react 项目作为lerna 包以及一些在应用程序之间共享的通用模块/组件。

现在所有这些通用模块/组件都是 es6 模块。没有被转译。因为公共模块也在不断发展。如果我们构建/转换它们,我确信在那之后反应 HMR 将不起作用(一个疯狂的猜测)。以下是我的目录结构

package.json lerna.json |--packages |--common |--react-app |--constants |--utilities

common 包含常见的 react 元素,例如 table,accordion 等,这些元素被导出为默认的 es6 模块。

react-appcommon 导入为 dependencyreact-app 有 webpack 构建配置集。

现在当我将common 模块导入我的react-app babel transform 时会出现此错误

Button.component.jsx 7:19
Module parse failed: Unexpected token (7:19)
You may need an appropriate loader to handle this file type.
| const { Search } = Input;
| class TextBoxWithButton extends React.Component {
>   static propTypes = {
|     placeholder: PropTypes.string.isRequired,
|     onAction: PropTypes.func.isRequired,
 @ ./src/App/Modules/Todo/Components/Header/Header.component.jsx 10:0-111 16:25-41
 @ ./src/App/Modules/Todo/Todo.component.jsx
 @ ./src/App/Router/index.jsx
 @ ./src/App/Layout/index.jsx
 @ ./src/App/index.jsx
 @ ./src/App.hot.js
 @ ./src/index.jsx

这意味着babel-loader 无法解析和转译node_nodules 文件夹中的内容,这是有道理的,因为预计所有依赖项都已转译。但不总是。如果你管理本地依赖项,你就不能一直构建它们(这就是我的想法)

现在我在网上找到了一些解决方案,使 1bable-loader 不排除 node_modules 或忽略 @mypackagein 排除正则表达式。但在我的情况下没有任何效果。

这是我到目前为止所尝试的。

  1. babel-loader 中删除exlucde: /node_modules/ => 不起作用
  2. 使用require.resolve('babel-loader') =>不工作
  3. 添加resolve.symlinks= false
  4. 添加resolve.modules='node_modules'path.resove(__dirname,'node_modules') => 不工作
  5. 将包路径添加到babel-loader 包括include: [srcPath, lernaPackagesPath]

似乎没有任何效果。 有什么我想念的吗? here 是我的 git test repo 的链接。

【问题讨论】:

标签: babeljs webpack-4 babel-loader lerna


【解决方案1】:

babel-loader 默认不会转译node_modules 中的任何内容。您可以在node_modules 中明确说明要转换的内容,但在@babel7.0.0 之后似乎也不起作用。 还有一个.babelrc 的范围,在@babel7.0.0 中引入。

根据我在正常情况下node_modules 所做的研究,期望已转译commonjsumd 模块。可以由任何应用程序导入。在我的情况下,我的包/组件中所有需要转译的 es6 模块。我的 webpack 构建失败了,因为 babel-loader 只是忽略了它们。

所以我决定使用@babel/cli 转译我的组件所在的每个包,我必须将.babelrc 以及其他配置添加到我的组件包中,并使用@babel/cli 构建它们

这是我的package.json 中的scripts

"scripts": {
    "start": "babel src --watch --out-dir dist --source-maps inline --copy-files --ignore spec.js,spec.jsx"
  },

然后我的 package.json 看起来像这样

{
  "name": "@pkg/components",
  "version": "1.0.1",
  "description": "a repository for react common components. may or may not be dependent on elements",
  "main": "dist/index.js",
  "author": "hannad rehman",
  "license": "MIT",
  "scripts": {
    "start": "babel src --watch --out-dir dist --source-maps inline --copy-files --ignore spec.js,spec.jsx"
  },
  "dependencies": {
    "@pkg/constants": "^1.0.1",
    "@pkg/elements": "^1.0.1"
  },
  "peerDependencies": {
    "prop-types": "^15.6.2",
    "react": "^16.4.2",
    "react-router-dom": "^4.3.1"
  }
}

采用这种方法。在任何应用程序可以导入它们之前,我所有的常用包都将进行单元测试、检查和构建。 babel 具有监视模式,可确保在发生更改时始终进行转译。

最后也是最重要的反应 HMR 按预期工作。

更新

上述解决方案确实有效,但几个月后我通过在 babel 加载器中使用 include 属性对其进行了更改

{
      test: /\.js(x?)$/,
      include: [/node_modules\/@pkg/],
      use: [
        'thread-loader',
        {
          loader: 'babel-loader',
          options: {
            cacheDirectory: true,
            configFile: path.resolve(
              __dirname,
              '../../../../',
              `${env.appConfig.folderSrc}/babel.config.js`,
            ),
          },
        },
      ],
    }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-02
    • 2019-08-25
    • 1970-01-01
    • 2017-12-01
    • 2019-05-31
    • 2022-09-29
    • 1970-01-01
    • 2019-06-06
    相关资源
    最近更新 更多