forked from Anidetrix/rollup-plugin-styles
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(sass): sync mode support with dart implementation
- Loading branch information
Showing
3 changed files
with
58 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,25 +1,42 @@ | ||
import path from "path"; | ||
import { sync as resolveSync } from "resolve"; | ||
import resolveAsync from "../../utils/resolve-async"; | ||
import { getUrlOfPartial, isModule, normalizeUrl } from "../../utils/url"; | ||
|
||
const importer: sass.Importer = (url, importer, done) => { | ||
// Do not add `.css` extension in order to inline the file | ||
const finishImport = (id: string): void => done({ file: id.replace(/\.css$/i, "") }); | ||
const extensions = [".scss", ".sass", ".css"]; | ||
|
||
// Pass responsibility back to other custom importers | ||
export const importer: sass.Importer = (url, importer, done): void => { | ||
const finalize = (id: string): void => done({ file: id.replace(/\.css$/i, "") }); | ||
const next = (): void => done(null); | ||
|
||
if (!isModule(url)) return next(); | ||
const moduleUrl = normalizeUrl(url); | ||
const partialUrl = getUrlOfPartial(moduleUrl); | ||
const options = { basedir: path.dirname(importer), extensions: [".scss", ".sass", ".css"] }; | ||
const options = { basedir: path.dirname(importer), extensions }; | ||
|
||
// Give precedence to importing a partial | ||
resolveAsync(partialUrl, options) | ||
.then(finishImport) | ||
.then(finalize) | ||
.catch(() => { | ||
resolveAsync(moduleUrl, options).then(finishImport).catch(next); | ||
resolveAsync(moduleUrl, options).then(finalize).catch(next); | ||
}); | ||
}; | ||
|
||
export default importer; | ||
const finalize = (id: string): sass.Data => ({ file: id.replace(/\.css$/i, "") }); | ||
export const importerSync: sass.Importer = (url, importer): sass.Data => { | ||
if (!isModule(url)) return null; | ||
const moduleUrl = normalizeUrl(url); | ||
const partialUrl = getUrlOfPartial(moduleUrl); | ||
const options = { basedir: path.dirname(importer), extensions }; | ||
|
||
// Give precedence to importing a partial | ||
try { | ||
try { | ||
return finalize(resolveSync(partialUrl, options)); | ||
} catch { | ||
return finalize(resolveSync(moduleUrl, options)); | ||
} | ||
} catch { | ||
return null; | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,29 +1,21 @@ | ||
import loadModule from "../../utils/load-module"; | ||
import arrayFmt from "../../utils/array-fmt"; | ||
|
||
const allSassIDs = ["node-sass", "sass"] as const; | ||
|
||
const idFmt = allSassIDs | ||
.map((id, i, arr) => { | ||
const newId = `\`${id}\``; | ||
if (i === arr.length - 2) return newId; | ||
if (i === arr.length - 1) return `or ${newId}`; | ||
return `${newId},`; | ||
}) | ||
.join(" "); | ||
|
||
export async function loadSass(impl?: string): Promise<[sass.Sass, string]> { | ||
const ids = ["node-sass", "sass"]; | ||
const idsFmt = arrayFmt(ids); | ||
export default function (impl?: string): [sass.Sass, string] { | ||
// Loading provided implementation | ||
if (impl) { | ||
const provided = await loadModule(impl); | ||
const provided = loadModule(impl); | ||
if (provided) return [provided as sass.Sass, impl]; | ||
throw new Error(`Could not load \`${impl}\` Sass implementation`); | ||
} | ||
|
||
// Loading one of the supported modules | ||
for await (const id of allSassIDs) { | ||
const sass = await loadModule(id); | ||
if (sass) return [sass, id]; | ||
for (const id of ids) { | ||
const sass = loadModule(id); | ||
if (sass) return [sass as sass.Sass, id]; | ||
} | ||
|
||
throw new Error(`You need to install ${idFmt} package in order to process Sass files`); | ||
throw new Error(`You need to install ${idsFmt} package in order to process Sass files`); | ||
} |