Skip to content

Commit 9e23011

Browse files
committed
refactor(generateBundle): convert to async function
1 parent b481fe9 commit 9e23011

File tree

1 file changed

+19
-19
lines changed

1 file changed

+19
-19
lines changed

src/index.ts

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,7 @@ import { createFilter } from '@rollup/pluginutils';
1313
*/
1414
import type { Plugin as RollupPlugin, TransformResult } from 'rollup';
1515

16-
import type {
17-
RollupPluginSassOptions,
18-
RollupPluginSassOutputFn,
19-
RollupPluginSassState,
20-
} from './types';
16+
import type { RollupPluginSassOptions, RollupPluginSassState } from './types';
2117
import {
2218
getImporterListLegacy,
2319
getImporterListModern,
@@ -182,36 +178,40 @@ export = function plugin(
182178
}
183179
},
184180

185-
generateBundle(outputOptions, _, isWrite) {
181+
async generateBundle(outputOptions, _, isWrite) {
186182
const { styles } = pluginState;
187183
const { output, insert } = pluginOptions;
188184

189185
if (!isWrite || (!insert && (!styles.length || output === false))) {
190-
return Promise.resolve();
186+
return;
191187
}
192188

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

195191
if (typeof output === 'string') {
196-
return fs.promises
197-
.mkdir(path.dirname(output as string), { recursive: true })
198-
.then(() => fs.promises.writeFile(output as string, css));
199-
} else if (typeof output === 'function') {
200-
(output as RollupPluginSassOutputFn)(css, styles);
201-
return Promise.resolve();
202-
} else if (!insert && outputOptions.file && output === true) {
192+
await fs.promises.mkdir(path.dirname(output), { recursive: true });
193+
await fs.promises.writeFile(output, css);
194+
195+
return;
196+
}
197+
198+
if (typeof output === 'function') {
199+
output(css, styles);
200+
return;
201+
}
202+
203+
if (!insert && outputOptions.file && output === true) {
203204
let dest = outputOptions.file;
204205

205206
if (dest.endsWith('.js') || dest.endsWith('.ts')) {
206207
dest = dest.slice(0, -3);
207208
}
208209
dest = `${dest}.css`;
209-
return fs.promises
210-
.mkdir(path.dirname(dest), { recursive: true })
211-
.then(() => fs.promises.writeFile(dest, css));
212-
}
213210

214-
return Promise.resolve();
211+
await fs.promises.mkdir(path.dirname(dest), { recursive: true });
212+
await fs.promises.writeFile(dest, css);
213+
return;
214+
}
215215
},
216216
};
217217
};

0 commit comments

Comments
 (0)