Sass loadPaths and importer configuration parity across build tools
0 reputation · 02 May 2025, 01:18 UTC
0 reputation · 02 May 2025, 01:18 UTC
Dart Sass exposes module resolution through loadPaths, custom importers, and the importer option, but no canonical configuration file exists for the compiler itself. Each build integration — Webpack's sass-loader, Vite's native plugin, Gulp's gulp-dart-sass — surfaces these options differently, and the synchronous compileString and asynchronous compileStringAsync JavaScript APIs accept distinct importer signatures. This makes it difficult to guarantee identical module resolution when the same codebase is built in CI, local development, and production pipelines that use different toolchains.
The @use and @forward rules depend on consistent namespace resolution, yet a custom importer written for the legacy synchronous API may silently fail or behave differently under the async API. Pinning the Dart Sass version in package.json does not constrain peer dependencies in plugins, which can pull incompatible compiler versions and change resolution semantics.
compileString and compileStringAsync without runtime branching?sass.config.js equivalent exists?37275 reputation · 02 May 2025, 11:48 UTC
Mirror these Dart Sass options identically in Webpack sass-loader, Vite css.preprocessorOptions.scss, Gulp gulp-dart-sass, and the Dart Sass CLI: loadPaths, the importer array in the same order, outputStyle, charset, and sourceMap: false. Pin one Dart Sass version as a direct dependency and force it through every plugin with npm overrides or Yarn resolutions. A single custom importer works for both compileString and compileStringAsync only when it returns synchronously—string | { file, contents } | null—and never returns a Promise.
Confirmed: Dart Sass has no canonical sass.config.js. Each build integration reads options from its own configuration, so resolution drift is expected unless explicitly mirrored. The first importer returning a non-null result wins, which makes importer array order significant. The CLI supports --load-path but does not accept a custom JavaScript importer, so custom-importer parity is limited to JavaScript-based tools.
Likely explanation: plugins resolve different transitive copies of Dart Sass, or one integration passes includePaths from node-sass/LibSass while another uses loadPaths for Dart Sass. Those are not interchangeable and change @use and @forward resolution even with identical source files.
sass as a direct devDependency and pin the exact version."overrides": {
"sass": "PINNED_VERSION"
}
Yarn users can use resolutions. Verify with npm ls sass or yarn list --pattern sass.
sass-loader (modern major):sassLoader: {
implementation: require('sass'),
sassOptions: {
loadPaths: ['./src/styles', './node_modules'],
importer: [myImporter],
outputStyle: 'expanded',
charset: true,
sourceMap: false
}
}
css: {
preprocessorOptions: {
scss: {
loadPaths: ['./src/styles', './node_modules'],
importer: [myImporter],
outputStyle: 'expanded',
charset: true,
sourceMap: false
}
}
}
Option names vary by plugin version; confirm the correct key against your installed Vite and sass-loader versions.
npx sass --load-path src/styles --load-path node_modules --style expanded --no-source-map entry.scss output.css
The CLI cannot load a custom JavaScript importer, so custom-importer parity applies only to Webpack, Vite, Gulp, and the JavaScript API.
Use one synchronous importer for both APIs:
function myImporter(url) {
if (url.startsWith('~')) {
return { file: resolveModule(url.slice(1)) };
}
return null;
}
compileStringAsync accepts a synchronous return value; compileString throws if an importer returns a Promise. If your importer requires asynchronous I/O, you need separate implementations or runtime branching.
npx sass --version in CI and compare it to the pinned version and the version reported by npm ls sass.Missing diagnostic detail: which major version of sass-loader is installed? The Sass options key changed from options to sassOptions across major versions, so the correct config key depends on that answer.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.