Skip to content

Commit

Permalink
fix: fix inlineStyle filter function check
Browse files Browse the repository at this point in the history
  • Loading branch information
linbudu599 committed Jul 20, 2023
1 parent 19bdfb1 commit 970f879
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 12 deletions.
5 changes: 5 additions & 0 deletions .changeset/flat-dodos-sniff.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@ice/plugin-rax-compat': patch
---

fix inlineStyle filter function check
21 changes: 9 additions & 12 deletions packages/plugin-rax-compat/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,8 @@ const plugin: Plugin<CompatRaxOptions> = (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 || []),
Expand Down Expand Up @@ -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++) {
Expand All @@ -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;
};
Expand Down

0 comments on commit 970f879

Please sign in to comment.