From 970f8794b63666ffd6433c5af9c4a68b170dbb52 Mon Sep 17 00:00:00 2001 From: linbudu599 Date: Thu, 20 Jul 2023 18:01:19 +0800 Subject: [PATCH] fix: fix inlineStyle filter function check --- .changeset/flat-dodos-sniff.md | 5 +++++ packages/plugin-rax-compat/src/index.ts | 21 +++++++++------------ 2 files changed, 14 insertions(+), 12 deletions(-) create mode 100644 .changeset/flat-dodos-sniff.md diff --git a/.changeset/flat-dodos-sniff.md b/.changeset/flat-dodos-sniff.md new file mode 100644 index 0000000000..ba7d69a2ca --- /dev/null +++ b/.changeset/flat-dodos-sniff.md @@ -0,0 +1,5 @@ +--- +'@ice/plugin-rax-compat': patch +--- + +fix inlineStyle filter function check diff --git a/packages/plugin-rax-compat/src/index.ts b/packages/plugin-rax-compat/src/index.ts index be2efcd77b..b5bb96689d 100644 --- a/packages/plugin-rax-compat/src/index.ts +++ b/packages/plugin-rax-compat/src/index.ts @@ -155,12 +155,8 @@ const plugin: Plugin = (options = {}) => ({ config.configureWebpack ??= []; - // When code enters here, options.inlineStyle is either `true` or filter function. - // If user provide `true`, use a filter which always return true, or, use the filter. - const inlineStyleFilter = options.inlineStyle === true ? () => true : options.inlineStyle; - config.configureWebpack.unshift((config) => - styleSheetLoaderForClient(config, transformCssModule, inlineStyleFilter), + styleSheetLoaderForClient(config, transformCssModule, options.inlineStyle), ); config.transforms = [ ...(config.transforms || []), @@ -241,7 +237,7 @@ function getClassNameToStyleTransformer(syntaxFeatures) { * StyleSheet Loader for CSR. * Transform css files to inline style by webpack loader. */ -const styleSheetLoaderForClient = (config, transformCssModule, inlineStyleFiler: (id: string) => boolean) => { +const styleSheetLoaderForClient = (config, transformCssModule, inlineStyleFiler: CompatRaxOptions['inlineStyle']) => { const { rules } = config.module || {}; if (Array.isArray(rules)) { for (let i = 0, l = rules.length; i < l; i++) { @@ -251,14 +247,15 @@ const styleSheetLoaderForClient = (config, transformCssModule, inlineStyleFiler: // Apply inlineStyle here as original rule got higher priority, // the resource doesnot match the filter will be bypassed to stylesheet-loader. rule.test = (id: string) => { - const inlineStyleFilterEnabled = inlineStyleFiler(id) === true; - - inlineStyleFilterEnabled && consola.warn(`InlineStyle enabled for current css file: ${id}`); + const inlineStyleDisabled = typeof inlineStyleFiler === 'function' + // The tester returns false, means user want this file to be handled by ExternalStyleLoader. + ? inlineStyleFiler(id) === false + // inlineStyle: true, use the next InlineStyleLoader just like before. + : false; const matched = - (transformCssModule ? /(\.module|global)\.css$/i : /(\.global)\.css$/i).test(id) && - // If filter returns true, bypass the resource to stylesheet-loader. - !inlineStyleFilterEnabled; + (transformCssModule ? /(\.module|global)\.css$/i : /(\.global)\.css$/i).test(id) || + inlineStyleDisabled; return matched; };