| Leo Repp | 58b9f11 | 2021-11-22 11:57:47 +0100 | [diff] [blame^] | 1 | /* eslint-disable prefer-object-spread, promise/prefer-await-to-then */ |
| 2 | 'use strict'; |
| 3 | const util = require('util'); |
| 4 | const path = require('path'); |
| 5 | |
| 6 | module.exports = grunt => { |
| 7 | grunt.registerMultiTask('sass', 'Compile Sass to CSS', function () { |
| 8 | const done = this.async(); |
| 9 | |
| 10 | const options = this.options({ |
| 11 | precision: 10 |
| 12 | }); |
| 13 | |
| 14 | if (!options.implementation) { |
| 15 | grunt.fatal('The implementation option must be passed to the Sass task'); |
| 16 | } |
| 17 | grunt.verbose.writeln(`\n${options.implementation.info}\n`); |
| 18 | |
| 19 | (async () => { |
| 20 | await Promise.all(this.files.map(async item => { |
| 21 | const [src] = item.src; |
| 22 | |
| 23 | if (!src || path.basename(src)[0] === '_') { |
| 24 | return; |
| 25 | } |
| 26 | |
| 27 | const result = await util.promisify(options.implementation.render)(Object.assign({}, options, { |
| 28 | file: src, |
| 29 | outFile: item.dest |
| 30 | })); |
| 31 | |
| 32 | grunt.file.write(item.dest, result.css); |
| 33 | |
| 34 | if (options.sourceMap) { |
| 35 | const filePath = options.sourceMap === true ? `${item.dest}.map` : options.sourceMap; |
| 36 | grunt.file.write(filePath, result.map); |
| 37 | } |
| 38 | })); |
| 39 | })().catch(error => { |
| 40 | grunt.fatal(error.formatted || error); |
| 41 | }).then(done); |
| 42 | }); |
| 43 | }; |