Skip to content

Commit

Permalink
Fixing issue that was writing instead of appending and using async
Browse files Browse the repository at this point in the history
  • Loading branch information
Ray Booysen authored and Ray Booysen committed May 28, 2019
1 parent c6e8cfd commit f834cee
Showing 1 changed file with 31 additions and 25 deletions.
56 changes: 31 additions & 25 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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;
Expand All @@ -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;
}
}
}

0 comments on commit f834cee

Please sign in to comment.