From 679cf8fe5f061f34b059e087bc8694f1b94d452a Mon Sep 17 00:00:00 2001 From: meteorlxy Date: Thu, 25 Jul 2024 21:28:28 +0800 Subject: [PATCH] feat(bundler-webpack): normalize loader options type --- .../src/config/handleModuleStyles.ts | 6 +++-- packages/bundler-webpack/src/types.ts | 23 +++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/packages/bundler-webpack/src/config/handleModuleStyles.ts b/packages/bundler-webpack/src/config/handleModuleStyles.ts index ea799e5332..45b5bede25 100644 --- a/packages/bundler-webpack/src/config/handleModuleStyles.ts +++ b/packages/bundler-webpack/src/config/handleModuleStyles.ts @@ -4,8 +4,8 @@ import MiniCssExtractPlugin from 'mini-css-extract-plugin' import type Config from 'webpack-5-chain' import type { LessLoaderOptions, - LoaderOptions, SassLoaderOptions, + StylePreprocessorLoaderOptions, StylusLoaderOptions, WebpackBundlerOptions, } from '../types.js' @@ -26,7 +26,9 @@ export const handleModuleStyles = ({ isBuild: boolean isServer: boolean }): void => { - const handleStyle = ({ + const handleStyle = < + T extends StylePreprocessorLoaderOptions = StylePreprocessorLoaderOptions, + >({ lang, test, loaderName, diff --git a/packages/bundler-webpack/src/types.ts b/packages/bundler-webpack/src/types.ts index eea6ec05a3..9ce835dba1 100644 --- a/packages/bundler-webpack/src/types.ts +++ b/packages/bundler-webpack/src/types.ts @@ -77,17 +77,17 @@ export interface WebpackBundlerOptions { } /** - * Common options for some webpack loaders + * Common options for style preprocessor webpack loaders */ -export interface LoaderOptions { - sourceMap?: boolean - webpackImporter?: boolean +export interface StylePreprocessorLoaderOptions { additionalData?: | string | (( content: string, loaderContext: LoaderContext>, ) => string) + sourceMap?: boolean + webpackImporter?: boolean } /** @@ -102,10 +102,11 @@ export type StylePreprocessorOptions< * * @see https://github.com/webpack-contrib/postcss-loader#options */ -export interface PostcssLoaderOptions extends Pick { +export interface PostcssLoaderOptions + extends Pick { execute?: boolean - postcssOptions?: StylePreprocessorOptions implementation?: ((...args: any) => any) | string + postcssOptions?: StylePreprocessorOptions } /** @@ -113,7 +114,8 @@ export interface PostcssLoaderOptions extends Pick { * * @see https://github.com/webpack-contrib/stylus-loader#options */ -export interface StylusLoaderOptions extends LoaderOptions { +export interface StylusLoaderOptions extends StylePreprocessorLoaderOptions { + implementation?: ((...args: any) => any) | string stylusOptions?: StylePreprocessorOptions } @@ -122,10 +124,11 @@ export interface StylusLoaderOptions extends LoaderOptions { * * @see https://github.com/webpack-contrib/sass-loader#options */ -export interface SassLoaderOptions extends LoaderOptions { +export interface SassLoaderOptions extends StylePreprocessorLoaderOptions { api?: 'legacy' | 'modern' | 'modern-compiler' implementation?: Record | string sassOptions?: StylePreprocessorOptions + warnRuleAsWarning?: boolean } /** @@ -133,6 +136,8 @@ export interface SassLoaderOptions extends LoaderOptions { * * @see https://github.com/webpack-contrib/less-loader#options */ -export interface LessLoaderOptions extends LoaderOptions { +export interface LessLoaderOptions extends StylePreprocessorLoaderOptions { + implementation?: Record | string + lessLogAsWarnOrErr?: boolean lessOptions?: StylePreprocessorOptions }