diff --git a/src/index.js b/src/index.js index b94037e..d8aee58 100644 --- a/src/index.js +++ b/src/index.js @@ -6,33 +6,37 @@ import { insertStyle } from './style.js'; import mkdirp from 'mkdirp'; /** - * Write to a file even if its directory does not exist + * Appends to a file even if its directory does not exist * @param {String} path the path of the file write to * @param {String} contents contents of file - * @param {Function} cb callback function when write action finish */ -const writeFile = (path, contents, cb) => { - mkdirp(dirname(path), function (err) { - if (err && typeof cb === 'function') return cb(err); +const appendToFile = (path, contents) => { + return new Promise((resolve, reject) => { + mkdirp(dirname(path), function (err) { + if (err) { + reject(err) + } - fs.writeFile(path, contents, cb); + fs.appendFileSync(path, contents) + resolve(r); + }); }); } let renderSync = (code, option) => { return less.render(code, option) - .then(function(output){ + .then(function (output) { return output.css; - }, function(error){ + }, function (error) { throw error; }) }; let fileCount = 0; -export default function plugin (options = {}) { +export default function plugin(options = {}) { options.insert = options.insert || false; - const filter = createFilter(options.include || [ '**/*.less', '**/*.css' ], options.exclude || 'node_modules/**'); + const filter = createFilter(options.include || ['**/*.less', '**/*.css'], options.exclude || 'node_modules/**'); const injectFnName = '__$styleInject' return { @@ -51,33 +55,35 @@ export default function plugin (options = {}) { options.option['filename'] = id; options.output = options.output === undefined || options.output === true ? 'rollup.build.css' : options.output; if (options.plugins) { - options.option['plugins'] = options.plugins + options.option['plugins'] = options.plugins } let css = await renderSync(code, options.option); - if(options.output&&isFunc(options.output)){ + if (options.output && isFunc(options.output)) { css = await options.output(css, id); } - if (options.output&&isString(options.output)) { - if(fileCount == 1){ + if (options.output && isString(options.output)) { + if (fileCount == 1) { //clean the output file fs.removeSync(options.output); } - writeFile(options.output, css); + await appendToFile(options.output, css); } let exportCode = ''; - if(options.insert!=false){ + if (options.insert != false) { exportCode = `export default ${injectFnName}(${JSON.stringify(css.toString())});`; - }else{ + } else { exportCode = `export default ${JSON.stringify(css.toString())};`; } return { code: exportCode, - map: { mappings: '' } + map: { + mappings: '' + } }; } catch (error) { throw error; @@ -86,18 +92,18 @@ export default function plugin (options = {}) { }; }; -function isString (str) { - if(typeof str == 'string'){ +function isString(str) { + if (typeof str == 'string') { return true; - }else{ + } else { return false; } } -function isFunc (fn){ - if ( typeof fn == 'function' ){ +function isFunc(fn) { + if (typeof fn == 'function') { return true; - }else{ + } else { return false; } -} +} \ No newline at end of file