|
1 | 1 | import { createRequire } from 'node:module'; |
2 | | -import path from 'node:path'; |
3 | | -import type { |
4 | | - CSSLoaderOptions, |
5 | | - EnvironmentConfig, |
6 | | - RsbuildPlugin, |
7 | | -} from '@rsbuild/core'; |
8 | | -import { CSS_EXTENSIONS_PATTERN } from '../constant'; |
| 2 | +import type { EnvironmentConfig, RsbuildPlugin } from '@rsbuild/core'; |
9 | 3 | import { LibCssExtractPlugin } from './LibCssExtractPlugin'; |
| 4 | +import { |
| 5 | + type CssLoaderOptionsAuto, |
| 6 | + isCssFile, |
| 7 | + isCssModulesFile, |
| 8 | +} from './utils'; |
10 | 9 | const require = createRequire(import.meta.url); |
11 | 10 |
|
12 | 11 | export const RSLIB_CSS_ENTRY_FLAG = '__rslib_css__'; |
13 | 12 |
|
14 | | -// https://rsbuild.rs/config/output/css-modules#cssmodulesauto |
15 | | -export type CssLoaderOptionsAuto = CSSLoaderOptions['modules'] extends infer T |
16 | | - ? T extends { auto?: any } |
17 | | - ? T['auto'] |
18 | | - : never |
19 | | - : never; |
20 | | - |
21 | | -export function isCssFile(filepath: string): boolean { |
22 | | - return CSS_EXTENSIONS_PATTERN.test(filepath); |
23 | | -} |
24 | | - |
25 | | -const CSS_MODULE_REG = /\.module\.\w+$/i; |
26 | | - |
27 | | -/** |
28 | | - * This function is modified based on |
29 | | - * https://github.com/web-infra-dev/rspack/blob/7b80a45a1c58de7bc506dbb107fad6fda37d2a1f/packages/rspack/src/loader-runner/index.ts#L903 |
30 | | - */ |
31 | | -const PATH_QUERY_FRAGMENT_REGEXP = |
32 | | - /^((?:\u200b.|[^?#\u200b])*)(\?(?:\u200b.|[^#\u200b])*)?(#.*)?$/; |
33 | | -export function parsePathQueryFragment(str: string): { |
34 | | - path: string; |
35 | | - query: string; |
36 | | - fragment: string; |
37 | | -} { |
38 | | - const match = PATH_QUERY_FRAGMENT_REGEXP.exec(str); |
39 | | - return { |
40 | | - path: match?.[1]?.replace(/\u200b(.)/g, '$1') || '', |
41 | | - query: match?.[2] ? match[2].replace(/\u200b(.)/g, '$1') : '', |
42 | | - fragment: match?.[3] || '', |
43 | | - }; |
44 | | -} |
45 | | - |
46 | | -export function isCssModulesFile( |
47 | | - filepath: string, |
48 | | - auto: CssLoaderOptionsAuto, |
49 | | -): boolean { |
50 | | - const filename = path.basename(filepath); |
51 | | - if (auto === true) { |
52 | | - return CSS_MODULE_REG.test(filename); |
53 | | - } |
54 | | - |
55 | | - if (auto instanceof RegExp) { |
56 | | - return auto.test(filepath); |
57 | | - } |
58 | | - |
59 | | - if (typeof auto === 'function') { |
60 | | - const { path, query, fragment } = parsePathQueryFragment(filepath); |
61 | | - // this is a mock for loader |
62 | | - return auto(path, query, fragment); |
63 | | - } |
64 | | - |
65 | | - return false; |
66 | | -} |
67 | | - |
68 | | -export function isCssGlobalFile( |
69 | | - filepath: string, |
70 | | - auto: CssLoaderOptionsAuto, |
71 | | -): boolean { |
72 | | - const isCss = isCssFile(filepath); |
73 | | - if (!isCss) { |
74 | | - return false; |
75 | | - } |
76 | | - const isCssModules = isCssModulesFile(filepath, auto); |
77 | | - return !isCssModules; |
78 | | -} |
79 | | - |
80 | 13 | type ExternalCallback = (arg0?: undefined, arg1?: string) => void; |
81 | 14 |
|
82 | 15 | export async function cssExternalHandler( |
|
0 commit comments