Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/append not write #26

Merged
merged 1 commit into from
Dec 31, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
}
}
}