Skip to content

Commit

Permalink
fix(sass): properly call custom resolvers
Browse files Browse the repository at this point in the history
  • Loading branch information
Anidetrix committed May 20, 2020
1 parent c350df5 commit 002366f
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 2 deletions.
69 changes: 69 additions & 0 deletions __tests__/__snapshots__/index.test.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -2781,6 +2781,75 @@ injector_948a9cfd(css_a77306c7);
"
`;

exports[`sass importer: js 1`] = `
"/** @type {HTMLElement[]} */
var containers = [];
/** @type {{prepend:HTMLStyleElement,append:HTMLStyleElement}[]} */
var styleTags = [];
/**
* @param {string} css
* @param {object} [options={}]
* @param {boolean} [options.prepend]
* @param {boolean} [options.singleTag]
* @param {string} [options.container]
* @returns {void}
*/
function injector_bc6501d3 (css, options) {
if (!css || typeof document === \\"undefined\\") return;
if (typeof options === \\"undefined\\") options = {};
var position = options.prepend === true ? \\"prepend\\" : \\"append\\";
var singleTag = typeof options.singleTag !== \\"undefined\\" ? options.singleTag : false;
var container =
typeof options.container !== \\"undefined\\"
? document.querySelector(options.container)
: document.getElementsByTagName(\\"head\\")[0];
function createStyleTag() {
var styleTag = document.createElement(\\"style\\");
styleTag.setAttribute(\\"type\\", \\"text/css\\");
var pos = position === \\"prepend\\" ? \\"afterbegin\\" : \\"beforeend\\";
container.insertAdjacentElement(pos, styleTag);
return styleTag;
}
/** @type {HTMLStyleElement} */
var styleTag;
if (singleTag) {
var id = containers.indexOf(container);
if (id === -1) {
id = containers.push(container) - 1;
styleTags[id] = {};
}
if (styleTags[id] && styleTags[id][position]) {
styleTag = styleTags[id][position];
} else {
styleTag = styleTags[id][position] = createStyleTag();
}
} else {
styleTag = createStyleTag();
}
// strip potential UTF-8 BOM if css was read from a file
if (css.charCodeAt(0) === 0xfeff) css = css.substring(1);
if (styleTag.styleSheet) {
styleTag.styleSheet.cssText += css;
} else {
styleTag.appendChild(document.createTextNode(css));
}
}
const css_dba73744 = \\".virtual {\\\\n color: red; }\\\\n\\";
injector_bc6501d3(css_dba73744);
"
`;

exports[`sass modules: js 1`] = `
"/** @type {HTMLElement[]} */
var containers = [];
Expand Down
1 change: 1 addition & 0 deletions __tests__/fixtures/sass-importer/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import "./style.scss";
1 change: 1 addition & 0 deletions __tests__/fixtures/sass-importer/style.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
@import "virtualimport";
11 changes: 11 additions & 0 deletions __tests__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -298,6 +298,17 @@ validateMany("sass", [
title: "import",
input: "sass-import/index.js",
},
{
title: "importer",
input: "sass-importer/index.js",
options: {
sass: {
importer(_, __, done): void {
done({ contents: ".virtual{color:red}" });
},
},
},
},
]);

validateMany("less", [
Expand Down
3 changes: 1 addition & 2 deletions src/loaders/sass/importer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import resolveAsync from "../../utils/resolve-async";
import { getUrlOfPartial, isModule, normalizeUrl } from "../../utils/url";

const importer: sass.Importer = (url, importer, done) => {
if (!isModule(url)) return done({ file: url });

// Do not add `.css` extension in order to inline the file
const finishImport = (id: string): void => done({ file: id.replace(/\.css$/i, "") });

// Pass responsibility back to other custom importers
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"] };
Expand Down

0 comments on commit 002366f

Please sign in to comment.