首页 > 解决方案 > 将未编译的打字稿库添加到 Craco 配置

问题描述

我有一个用 Typescript 编写的“公共库”,我想在更多项目中使用它。我不想从中创建一个 npm 包,我只想将它作为“未编译的包”导入,但我在设置它时遇到了麻烦。我尝试添加 ts-loader 并排除这样的模块:

webpackConfig.module.rules.push({
  test: /\.tsx?$/,
  use: 'ts-loader',
  exclude: /node_modules\/((?!common-library).)*$/,
});

但是,这会导致错误:TypeScript 没有为 /path/to/index,tsx 发出输出。

我也尝试使用craco-babel-loader https://github.com/rjerue/craco-babel-loader,但是将其添加到插件列表时:

{
  plugin: rewireBabelLoader,
  options: {
    includes: [resolveApp('node_modules/common-library')],
    excludes: [/node_modules/],
  }
},

我得到以下信息:

C:\path\to\app\node_modules\@craco\craco\lib\loaders.js:34
    rules.some((rule, index) => {
          ^

TypeError: rules.some is not a function

标签: reactjstypescriptcraco

解决方案


推荐阅读