-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathtyped-scss-modules.config.ts
More file actions
59 lines (53 loc) · 1.81 KB
/
typed-scss-modules.config.ts
File metadata and controls
59 lines (53 loc) · 1.81 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
import { ConfigOptions } from 'typed-scss-modules/dist/lib/core';
import { fileURLToPath } from 'url';
import { dirname } from 'path';
import pnpapi from 'pnpapi';
import { LegacySyncImporter } from 'sass';
import path from 'node:path';
function getResolve(dirname: string) {
return (url: string) => pnpapi.resolveRequest(url, dirname);
}
// Adapted from https://github.com/styu/pnp-sass-importer/blob/main/packages/pnp-sass-importer/src/legacyImporter.ts
/**
*
* @param dirname The directory from which to resolve the request from
* @returns
*/
export function legacyImporter(dirname: string) {
const importer: LegacySyncImporter = (url) => {
const resolveFunc = getResolve(dirname);
const resolveUrlOrPartialUrl = (url) => {
try {
return resolveFunc(url);
} catch (error) {
const partialUrl = path.join(path.dirname(url), '_' + path.basename(url));
try {
return resolveFunc(partialUrl);
} catch {
// It's possible the package's exports weren't set up correctly and this URL is attempting to reach into the package nonetheless
// In that case, we can see if the URL is simply missing a .scss extension and try again
if (!url.endsWith('.scss')) {
return resolveUrlOrPartialUrl(url + '.scss');
} else {
console.log(error);
throw error;
}
}
}
};
try {
const file = resolveUrlOrPartialUrl(url);
return file ? { file } : null;
} catch {
return null;
}
};
return importer;
}
const importFilename = fileURLToPath(import.meta.url);
const importDirname = dirname(importFilename);
export const config: Partial<ConfigOptions> = {
banner: '// Generated by typed-scss-modules',
importer: [legacyImporter(importDirname)],
implementation: 'sass',
};