-
Notifications
You must be signed in to change notification settings - Fork 49
/
Copy pathwith-web.ts
114 lines (107 loc) · 3.15 KB
/
with-web.ts
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import { Configuration, RuleSetRule, rspack } from '@rspack/core';
import * as path from 'path';
import { SharedConfigContext } from './model';
export interface WithWebOptions {
stylePreprocessorOptions?: {
includePaths?: string[];
};
cssModules?: boolean;
}
export function withWeb(opts: WithWebOptions = {}) {
return function makeConfig(
config: Configuration,
{ options, context }: SharedConfigContext
): Configuration {
const isProd =
process.env.NODE_ENV === 'production' || options.mode === 'production';
const projectRoot = path.join(
context.root,
context.projectGraph.nodes[context.projectName].data.root
);
const includePaths: string[] = [];
if (opts?.stylePreprocessorOptions?.includePaths?.length > 0) {
opts.stylePreprocessorOptions.includePaths.forEach(
(includePath: string) =>
includePaths.push(path.resolve(context.root, includePath))
);
}
let lessPathOptions: { paths?: string[] } = {};
if (includePaths.length > 0) {
lessPathOptions = {
paths: includePaths,
};
}
return {
...config,
module: {
...config.module,
rules: [
...(config.module.rules || []),
{
test: /\.css$/,
type: opts?.cssModules ? 'css/module' : undefined,
},
{
test: /\.scss$|\.sass$/,
type: opts?.cssModules ? 'css/module' : undefined,
use: [
{
loader: require.resolve('sass-loader'),
options: {
sourceMap: !!options.sourceMap,
sassOptions: {
fiber: false,
// bootstrap-sass requires a minimum precision of 8
precision: 8,
includePaths,
},
},
},
],
},
{
test: /.less$/,
type: opts?.cssModules ? 'css/module' : undefined,
use: [
{
loader: require.resolve('less-loader'),
options: {
sourceMap: !!options.sourceMap,
lessOptions: {
javascriptEnabled: true,
...lessPathOptions,
},
},
},
],
},
{
test: /\.styl$/,
use: [
{
loader: require.resolve('stylus-loader'),
options: {
sourceMap: !!options.sourceMap,
stylusOptions: {
include: includePaths,
},
},
},
],
},
].filter((a): a is RuleSetRule => !!a),
},
plugins: [
...config.plugins,
new rspack.HtmlRspackPlugin({
template: options.indexHtml
? path.join(context.root, options.indexHtml)
: path.join(projectRoot, 'src/index.html'),
}),
new rspack.DefinePlugin({
'process.env.NODE_ENV': isProd ? "'production'" : "'development'",
})
],
};
};
}