Skip to content

Commit d9afa51

Browse files
authored
feat: add Sass support for tsconfig path aliases (#105)
1 parent 44be26d commit d9afa51

File tree

7 files changed

+77
-1
lines changed

7 files changed

+77
-1
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@
7575
"postcss-load-config": "^2.1.0",
7676
"reserved-words": "^0.1.2",
7777
"sass": "^1.26.5",
78-
"stylus": "^0.54.7"
78+
"stylus": "^0.54.7",
79+
"tsconfig-paths": "^3.9.0"
7980
},
8081
"devDependencies": {
8182
"@types/icss-utils": "^4.1.0",

src/helpers/__tests__/__snapshots__/getDtsSnapshot.test.ts.snap

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,14 @@ Object {
99
}
1010
`;
1111

12+
exports[`utils / cssSnapshots with baseUrl and paths in compilerOptions sass should find the files 1`] = `
13+
Object {
14+
"big-font": "tsconfig-paths-module__big-font---3FOK9",
15+
"class-with-mixin": "tsconfig-paths-module__class-with-mixin---Uo34z",
16+
"public-module": "tsconfig-paths-module__public-module---2IHe1",
17+
}
18+
`;
19+
1220
exports[`utils / cssSnapshots with file 'empty.module.less' createExports should create an exports file 1`] = `
1321
"declare let classes: {
1422
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
.public-module {
2+
color: green;
3+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
@import '@scss/_external.module.scss';
2+
@import 'alias.scss';

src/helpers/__tests__/getDtsSnapshot.test.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,4 +186,29 @@ describe('utils / cssSnapshots', () => {
186186
expect(classes).toMatchSnapshot();
187187
});
188188
});
189+
190+
describe('with baseUrl and paths in compilerOptions', () => {
191+
const fileName = join(__dirname, 'fixtures', 'tsconfig-paths.module.scss');
192+
const css = readFileSync(fileName, 'utf8');
193+
const compilerOptions = {
194+
baseUrl: __dirname,
195+
paths: {
196+
'@scss/*': ['external/package/*'],
197+
'alias.scss': ['external/package/public.module.scss'],
198+
},
199+
};
200+
201+
it('sass should find the files', () => {
202+
const classes = getClasses({
203+
css,
204+
fileName,
205+
logger,
206+
options,
207+
processor,
208+
compilerOptions,
209+
});
210+
211+
expect(classes).toMatchSnapshot();
212+
});
213+
});
189214
});

src/helpers/getClasses.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import sass from 'sass';
55
import stylus from 'stylus';
66
import { extractICSS } from 'icss-utils';
77
import tsModule from 'typescript/lib/tsserverlibrary';
8+
import { createMatchPath } from 'tsconfig-paths';
89
import { Logger } from './logger';
910
import { Options, CustomRenderer } from '../options';
1011

@@ -71,6 +72,14 @@ export const getClasses = ({
7172
} else if (fileType === FileTypes.scss || fileType === FileTypes.sass) {
7273
const filePath = getFilePath(fileName);
7374
const { includePaths, ...sassOptions } = rendererOptions.sass || {};
75+
const { baseUrl, paths } = compilerOptions;
76+
const matchPath =
77+
baseUrl && paths ? createMatchPath(path.resolve(baseUrl), paths) : null;
78+
79+
const aliasImporter: sass.Importer = (url) => {
80+
const newUrl = matchPath !== null ? matchPath(url) : undefined;
81+
return newUrl ? { file: newUrl } : null;
82+
};
7483

7584
transformedCss = sass
7685
.renderSync({
@@ -80,6 +89,7 @@ export const getClasses = ({
8089
data: css.replace(/(@import ['"])~(?!\/)/gm, '$1'),
8190
indentedSyntax: fileType === FileTypes.sass,
8291
includePaths: [filePath, 'node_modules', ...(includePaths || [])],
92+
importer: [aliasImporter],
8393
...sassOptions,
8494
})
8595
.css.toString();

yarn.lock

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -605,6 +605,11 @@
605605
resolved "https://registry.yarnpkg.com/@types/json-schema/-/json-schema-7.0.3.tgz#bdfd69d61e464dcc81b25159c270d75a73c1a636"
606606
integrity sha512-Il2DtDVRGDcqjDtE+rF8iqg1CArehSK84HZJCT7AMITlyXRBpuPhqGLDQMowraqqu1coEaimg4ZOqggt6L6L+A==
607607

608+
"@types/json5@^0.0.29":
609+
version "0.0.29"
610+
resolved "https://registry.yarnpkg.com/@types/json5/-/json5-0.0.29.tgz#ee28707ae94e11d2b827bcbe5270bcea7f3e71ee"
611+
integrity sha1-7ihweulOEdK4J7y+UnC86n8+ce4=
612+
608613
"@types/less@^3.0.1":
609614
version "3.0.1"
610615
resolved "https://registry.yarnpkg.com/@types/less/-/less-3.0.1.tgz#625694093c72f8356c4042754e222407e50d6b08"
@@ -3084,6 +3089,13 @@ json5@^0.5.0:
30843089
resolved "https://registry.yarnpkg.com/json5/-/json5-0.5.1.tgz#1eade7acc012034ad84e2396767ead9fa5495821"
30853090
integrity sha1-Hq3nrMASA0rYTiOWdn6tn6VJWCE=
30863091

3092+
json5@^1.0.1:
3093+
version "1.0.1"
3094+
resolved "https://registry.yarnpkg.com/json5/-/json5-1.0.1.tgz#779fb0018604fa854eacbf6252180d83543e3dbe"
3095+
integrity sha512-aKS4WQjPenRxiQsC93MNfjx+nbF4PAdYzmd/1JIj8HYzqfbu86beTuNgXDzPknWk0n0uARlyewZo4s++ES36Ow==
3096+
dependencies:
3097+
minimist "^1.2.0"
3098+
30873099
jsprim@^1.2.2:
30883100
version "1.4.1"
30893101
resolved "https://registry.yarnpkg.com/jsprim/-/jsprim-1.4.1.tgz#313e66bc1e5cc06e438bc1b7499c2e5c56acb6a2"
@@ -4567,6 +4579,11 @@ strip-ansi@^6.0.0:
45674579
dependencies:
45684580
ansi-regex "^5.0.0"
45694581

4582+
strip-bom@^3.0.0:
4583+
version "3.0.0"
4584+
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-3.0.0.tgz#2334c18e9c759f7bdd56fdef7e9ae3d588e68ed3"
4585+
integrity sha1-IzTBjpx1n3vdVv3vfprj1YjmjtM=
4586+
45704587
strip-bom@^4.0.0:
45714588
version "4.0.0"
45724589
resolved "https://registry.yarnpkg.com/strip-bom/-/strip-bom-4.0.0.tgz#9c3505c1db45bcedca3d9cf7a16f5c5aa3901878"
@@ -4790,6 +4807,16 @@ ts-jest@^25.4.0:
47904807
semver "6.x"
47914808
yargs-parser "18.x"
47924809

4810+
tsconfig-paths@^3.9.0:
4811+
version "3.9.0"
4812+
resolved "https://registry.yarnpkg.com/tsconfig-paths/-/tsconfig-paths-3.9.0.tgz#098547a6c4448807e8fcb8eae081064ee9a3c90b"
4813+
integrity sha512-dRcuzokWhajtZWkQsDVKbWyY+jgcLC5sqJhg2PSgf4ZkH2aHPvaOY8YWGhmjb68b5qqTfasSsDO9k7RUiEmZAw==
4814+
dependencies:
4815+
"@types/json5" "^0.0.29"
4816+
json5 "^1.0.1"
4817+
minimist "^1.2.0"
4818+
strip-bom "^3.0.0"
4819+
47934820
tslib@^1.10.0:
47944821
version "1.11.0"
47954822
resolved "https://registry.yarnpkg.com/tslib/-/tslib-1.11.0.tgz#f1f3528301621a53220d58373ae510ff747a66bc"

0 commit comments

Comments
 (0)