Skip to content

Commit 2015f7c

Browse files
authored
feat: support sass partials and css imports in sass files (#110)
1 parent a68cd3d commit 2015f7c

File tree

2 files changed

+72
-2
lines changed

2 files changed

+72
-2
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
import sass from 'sass';
2+
import { sassTildeImporter } from '../sassTildeImporter';
3+
4+
describe('importers / sassTildeImporter', () => {
5+
const source = 'src/importers/sassTildeImporter.ts';
6+
const done = (data: sass.ImporterReturnType) => void data;
7+
8+
it('should return null when not a tilde import', () => {
9+
expect(sassTildeImporter('color.scss', source, done)).toBeNull();
10+
});
11+
12+
it('should return null when node module does not exist', () => {
13+
expect(
14+
sassTildeImporter('~made_up_module/color.scss', source, done),
15+
).toBeNull();
16+
});
17+
18+
it('should resolve file from node_modules', () => {
19+
expect(
20+
sassTildeImporter('~bootstrap/scss/bootstrap', source, done),
21+
).toMatchObject({ file: 'node_modules/bootstrap/scss/bootstrap.scss' });
22+
});
23+
24+
it('should resolve sass partial from node_modules', () => {
25+
// explicit
26+
expect(
27+
sassTildeImporter('~bootstrap/scss/_grid.scss', source, done),
28+
).toMatchObject({ file: 'node_modules/bootstrap/scss/_grid.scss' });
29+
expect(
30+
sassTildeImporter('~bootstrap/scss/_grid', source, done),
31+
).toMatchObject({ file: 'node_modules/bootstrap/scss/_grid.scss' });
32+
// implicit
33+
expect(
34+
sassTildeImporter('~bootstrap/scss/grid.scss', source, done),
35+
).toMatchObject({ file: 'node_modules/bootstrap/scss/_grid.scss' });
36+
expect(
37+
sassTildeImporter('~bootstrap/scss/grid', source, done),
38+
).toMatchObject({ file: 'node_modules/bootstrap/scss/_grid.scss' });
39+
});
40+
41+
it('should resolve .css files', () => {
42+
expect(
43+
sassTildeImporter('~bootstrap/dist/css/bootstrap-grid.css', source, done),
44+
).toMatchObject({
45+
file: 'node_modules/bootstrap/dist/css/bootstrap-grid.css',
46+
});
47+
expect(
48+
sassTildeImporter('~bootstrap/dist/css/bootstrap-grid', source, done),
49+
).toMatchObject({
50+
file: 'node_modules/bootstrap/dist/css/bootstrap-grid.css',
51+
});
52+
});
53+
});

src/importers/sassTildeImporter.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,28 @@ export const sassTildeImporter: sass.Importer = (
1919
// for an import of the form ~@foo/bar/baz(.(scss|sass))?
2020
const nodeModSubpath = path.join('node_modules', rawImportPath.substring(1));
2121
const subpathsWithExts: string[] = [];
22-
if (nodeModSubpath.endsWith('.scss') || nodeModSubpath.endsWith('.sass')) {
22+
if (
23+
nodeModSubpath.endsWith('.scss') ||
24+
nodeModSubpath.endsWith('.sass') ||
25+
nodeModSubpath.endsWith('.css')
26+
) {
2327
subpathsWithExts.push(nodeModSubpath);
2428
} else {
2529
// Look for .scss first.
26-
subpathsWithExts.push(`${nodeModSubpath}.scss`, `${nodeModSubpath}.sass`);
30+
subpathsWithExts.push(
31+
`${nodeModSubpath}.scss`,
32+
`${nodeModSubpath}.sass`,
33+
`${nodeModSubpath}.css`,
34+
);
35+
}
36+
37+
// Support sass partials by including paths where the file is prefixed by an underscore.
38+
const basename = path.basename(nodeModSubpath);
39+
if (!basename.startsWith('_')) {
40+
const partials = subpathsWithExts.map((file) =>
41+
file.replace(basename, `_${basename}`),
42+
);
43+
subpathsWithExts.push(...partials);
2744
}
2845

2946
// Climbs the filesystem tree until we get to the root, looking for the first

0 commit comments

Comments
 (0)