svelte-preprocess
Version:
A Svelte preprocessor wrapper with baked in support for common used preprocessors
47 lines (46 loc) • 1.61 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformer = void 0;
const utils_1 = require("../modules/utils");
let sass;
function getResultForResolve(result) {
var _a;
return {
code: result.css.toString(),
map: (_a = result.map) === null || _a === void 0 ? void 0 : _a.toString(),
dependencies: result.stats.includedFiles,
};
}
const transformer = async ({ content, filename, options = {}, }) => {
var _a;
let implementation = (_a = options === null || options === void 0 ? void 0 : options.implementation) !== null && _a !== void 0 ? _a : sass;
if (implementation == null) {
const mod = await utils_1.importAny('sass', 'node-sass');
// eslint-disable-next-line no-multi-assign
implementation = sass = mod.default;
}
const { renderSync, prependData, ...restOptions } = {
...options,
includePaths: utils_1.getIncludePaths(filename, options.includePaths),
outFile: `${filename}.css`,
};
const sassOptions = {
...restOptions,
data: content,
};
// scss errors if passed an empty string
if (sassOptions.data.length === 0) {
return { code: '' };
}
if (renderSync) {
return getResultForResolve(implementation.renderSync(sassOptions));
}
return new Promise((resolve, reject) => {
implementation.render(sassOptions, (err, result) => {
if (err)
return reject(err);
resolve(getResultForResolve(result));
});
});
};
exports.transformer = transformer;