-
Notifications
You must be signed in to change notification settings - Fork 6.8k
/
Copy pathcompiler-main.ts
72 lines (63 loc) · 2.35 KB
/
compiler-main.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import * as worker from '@bazel/worker';
import * as fs from 'fs';
import * as path from 'path';
import yargs from 'yargs';
import {OutputStyle, compile} from 'sass';
import {createLocalAngularPackageImporter} from './local-sass-importer';
const workerArgs = process.argv.slice(2);
// Note: This path is relative to the current working directory as build actions
// are always spawned in the execroot (which is exactly what we want).
const execrootProjectDir = path.resolve('./src/');
const localPackageSassImporter = createLocalAngularPackageImporter(execrootProjectDir);
if (require.main === module) {
main().catch(e => {
console.error(e);
process.exitCode = 1;
});
}
async function main() {
if (worker.runAsWorker(workerArgs)) {
await worker.runWorkerLoop(args =>
processBuildAction(args)
.then(() => true)
.catch(error => {
worker.log(error);
return false;
}),
);
} else {
// For non-worker mode, we parse the flag/params file ourselves. The Sass rule
// uses a multi-line params file (with `\n` used as separator).
const configFile = workerArgs[0].replace(/^@+/, '');
const configContent = fs.readFileSync(configFile, 'utf8').trim();
const args = configContent.split('\n');
await processBuildAction(args);
}
}
/**
* Processes a build action expressed through command line arguments
* as composed by the `sass_binary` rule.
*/
async function processBuildAction(args: string[]) {
const {loadPath, style, sourceMap, embedSources, inputExecpath, outExecpath} = await yargs(args)
.showHelpOnFail(false)
.strict()
.parserConfiguration({'greedy-arrays': false})
.command('$0 <inputExecpath> <outExecpath>', 'Compiles a Sass file')
.positional('inputExecpath', {type: 'string', demandOption: true})
.positional('outExecpath', {type: 'string', demandOption: true})
.option('embedSources', {type: 'boolean'})
.option('errorCss', {type: 'boolean'})
.option('sourceMap', {type: 'boolean'})
.option('loadPath', {type: 'array', string: true})
.option('style', {type: 'string'})
.parseAsync();
const result = compile(inputExecpath, {
style: style as OutputStyle,
sourceMap,
sourceMapIncludeSources: embedSources,
loadPaths: loadPath,
importers: [localPackageSassImporter],
});
await fs.promises.writeFile(outExecpath, result.css);
}