@Kolya112151

Как типизировать webpack.config.ts для Storybook?

Пытаюсь настроить svg в storybook, но когда пытаюсь типизировать, выдаёт ошибку из-за типа аргумента RuleSetRule.

// Часть кода, с происходящим:

  if (config.module?.rules) {
    config.module.rules = config.module.rules.map((rule: RuleSetRule) => {
      if (/svg/.test(rule.test as string)) {
        return { ...rule, exclude: /\.svg$/i }
      }
      return rule
    })
  }


// Ошибка, которая появляется:

Argument of type '(rule: RuleSetRule) => webpack.RuleSetRule' is not assignable to parameter of type '(value: false | "" | 0 | RuleSetRule | "..." | null | undefined, index: number, array: (false | "" | 0 | RuleSetRule | "..." | null | undefined)[]) => RuleSetRule'.
  Types of parameters 'rule' and 'value' are incompatible.
    Type 'false | "" | 0 | RuleSetRule | "..." | null | undefined' is not assignable to type 'RuleSetRule'.
      Type 'undefined' is not assignable to type 'RuleSetRule'.ts(2345)


Если я правильно понял, то ошибка связана с тем, что аргумент может иметь значение undefined. Но как решить эту проблему?

Лоадеры

import webpack from 'webpack'
import { BuildOptions } from './types/config'
import { buildCssLoader } from './loaders/buildCssLoader'

export const buildLoaders = (options: BuildOptions): webpack.RuleSetRule[] => {
  const { isDev } = options

  const fileLoader = {
    test: /\.(png|jpe?g|gif|woff2|woff)$/i,
    use: [
      {
        loader: 'file-loader'
      }
    ]
  }

  const svgLoader = {
    test: /\.svg$/,
    use: ['@svgr/webpack']
  }

  const cssLoader = buildCssLoader(isDev)

  const babelLoader = {
    test: /\.(js|jsx|ts|tsx)$/,
    exclude: /node_modules/,
    use: {
      loader: 'babel-loader',
      options: {
        presets: ['@babel/preset-env'],
        plugins: [
          [
            'i18next-extract',
            {
              locales: ['ru', 'en'],
              keyAsDefaultValue: true
            }
          ]
        ]
      }
    }
  }

  const typescriptLoader = {
    test: /\.tsx?$/,
    use: 'ts-loader',
    exclude: /node_modules/
  }

  return [fileLoader, svgLoader, babelLoader, typescriptLoader, cssLoader]
}


Полная часть конфига для сторибука

import webpack, { RuleSetRule } from 'webpack'
import { BuildPaths } from '../build/types/config'
import path from 'path'
import { buildCssLoader } from '../build/loaders/buildCssLoader'

export default ({ config }: { config: webpack.Configuration }) => {
  const paths: BuildPaths = {
    build: '',
    html: '',
    entry: '',
    src: path.resolve(__dirname, '..', '..', 'src')
  }

  config.resolve?.modules?.push(paths.src)
  config.resolve?.extensions?.push('.ts', '.tsx')

  if (config.module?.rules) {
    config.module.rules = config.module.rules.map((rule: RuleSetRule) => {
      if (/svg/.test(rule.test as string)) {
        return { ...rule, exclude: /\.svg$/i }
      }
      return rule
    })
  }

  config.module?.rules?.push(buildCssLoader(true))

  return config
}

  • Вопрос задан
  • 251 просмотр
Пригласить эксперта
Ответы на вопрос 1
@Pavelsiba
// попробуй вот так

if ((config.module?.rules) !== undefined) {
config.module.rules = config.module.rules.map((rule: RuleSetRule | '...') => {
if (rule !== '...' && rule.test instanceof RegExp && rule.test.toString().includes('svg')) {
return { ...rule, exclude: /\.svg$/i }
}
return rule
}
)
config.module.rules.push({
test: /\.svg$/i,
use: ['@svgr/webpack']
})
}
Ответ написан
Комментировать
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Войти через центр авторизации
Похожие вопросы