-
-
Notifications
You must be signed in to change notification settings - Fork 109
Expand file tree
/
Copy pathgetStyleRule.ts
More file actions
64 lines (54 loc) · 1.73 KB
/
Copy pathgetStyleRule.ts
File metadata and controls
64 lines (54 loc) · 1.73 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
/* eslint global-require: 0 */
const { canProcess, moduleExists } = require("./helpers")
const { requireOrError } = require("./requireOrError")
const config = require("../config")
const inliningCss = require("./inliningCss")
interface StyleRule {
test: RegExp
use: any[]
type?: string
}
const getStyleRule = (test: RegExp, preprocessors: any[] = []): 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
const use = [
inliningCss ? "style-loader" : extractionPlugin,
{
loader: require.resolve("css-loader"),
options: {
sourceMap: true,
importLoaders: 2,
modules: {
auto: true,
// v9 defaults: Use named exports with camelCase conversion
// Note: css-loader requires 'camelCaseOnly' or 'dashesOnly' when namedExport is true
// Using 'camelCase' with namedExport: true causes a build error
namedExport: true,
exportLocalsConvention: 'camelCaseOnly'
}
}
},
tryPostcss(),
...preprocessors
].filter(Boolean)
const result: StyleRule = {
test,
use
}
if (config.assets_bundler === "rspack") {
result.type = "javascript/auto"
}
return result
}
return null
}
export = { getStyleRule }