import TsconfigPathsPlugin from 'tsconfig-paths-webpack-plugin' import FixDefaultImportPlugin from 'webpack-fix-default-import-plugin' import { findLoaderByRegex } from './findLoaderByRegex' import { insetBeforeIndex } from './insertBeforeIndex' import { safelyAddToArray } from './safelyAddToArray' export const extendWebpackConfig = (currentWebpackConfig: any, extendWebpackFile: string) => { insetBeforeIndex(currentWebpackConfig.module.rules)(0, { enforce: 'pre', loader: 'tslint-loader', options: { emitErrors: true, fix: true, }, test: /\.(ts|tsx)$/, }) const oneOfRules = currentWebpackConfig.module.rules[2].oneOf // 2 is a "magic number" const insetLoaderInRulesBeforeIndex = insetBeforeIndex(oneOfRules) insetLoaderInRulesBeforeIndex(0, { loader: 'ts-loader', options: { transpileOnly: true, }, test: /\.(tsx|ts)?$/, }) const cssIndex = findLoaderByRegex(/\.css$/.toString(), oneOfRules) insetLoaderInRulesBeforeIndex(cssIndex, { test: /\.module\.css$/, use: [ { loader: 'style-loader' }, { loader: 'typings-for-css-modules-loader', options: { camelcase: true, modules: true, namedExport: true, }, }, ], }) currentWebpackConfig.plugins = safelyAddToArray(currentWebpackConfig.plugins, new FixDefaultImportPlugin()) currentWebpackConfig.resolve.plugins = safelyAddToArray(currentWebpackConfig.resolve.plugins, new TsconfigPathsPlugin()) currentWebpackConfig.resolve.extensions.unshift('.ts', '.tsx') if (extendWebpackFile) { const filePath = process.cwd() + '/' + extendWebpackFile require(filePath)(currentWebpackConfig) } return currentWebpackConfig }