rollup-plugin-sass
Version:
Rollup Sass files.
92 lines • 3.67 kB
JavaScript
;
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.getImporterListModern = exports.getImporterListLegacy = void 0;
const path_1 = require("path");
const resolve_1 = __importDefault(require("resolve"));
const logger_1 = require("./logger");
const url_1 = require("url");
const resolveAsync = (id, opts) => new Promise((res, rej) => (0, resolve_1.default)(id, opts, (err, resolved) => {
if (err) {
rej(err);
}
else {
res(resolved);
}
}));
const MATCH_NODE_MODULE_RE = /^~([a-z0-9]|@).+/i;
const getImporterListLegacy = (importOption) => {
let lastResult = Promise.resolve();
const importer1 = (url, prevUrl, done) => {
if (!MATCH_NODE_MODULE_RE.test(url)) {
return null;
}
const moduleUrl = url.slice(1);
const resolveOptions = {
basedir: (0, path_1.dirname)(prevUrl),
extensions: ['.scss', '.sass'],
};
lastResult = lastResult
.then(() => resolveAsync(moduleUrl, resolveOptions))
.then((file) => done({ file }))
.catch((err) => {
(0, logger_1.warn)('[rollup-plugin-sass]: Recovered from error: ', err);
if (Array.isArray(importOption) && importOption.length > 1) {
done(null);
}
else {
done({ file: url });
}
});
};
const extras = Array.isArray(importOption)
? importOption
: importOption
? [importOption]
: [];
return [importer1, ...extras];
};
exports.getImporterListLegacy = getImporterListLegacy;
const getImporterListModern = (importOption) => {
const importer = {
findFileUrl(url, context) {
return __awaiter(this, void 0, void 0, function* () {
if (!MATCH_NODE_MODULE_RE.test(url)) {
return null;
}
const moduleUrl = url.slice(1);
const resolveOptions = {
basedir: (0, path_1.dirname)((0, url_1.fileURLToPath)(context.containingUrl)),
extensions: ['.scss', '.sass'],
};
try {
const file = yield resolveAsync(moduleUrl, resolveOptions);
return (0, url_1.pathToFileURL)(file);
}
catch (err) {
(0, logger_1.warn)('[rollup-plugin-sass]: error resolving import path: ', err);
return null;
}
});
},
};
const extras = Array.isArray(importOption)
? importOption
: importOption
? [importOption]
: [];
return [importer, ...extras];
};
exports.getImporterListModern = getImporterListModern;
//# sourceMappingURL=getImporterList.js.map