Skip to content

Commit

Permalink
refactor(generateBundle): convert to async function
Browse files Browse the repository at this point in the history
  • Loading branch information
marcalexiei committed Jan 7, 2025
1 parent affe318 commit 06f0f60
Showing 1 changed file with 19 additions and 19 deletions.
38 changes: 19 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,7 @@ import { createFilter } from '@rollup/pluginutils';
*/
import type { Plugin as RollupPlugin, TransformResult } from 'rollup';

import type {
RollupPluginSassOptions,
RollupPluginSassOutputFn,
RollupPluginSassState,
} from './types';
import type { RollupPluginSassOptions, RollupPluginSassState } from './types';
import {
getImporterListLegacy,
getImporterListModern,
Expand Down Expand Up @@ -182,36 +178,40 @@ export = function plugin(
}
},

generateBundle(outputOptions, _, isWrite) {
async generateBundle(outputOptions, _, isWrite) {
const { styles } = pluginState;
const { output, insert } = pluginOptions;

if (!isWrite || (!insert && (!styles.length || output === false))) {
return Promise.resolve();
return;
}

const css = styles.map((style) => style.content).join('');

if (typeof output === 'string') {
return fs.promises
.mkdir(path.dirname(output as string), { recursive: true })
.then(() => fs.promises.writeFile(output as string, css));
} else if (typeof output === 'function') {
(output as RollupPluginSassOutputFn)(css, styles);
return Promise.resolve();
} else if (!insert && outputOptions.file && output === true) {
await fs.promises.mkdir(path.dirname(output), { recursive: true });
await fs.promises.writeFile(output, css);

return;
}

if (typeof output === 'function') {
output(css, styles);
return;
}

if (!insert && outputOptions.file && output === true) {
let dest = outputOptions.file;

if (dest.endsWith('.js') || dest.endsWith('.ts')) {
dest = dest.slice(0, -3);
}
dest = `${dest}.css`;
return fs.promises
.mkdir(path.dirname(dest), { recursive: true })
.then(() => fs.promises.writeFile(dest, css));
}

return Promise.resolve();
await fs.promises.mkdir(path.dirname(dest), { recursive: true });
await fs.promises.writeFile(dest, css);
return;
}
},
};
};

0 comments on commit 06f0f60

Please sign in to comment.