|
| 1 | +/* jshint node: true, esversion: 6 */ |
| 2 | +'use strict'; |
| 3 | + |
| 4 | +try { |
| 5 | + process.env.NODE_PATH += `:${process.env.PWD}/node_modules`; |
| 6 | + require('module').Module._initPaths(); |
| 7 | + require.resolve('stylus'); |
| 8 | + |
| 9 | + const Plugin = require('broccoli-caching-writer'); |
| 10 | + const fs = require('fs'); |
| 11 | + const fse = require('fs-extra'); |
| 12 | + const path = require('path'); |
| 13 | + const stylus = require('stylus'); |
| 14 | + |
| 15 | + class StylusPlugin extends Plugin { |
| 16 | + constructor(inputNodes, options) { |
| 17 | + super(inputNodes, {}); |
| 18 | + |
| 19 | + options = options || {}; |
| 20 | + Plugin.call(this, inputNodes, { |
| 21 | + cacheInclude: [/(.*?).styl$/] |
| 22 | + }); |
| 23 | + this.options = options; |
| 24 | + this.fileRegistry = []; |
| 25 | + } |
| 26 | + |
| 27 | + build() { |
| 28 | + const that = this; |
| 29 | + let entries = this.listEntries(); |
| 30 | + let rootFileNames = entries.map(e => { |
| 31 | + return path.resolve(e.basePath, e.relativePath); |
| 32 | + }); |
| 33 | + |
| 34 | + let promises = []; |
| 35 | + |
| 36 | + rootFileNames.forEach(fileName => { |
| 37 | + let p = new Promise(resolve => { |
| 38 | + that.compile(fileName, that.inputPaths[0], that.outputPath) |
| 39 | + .then(() => { |
| 40 | + resolve(); |
| 41 | + }); |
| 42 | + }); |
| 43 | + |
| 44 | + promises.push(p); |
| 45 | + }); |
| 46 | + |
| 47 | + return Promise.all(promises); |
| 48 | + } |
| 49 | + |
| 50 | + compile(fileName, inputPath, outputPath) { |
| 51 | + return new Promise(resolve => { |
| 52 | + let content = fs.readFileSync(fileName, 'utf8'); |
| 53 | + |
| 54 | + stylus.render(content, { filename: path.basename(fileName) }, function(err, css) { |
| 55 | + let filePath = fileName.replace(inputPath, outputPath).replace(/\.styl$/, '.css'); |
| 56 | + fse.outputFileSync(filePath, css, 'utf8'); |
| 57 | + resolve(); |
| 58 | + }); |
| 59 | + }); |
| 60 | + } |
| 61 | + } |
| 62 | + |
| 63 | + module.exports = StylusPlugin; |
| 64 | +} |
| 65 | +catch (e) { |
| 66 | + module.exports = null; |
| 67 | +} |
0 commit comments