Skip to content

Support sass partials imports and css imports in sass files #110

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jan 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/importers/__tests__/sassTildeImporter.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import sass from 'sass';
import { sassTildeImporter } from '../sassTildeImporter';

describe('importers / sassTildeImporter', () => {
const source = 'src/importers/sassTildeImporter.ts';
const done = (data: sass.ImporterReturnType) => void data;

it('should return null when not a tilde import', () => {
expect(sassTildeImporter('color.scss', source, done)).toBeNull();
});

it('should return null when node module does not exist', () => {
expect(
sassTildeImporter('~made_up_module/color.scss', source, done),
).toBeNull();
});

it('should resolve file from node_modules', () => {
expect(
sassTildeImporter('~bootstrap/scss/bootstrap', source, done),
).toMatchObject({ file: 'node_modules/bootstrap/scss/bootstrap.scss' });
});

it('should resolve sass partial from node_modules', () => {
// explicit
expect(
sassTildeImporter('~bootstrap/scss/_grid.scss', source, done),
).toMatchObject({ file: 'node_modules/bootstrap/scss/_grid.scss' });
expect(
sassTildeImporter('~bootstrap/scss/_grid', source, done),
).toMatchObject({ file: 'node_modules/bootstrap/scss/_grid.scss' });
// implicit
expect(
sassTildeImporter('~bootstrap/scss/grid.scss', source, done),
).toMatchObject({ file: 'node_modules/bootstrap/scss/_grid.scss' });
expect(
sassTildeImporter('~bootstrap/scss/grid', source, done),
).toMatchObject({ file: 'node_modules/bootstrap/scss/_grid.scss' });
});

it('should resolve .css files', () => {
expect(
sassTildeImporter('~bootstrap/dist/css/bootstrap-grid.css', source, done),
).toMatchObject({
file: 'node_modules/bootstrap/dist/css/bootstrap-grid.css',
});
expect(
sassTildeImporter('~bootstrap/dist/css/bootstrap-grid', source, done),
).toMatchObject({
file: 'node_modules/bootstrap/dist/css/bootstrap-grid.css',
});
});
});
21 changes: 19 additions & 2 deletions src/importers/sassTildeImporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,28 @@ export const sassTildeImporter: sass.Importer = (
// for an import of the form ~@foo/bar/baz(.(scss|sass))?
const nodeModSubpath = path.join('node_modules', rawImportPath.substring(1));
const subpathsWithExts: string[] = [];
if (nodeModSubpath.endsWith('.scss') || nodeModSubpath.endsWith('.sass')) {
if (
nodeModSubpath.endsWith('.scss') ||
nodeModSubpath.endsWith('.sass') ||
nodeModSubpath.endsWith('.css')
) {
subpathsWithExts.push(nodeModSubpath);
} else {
// Look for .scss first.
subpathsWithExts.push(`${nodeModSubpath}.scss`, `${nodeModSubpath}.sass`);
subpathsWithExts.push(
`${nodeModSubpath}.scss`,
`${nodeModSubpath}.sass`,
`${nodeModSubpath}.css`,
);
}

// Support sass partials by including paths where the file is prefixed by an underscore.
const basename = path.basename(nodeModSubpath);
if (!basename.startsWith('_')) {
const partials = subpathsWithExts.map((file) =>
file.replace(basename, `_${basename}`),
);
subpathsWithExts.push(...partials);
}

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