-
Notifications
You must be signed in to change notification settings - Fork 2.1k
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
Export default importer in the JavaScript API #3655
Comments
It seems like what you are looking for is writing an importer where you only implement Also note that you can "pass-through" a URL by having the function return https://sass-lang.com/documentation/js-api/interfaces/fileimporter/#findFileUrl.findFileUrl-1
https://sass-lang.com/documentation/js-api/interfaces/importer/#canonicalize.canonicalize-1
So you want something like... compileString('@import "blob:ui";', {
importers: [{ // importers, not importer
canonicalize(url, options) {
const u = new URL(url);
// Handle blobs only, let the rest URLs be loaded by
// other importers
return u.protocol === 'blob:' ? url : null;
},
load(url) {
// ... This load() will only work for "blob:" URLs
},
}, /* ... other importers? ... */]
}); |
Hi @Goodwine, thanks for the quick response. I think there has been some confusion — the challenge here is that only (The example was to illustrate how the default importer can be used as a drop-in when writing a custom importer, but not the actual use-case. I can see how that might have caused some confusion.) |
Even with |
Unless I'm missing the correct usage of // Compiles successfully
compileString('@import "partial";', {
url: pathToFileURL(path.join(__dirname, 'style.scss')),
}); // Fails to handle the import
compileString('@import "partial";', {
url: pathToFileURL(path.join(__dirname, 'style.scss')),
importer: {
canonicalize(url) { return null; },
load(url) { return null; },
}
}); However, the moment the When using For instance, in this contrived example where const result = compileString(`
@import "@peach";
@import "partial";
`, {
url: pathToFileURL(path.join(__dirname, 'style.scss')),
importer: {
canonicalize(url) {
const baseName = path.basename(url);
if (baseName.startsWith('@')) {
const filePath = fileURLToPath(url);
return new URL("color:" + filePath);
}
return null;
},
load(url) {
if (url.protocol === 'color:') {
const parentDir = path.dirname(url.pathname);
const baseName = path.basename(url.pathname, path.extname(url.pathname)).slice(1);
const jsonPath = path.join(parentDir, baseName + ".json");
const data = JSON.parse(fs.readFileSync(jsonPath, "utf8"));
return {
contents: `body { background-color: ${ data.color }; }`,
syntax: 'scss'
};
}
return null;
}
}
}); Would successfully handle |
Got it, thanks for the additional explanation, I'll reopen so @nex3 can see it and maybe comment additionally to this. Then, my take is that exporting |
We do provide access to the internal That said, for the use-case you describe here of effectively adding a new file extension that can be loaded as Sass (such as |
A quality-of-life suggestion (or work-around until #3247) — when authoring a custom importer it would be nice to be able to drop-in the existing default importer as a fall-back:
For instance, this would be functionally identical to:
But would allow for the following structure:
This would allow an author of an Importer to handle only their specific use-case, and not worry about re-implementing the file-naming edge-cases already supported by Sass.
I assume
importers
would be a conceptually better place for this kind of behaviour, as it is intended to handle fall-backs once Sass has had a first pass at it. However, this suggestion arises as theimporter
option resolves a relative URL to the parent file, butimporters
does not. Therefore, to create a custom importer that is relative to the parent file, onlyimporter
option can be used.As an aside, in the above example,
blob:
isn't a great fit as a different protocol would not resolve to a relative URL anyway, but it's just for illustrative purposes.The text was updated successfully, but these errors were encountered: