Skip to content
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

Fix and add specs for relative imports with compileString() #1785

Merged
merged 1 commit into from
Apr 12, 2022
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
22 changes: 19 additions & 3 deletions js-api-spec/compile.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ describe('compileString', () => {
});
});

it('url is used to resolve relative loads', () =>
it('file: url is used to resolve relative loads', () =>
sandbox(dir => {
dir.write({'foo/bar/_other.scss': 'a {b: c}'});

Expand Down Expand Up @@ -244,14 +244,30 @@ describe('compileString', () => {

it('relative loads fail without a URL', () =>
sandbox(dir => {
dir.write({'other.scss': 'a {b: c}'});
dir.write({'_other.scss': 'a {b: c}'});

expect(() => compileString('@use "other";')).toThrowSassException({
expect(() =>
compileString(`@use "${dir.relativeUrl('other')}";`)
).toThrowSassException({
line: 0,
noUrl: true,
});
}));

it('relative loads fail with a non-file: URL', () =>
sandbox(dir => {
dir.write({'_other.scss': 'a {b: c}'});

expect(() =>
compileString(`@use "${dir.relativeUrl('other')}";`, {
url: new URL('unknown:style.scss'),
})
).toThrowSassException({
line: 0,
url: 'unknown:style.scss',
});
}));

describe('includes source span information', () => {
it('in syntax errors', () =>
sandbox(dir => {
Expand Down
12 changes: 12 additions & 0 deletions js-api-spec/sandbox.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,12 @@ export async function sandbox(
Object.assign((...paths: string[]) => p.join(testDir, ...paths), {
root: testDir,
url: (...paths: string[]) => pathToFileURL(p.join(testDir, ...paths)),
relativeUrl: (...paths: string[]) => {
const path = p.relative(process.cwd(), p.join(testDir, ...paths));
const components =
p.sep === '\\' ? path.split(/[/\\]/) : path.split('/');
return components.map(encodeURIComponent).join('/');
},
write: (paths: {[path: string]: string}) => {
for (const [path, contents] of Object.entries(paths)) {
const fullPath = p.join(testDir, path);
Expand Down Expand Up @@ -74,6 +80,12 @@ interface SandboxDirectory {
*/
url(...paths: string[]): URL;

/**
* Joins `paths` underneath `root` and converts the result to a relative
* `file:`-style URL.
*/
relativeUrl(...paths: string[]): string;

/**
* Writes `paths` to disk within this directory.
*
Expand Down