Skip to content

Commit

Permalink
feat(bundler-webpack): normalize loader options type
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorlxy committed Jul 25, 2024
1 parent f3251b7 commit 679cf8f
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
6 changes: 4 additions & 2 deletions packages/bundler-webpack/src/config/handleModuleStyles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -26,7 +26,9 @@ export const handleModuleStyles = ({
isBuild: boolean
isServer: boolean
}): void => {
const handleStyle = <T extends LoaderOptions = LoaderOptions>({
const handleStyle = <
T extends StylePreprocessorLoaderOptions = StylePreprocessorLoaderOptions,
>({
lang,
test,
loaderName,
Expand Down
23 changes: 14 additions & 9 deletions packages/bundler-webpack/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Record<string, any>>,
) => string)
sourceMap?: boolean
webpackImporter?: boolean
}

/**
Expand All @@ -102,18 +102,20 @@ export type StylePreprocessorOptions<
*
* @see https://github.com/webpack-contrib/postcss-loader#options
*/
export interface PostcssLoaderOptions extends Pick<LoaderOptions, 'sourceMap'> {
export interface PostcssLoaderOptions
extends Pick<StylePreprocessorLoaderOptions, 'sourceMap'> {
execute?: boolean
postcssOptions?: StylePreprocessorOptions
implementation?: ((...args: any) => any) | string
postcssOptions?: StylePreprocessorOptions
}

/**
* Options for stylus-loader
*
* @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
}

Expand All @@ -122,17 +124,20 @@ 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, any> | string
sassOptions?: StylePreprocessorOptions
warnRuleAsWarning?: boolean
}

/**
* Options for less-loader
*
* @see https://github.com/webpack-contrib/less-loader#options
*/
export interface LessLoaderOptions extends LoaderOptions {
export interface LessLoaderOptions extends StylePreprocessorLoaderOptions {
implementation?: Record<string, any> | string
lessLogAsWarnOrErr?: boolean
lessOptions?: StylePreprocessorOptions
}

0 comments on commit 679cf8f

Please sign in to comment.