Resolve SCSS imports on Windows using Turbopack.#9
Conversation
- Add automatic includePaths configuration for Sass in Turbopack to ensure node_modules directory is included for SCSS import resolution - Normalize resource paths before passing to sass-loader on Windows to handle backslash path separators correctly - Add test to verify Bootstrap SCSS imports work with Turbopack Fixes vercel#86431
Fix: Bootstrap SCSS Imports in Turbopack on WindowsProblemOn Windows when using Turbopack with Next.js 16, Sass compilation fails when Bootstrap SCSS files attempt to import other SCSS files. Specifically, when Bootstrap's This issue only occurs on Windows with Turbopack. The same build works correctly on Linux/WSL and with webpack on Windows. Issue: #86431 Root CauseThe problem had two components:
Solution1. Automatic includePaths ConfigurationFile: Added logic to automatically include // Ensure includePaths is set for proper SCSS import resolution, especially on Windows
// This helps resolve imports like Bootstrap's @import "variables-dark" which looks
// for files in the same directory and includePaths
let mut sass_options_cloned = sass_options.clone();
let include_paths = sass_options_cloned
.get("includePaths")
.and_then(|v| v.as_array())
.map(|arr| arr.clone());
let mut include_paths_vec = include_paths.unwrap_or_default();
// Add node_modules directory to includePaths if not already present
// This ensures that SCSS imports from node_modules can be resolved correctly
let node_modules_path = project_path.join("node_modules")?;
let node_modules_path_str = node_modules_path.to_string().await?;
// Check if node_modules path is already in includePaths
let node_modules_already_included = include_paths_vec
.iter()
.any(|path| {
path.as_str()
.map(|s| s == node_modules_path_str.as_str())
.unwrap_or(false)
});
if !node_modules_already_included {
include_paths_vec.push(serde_json::Value::String(node_modules_path_str));
sass_options_cloned.insert(
"includePaths".to_string(),
serde_json::Value::Array(include_paths_vec),
);
}2. Path Normalization on WindowsFile: Added path normalization before passing resource paths to // Normalize paths to ensure consistent path separators, especially important on Windows
// This helps sass-loader resolve imports correctly by ensuring paths are properly formatted
const resource = pathResolve(contextDir, name)
const resourceDir = dirname(resource)
// Normalize paths to use forward slashes for consistency across platforms
// This is important for sass-loader which may have issues with backslashes on Windows
const normalizedResource = path.normalize(resource).replace(/\\/g, '/')
runLoaders(
{
resource: normalizedResource + query,
// ...
}
)Changes MadeModified Files
New Test FilesCreated test suite:
TestingManual TestingTo test the fix manually on Windows:
Automated TestingRun the test suite: # Run with Turbopack
pnpm test-start-turbo --testPathPattern="turbopack-bootstrap-scss-windows"
# Or run directly with Jest
IS_TURBOPACK_TEST=1 TURBOPACK_BUILD=1 NEXT_TEST_MODE=start pnpm testonly --testPathPattern="turbopack-bootstrap-scss-windows"The test verifies:
Before vs AfterBefore
After
Impact
Related Issues
Commit |
What?
This pull request resolves issues with SCSS imports on Windows when using Turbopack. It fixes backslash path separator handling and adds automatic
includePathsfor SCSS imports, ensuring the resolution ofnode_modules. A test is also included to verify Bootstrap SCSS compatibility with Turbopack.Why?
Addressing these issues ensures compatibility and reliability when using SCSS in Turbopack projects, especially with Bootstrap on Windows systems.
How?
includePathsconfiguration addsnode_modulesfor SCSS imports.sass-loaderon Windows.Closes vercel#86431
Fixes vercel#86431