in packages/just-scripts/src/tasks/sassTask.ts [17:93]
export function sassTask(
optionsOrCreateSourceModule: SassTaskOptions | ((fileName: string, css: string) => string),
postcssPlugins?: any[],
): TaskFunction {
let createSourceModule: (fileName: string, css: string) => string;
if (typeof optionsOrCreateSourceModule === 'function') {
createSourceModule = optionsOrCreateSourceModule;
} else {
createSourceModule = optionsOrCreateSourceModule.createSourceModule;
postcssPlugins = optionsOrCreateSourceModule.postcssPlugins;
}
postcssPlugins = postcssPlugins || [];
return function sass(done: (err?: Error) => void) {
const nodeSass = tryRequire('node-sass');
const postcss = tryRequire('postcss');
const autoprefixer = tryRequire('autoprefixer');
const postcssRtl = tryRequire('postcss-rtl');
const clean = tryRequire('postcss-clean');
if (!nodeSass || !postcss || !autoprefixer) {
logger.warn('One of these [node-sass, postcss, autoprefixer] is not installed, so this task has no effect');
done();
return;
}
const autoprefixerFn = autoprefixer({ overrideBrowserslist: ['> 1%', 'last 2 versions', 'ie >= 11'] });
const files = glob.sync(path.resolve(process.cwd(), 'src/**/*.scss'));
if (files.length) {
const tasks = files.map(
(fileName: string) =>
function (cb: any) {
fileName = path.resolve(fileName);
nodeSass.render(
{
file: fileName,
importer: patchSassUrl,
includePaths: [path.resolve(process.cwd(), 'node_modules')],
},
(err: Error, result: { css: Buffer }) => {
if (err) {
cb(path.relative(process.cwd(), fileName) + ': ' + err);
} else {
const css = result.css.toString();
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
const plugins = [autoprefixerFn, ...postcssPlugins!];
// If the rtl plugin exists, insert it after autoprefix.
if (postcssRtl) {
plugins.splice(plugins.indexOf(autoprefixerFn) + 1, 0, postcssRtl({}));
}
// If postcss-clean exists, add it to the end of the chain.
if (clean) {
plugins.push(clean());
}
postcss(plugins)
.process(css, { from: fileName })
.then((result: { css: string }) => {
fs.writeFileSync(fileName + '.ts', createSourceModule(fileName, result.css));
cb();
});
}
},
);
},
);
parallelLimit(tasks, 5, done);
} else {
done();
}
};
}