From 59e9586a884766ab265d674705c7a5a1edfe7ff9 Mon Sep 17 00:00:00 2001 From: joneff Date: Wed, 7 Dec 2022 11:53:45 +0200 Subject: [PATCH] fix(package-importer): import files from node_modules correctly --- src/importers/cache-importer.ts | 2 +- src/importers/package-importer.ts | 45 +++++++++++++++++++++++-------- 2 files changed, 35 insertions(+), 12 deletions(-) diff --git a/src/importers/cache-importer.ts b/src/importers/cache-importer.ts index e2126e5..df1f1e9 100644 --- a/src/importers/cache-importer.ts +++ b/src/importers/cache-importer.ts @@ -48,5 +48,5 @@ export function sassCacheImporter( options: CacheImporterOptions = { cache: true return null; }; - return sassCacheImporter; + return sassCacheImporter; } diff --git a/src/importers/package-importer.ts b/src/importers/package-importer.ts index 8de41c7..c0fee44 100644 --- a/src/importers/package-importer.ts +++ b/src/importers/package-importer.ts @@ -1,3 +1,4 @@ +import fs from 'fs'; import path from 'path'; import { pathToFileURL } from 'url'; @@ -16,17 +17,28 @@ export function sassPackageImporter( options?: PackageImporterOptions ) : SassIm function sassPackageImporter( url: string ) : LegacyImporterResult { - if ( !url.startsWith('~') ) { + // If the file exists, don't process it + if ( fs.existsSync( url ) ) { return null; } + // Remove leading tilde, if any + if ( url.startsWith('~') ) { + // eslint-disable-next-line no-param-reassign + url = url.slice(1); + } + const file = path.resolve( - cwd, - nodeModules, - url.slice(1) + cwd, + nodeModules, + url ); - return { file }; + if ( fs.existsSync( file ) ) { + return { file }; + } + + return null; } sassPackageImporter.name = 'sassPackageImporter'; sassPackageImporter.before = function( context: { cwd: string } ) : void { @@ -35,18 +47,29 @@ export function sassPackageImporter( options?: PackageImporterOptions ) : SassIm sassPackageImporter.findFileUrl = function( url: string ) : null | URL { - if ( !url.startsWith('~') ) { + // If the file exists, don't process it + if ( fs.existsSync( url ) ) { return null; } + // Remove leading tilde, if any + if ( url.startsWith('~') ) { + // eslint-disable-next-line no-param-reassign + url = url.slice(1); + } + const file = path.resolve( - cwd, - nodeModules, - url.slice(1) + cwd, + nodeModules, + url ); - return pathToFileURL( file ); + if ( fs.existsSync( file ) ) { + return pathToFileURL( file ); + } + + return null; }; - return sassPackageImporter; + return sassPackageImporter; }