-
Notifications
You must be signed in to change notification settings - Fork 34
/
index.ts
66 lines (62 loc) · 2.32 KB
/
index.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
import resolve from 'resolve';
import sass from 'sass';
import {normalizeRuleMatch} from '@reskript/core';
import {SettingsPlugin, BuildSettings, LoaderType} from '@reskript/settings';
import {SassLoaderOptions} from './interface.js';
export default (options: SassLoaderOptions = {}): SettingsPlugin => {
const finalizeBuild: BuildSettings['finalize'] = (config, entry, internals) => {
const {cwd, usage, projectSettings: {build: {style: {modules, extract}}}} = entry;
const final: LoaderType = (usage === 'build' && extract) ? 'cssExtract' : 'style';
const sassUse = {
loader: resolve.sync('sass-loader'),
options: {
implementation: options.implementation ?? sass,
sassOptions: options.sassOptions,
sourceMap: extract,
additionalData: options.additionalData,
},
};
const sassRule = {
test: /\.s[ac]ss$/,
oneOf: [
{
test: /\.global\.s[ac]ss$/,
use: [
...internals.loaders([final, 'css', 'postCSS'], entry),
sassUse,
],
},
{
resource: normalizeRuleMatch(cwd, modules),
use: [
...internals.loaders(['classNames', final, 'cssModules', 'postCSSModules'], entry),
sassUse,
],
},
{
use: [
...internals.loaders([final, 'css', 'postCSS'], entry),
sassUse,
],
},
],
};
if (!config.module?.rules) {
throw new Error('Webpack configuration provided to @reskript/plugin-sass is invalid (no existing rules)');
}
config.module.rules.push(sassRule);
return config;
};
return settings => {
return {
...settings,
build: {
...settings.build,
finalize: (config, env, internals) => {
const previous = settings.build.finalize(config, env, internals);
return finalizeBuild(previous, env, internals);
},
},
};
};
};