in internal/runner.js [44:137]
function compile(rawArgs) {
const args = minimist(rawArgs);
const cwd = process.cwd();
const data = argAsArray(args, 'data');
for (const pair of argAsArray(args, 'namedData')) {
const [name, value] = pair.split(':');
(data[name] = data[name] || []).push(value);
}
// These variables are documented in `postcss_binary`'s docstring and are
// fair game for plugin configuration to read.
const bazel = {
binDir: args.binDir,
data: data,
additionalOutputs: argAsArray(args, 'additionalOutputs'),
};
const cssString = fs.readFileSync(args.cssFile, 'utf8');
const options = {
// The path of the input CSS file.
from: args.cssFile,
// The path of the output CSS file.
to: args.outCssFile,
map: args.sourcemap
? {
// Don't output source map inline, we want it as a separate file.
inline: false,
// Whether to add (or modify, if already existing) the
// sourceMappingURL comment in the output .css to point to the
// output .css.map.
annotation: true,
}
: false,
};
// Get absolute output paths before we change directory.
const outCssPath = path.join(cwd, args.outCssFile);
const outCssMapPath =
args.outCssMapFile ? path.join(cwd, args.outCssMapFile) : null;
// We use two parallel arrays, PostCSS plugin requires => strings of JS args.
// To use in PostCSS, convert these into the actual plugin instances.
const pluginRequires = argAsArray(args, 'pluginRequires');
const pluginArgs = argAsArray(args, 'pluginArgs');
const pluginInstances = pluginRequires.map(
(nodeRequire, i) => {
// Try and resolve this plugin as a module identifier.
let plugin;
try {
plugin = require(nodeRequire);
} catch { }
// If it fails, use the runfile helper in case it's a workspace file.
if (!plugin) {
try {
plugin = require(runfiles.resolve(nodeRequire));
} catch { }
}
// If that still fails, throw an error.
if (!plugin) {
const e = new Error(
`could not resolve plugin with node require ${nodeRequire}`);
e.code = 'MODULE_NOT_FOUND';
throw e;
}
return plugin.apply(this, eval(pluginArgs[i]));
});
return postcss(pluginInstances)
.process(cssString, options)
.then(
result => {
fs.writeFileSync(outCssPath, result.css);
if (args.sourcemap && result.map) {
fs.writeFileSync(outCssMapPath, result.map.toString());
}
return true;
},
e => {
// cssnano (and possibly other packages) can emit errors that aren't
// instances of PostCSS's CssSyntaxError class, but that have a
// postcssNode field which allows us to construct our own
// CssSyntaxError. We do so because its error formatting is more
// thorough.
if (e.postcssNode) e = e.postcssNode.error(e.message);
console.warn(e.toString());
return false;
});
}