From 2d390de52bf6e9d3d121103530325e557187b38f Mon Sep 17 00:00:00 2001 From: Kyle Herock Date: Sat, 15 Jan 2022 23:30:39 -0500 Subject: [PATCH] feat(sass): `exports` conditions (#197) * feat: add basic support resolving conditional package exports * resolve conflicts * adjust naming Co-authored-by: Anton Kudryavtsev --- src/loaders/sass/importer.ts | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/loaders/sass/importer.ts b/src/loaders/sass/importer.ts index 749e55f4..a3a865db 100644 --- a/src/loaders/sass/importer.ts +++ b/src/loaders/sass/importer.ts @@ -1,8 +1,9 @@ import path from "path"; -import { resolveAsync, resolveSync } from "../../utils/resolve"; +import { packageFilterBuiler, resolveAsync, resolveSync } from "../../utils/resolve"; import { getUrlOfPartial, isModule, normalizeUrl } from "../../utils/url"; const extensions = [".scss", ".sass", ".css"]; +const conditions = ["sass", "style"]; export const importer: sass.Importer = (url, importer, done): void => { const finalize = (id: string): void => done({ file: id.replace(/\.css$/i, "") }); @@ -11,7 +12,12 @@ export const importer: sass.Importer = (url, importer, done): void => { if (!isModule(url)) return next(); const moduleUrl = normalizeUrl(url); const partialUrl = getUrlOfPartial(moduleUrl); - const options = { caller: "Sass importer", basedirs: [path.dirname(importer)], extensions }; + const options = { + caller: "Sass importer", + basedirs: [path.dirname(importer)], + extensions, + packageFilter: packageFilterBuiler({ conditions }), + }; // Give precedence to importing a partial resolveAsync([partialUrl, moduleUrl], options).then(finalize).catch(next); }; @@ -21,7 +27,12 @@ export const importerSync: sass.Importer = (url, importer): sass.Data => { if (!isModule(url)) return null; const moduleUrl = normalizeUrl(url); const partialUrl = getUrlOfPartial(moduleUrl); - const options = { caller: "Sass importer", basedirs: [path.dirname(importer)], extensions }; + const options = { + caller: "Sass importer", + basedirs: [path.dirname(importer)], + extensions, + packageFilter: packageFilterBuiler({ conditions }), + }; // Give precedence to importing a partial try { return finalize(resolveSync([partialUrl, moduleUrl], options));