Skip to content

Commit

Permalink
fix(package-importer): import files from node_modules correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
joneff committed Dec 7, 2022
1 parent f0b1446 commit 59e9586
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 12 deletions.
2 changes: 1 addition & 1 deletion src/importers/cache-importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,5 +48,5 @@ export function sassCacheImporter( options: CacheImporterOptions = { cache: true
return null;
};

return <SassImporter>sassCacheImporter;
return <SassImporter> sassCacheImporter;
}
45 changes: 34 additions & 11 deletions src/importers/package-importer.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import fs from 'fs';
import path from 'path';
import { pathToFileURL } from 'url';

Expand All @@ -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(
<string>cwd,
<string>nodeModules,
url.slice(1)
<string> cwd,
<string> nodeModules,
url
);

return { file };
if ( fs.existsSync( file ) ) {
return { file };
}

return null;
}
sassPackageImporter.name = 'sassPackageImporter';
sassPackageImporter.before = function( context: { cwd: string } ) : void {
Expand All @@ -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(
<string>cwd,
<string>nodeModules,
url.slice(1)
<string> cwd,
<string> nodeModules,
url
);

return pathToFileURL( file );
if ( fs.existsSync( file ) ) {
return pathToFileURL( file );
}

return null;
};

return <SassImporter>sassPackageImporter;
return <SassImporter> sassPackageImporter;
}

0 comments on commit 59e9586

Please sign in to comment.