-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathgetStyleRule.ts
More file actions
75 lines (63 loc) · 2.12 KB
/
Copy pathgetStyleRule.ts
File metadata and controls
75 lines (63 loc) · 2.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
import type { Config } from "../types"
const { canProcess, moduleExists } = require("./helpers")
const { requireOrError } = require("./requireOrError")
const config = require("../config") as Config
const inliningCss = require("./inliningCss")
interface StyleRule {
test: RegExp
use: unknown[]
type?: string
}
const getStyleRule = (
test: RegExp,
preprocessors: unknown[] = []
): StyleRule | null => {
if (moduleExists("css-loader")) {
const tryPostcss = () =>
canProcess("postcss-loader", (loaderPath: string) => ({
loader: loaderPath,
options: { sourceMap: true }
}))
// style-loader is required when using css modules with HMR on the webpack-dev-server
const extractionPlugin =
config.assets_bundler === "rspack"
? requireOrError("@rspack/core").CssExtractRspackPlugin.loader
: requireOrError("mini-css-extract-plugin").loader
// Determine CSS Modules export mode based on configuration
// 'named' (default): Use named exports with camelCaseOnly (v9 behavior)
// 'default': Use default exports with camelCase (v8 behavior)
const useNamedExports = config.css_modules_export_mode !== "default"
const use = [
inliningCss ? "style-loader" : extractionPlugin,
{
loader: require.resolve("css-loader"),
options: {
sourceMap: true,
importLoaders: 2,
modules: {
auto: true,
// Use named exports for v9 (default), or default exports for v8 compatibility
namedExport: useNamedExports,
// 'camelCaseOnly' with namedExport: true (v9 default)
// 'camelCase' with namedExport: false (v8 behavior - exports both original and camelCase)
exportLocalsConvention: useNamedExports
? "camelCaseOnly"
: "camelCase"
}
}
},
tryPostcss(),
...preprocessors
].filter(Boolean)
const result: StyleRule = {
test,
use
}
if (config.assets_bundler === "rspack") {
result.type = "javascript/auto"
}
return result
}
return null
}
export = { getStyleRule }