sass compileString with in-memory importer: working versions (tested 2026-10-16)
Compile Sass from a string with dart-sass, using variables and mixins from an in-memory @use module provided by a custom canonicalize/load importer, compressed output, and an undefined-variable error assertion, all offline.
passed3 passed · 0 failed2026-10-16 00:54 UTC- Status
- passed
- Last passed
- 2026-10-16 00:54 UTC
- Tests
- 3 / 3
- Language
- TypeScript
- Runtime
- node v22.23.3
pinned versions3
sass@1.105.1@types/node@26.6.5tsx@4.23.15
run it
npm install --no-audit --no-fund --ignore-scripts=false npm test
test environment
node v22.23.3 linux 6.17.0-1022-azure x64 runner: github-actions 38831610868
The code
Exactly the files that passed. Your agent can pull them with get_recipe("sass-compile-string-importer") (install), or see them on GitHub.
package.json
{
"name": "sass-compile-string-importer",
"private": true,
"type": "module",
"scripts": {
"test": "node --import tsx --test test/*.test.ts"
},
"dependencies": {
"sass": "1.105.1"
},
"devDependencies": {
"@types/node": "26.6.5",
"tsx": "4.23.15"
}
}
src/compiler.ts
import * as sass from "sass";
export function createMemoryImporter(
modules: Record<string, string>,
): sass.Importer<"sync"> {
return {
canonicalize(url: string): URL | null {
return Object.prototype.hasOwnProperty.call(modules, url)
? new URL(`memory:${url}`)
: null;
},
load(canonicalUrl: URL): sass.ImporterResult | null {
const contents = modules[canonicalUrl.pathname];
if (contents === undefined) return null;
return { contents, syntax: "scss" };
},
};
}
export function compileWithModules(
source: string,
modules: Record<string, string>,
): string {
const result = sass.compileString(source, {
importers: [createMemoryImporter(modules)],
style: "compressed",
});
return result.css;
}
test/compiler.test.ts
import { test } from "node:test";
import assert from "node:assert/strict";
import { compileWithModules, createMemoryImporter } from "../src/compiler.ts";
const theme = `
$primary: #336699;
$radius: 4px;
@mixin rounded($r: $radius) {
border-radius: $r;
}
`;
test("compiles variables and mixins from an in-memory @use module", () => {
const css = compileWithModules(
`@use "theme";
.button {
color: theme.$primary;
@include theme.rounded(8px);
}`,
{ theme },
);
assert.equal(css.trim(), ".button{color:#369;border-radius:8px}");
});
test("throws on an undefined variable", () => {
assert.throws(
() =>
compileWithModules(
`@use "theme";
.bad { color: theme.$missing; }`,
{ theme: "$primary: #336699;" },
),
/Undefined variable/,
);
});
test("canonicalize resolves known modules and rejects unknown ones", () => {
const importer = createMemoryImporter({ theme: "$primary: red;" });
const canonical = importer.canonicalize("theme", {} as sass.CanonicalizeContext);
assert.equal(canonical?.pathname, "theme");
assert.equal(importer.canonicalize("nope", {} as sass.CanonicalizeContext), null);
});
Connect to ProvenPin
Add the free MCP server so your agent can pull this recipe and others like it. Same setup as the docs.
terminal
claude mcp add --transport http provenpin https://mcp.provenpin.com/mcp
~/.cursor/mcp.json
{ "mcpServers": { "provenpin": { "url": "https://mcp.provenpin.com/mcp" } } }.vscode/mcp.json
{ "servers": { "provenpin": { "type": "http", "url": "https://mcp.provenpin.com/mcp" } } }~/.codeium/windsurf/mcp_config.json
{ "mcpServers": { "provenpin": { "serverUrl": "https://mcp.provenpin.com/mcp" } } }MCP settings
{ "mcpServers": { "provenpin": { "type": "streamableHttp", "url": "https://mcp.provenpin.com/mcp" } } }Any other MCP client: Streamable HTTP at https://mcp.provenpin.com/mcp. No API key, no signup.