don't use readFileSync

This commit is contained in:
Erin 2023-08-14 14:33:14 -04:00
parent c22c6db195
commit e1a4f59516

View file

@ -1,6 +1,6 @@
/* eslint-env node */
import {readFileSync} from 'node:fs';
import {readFile} from 'node:fs/promises';
import {basename, dirname, extname, join, relative, resolve} from 'node:path';
import {type Plugin, type RollupOptions} from 'rollup';
@ -21,7 +21,7 @@ import {getAssetEntrypoints, getScriptEntrypoints} from './entrypoints';
* Javascript entry point
* @param options.sourcemap Controls inclusion of sourcemaps in the output
*/
export function buildConfig ({
export async function buildConfig ({
manifest: manifestPathRelative,
outDir,
plugins = [],
@ -31,14 +31,14 @@ export function buildConfig ({
outDir: string;
plugins: Plugin[];
sourcemap: boolean | 'inline' | 'hidden';
}): RollupOptions[] {
}): Promise<RollupOptions[]> {
const manifestPath = resolve(process.cwd(), manifestPathRelative);
const manifestDirname = dirname(manifestPath);
// Load the manifest
let manifestContent: chrome.runtime.Manifest;
try {
manifestContent = JSON.parse(readFileSync(manifestPath, {encoding: 'utf-8'}));
manifestContent = JSON.parse(await readFile(manifestPath, {encoding: 'utf-8'}));
} catch (error) {
throw new Error('Failed to load manifest');
}
@ -106,8 +106,8 @@ export function buildConfig ({
{
name: '_manifest-asset-processing',
// emit other assets
buildStart () {
assets.forEach(({path, replacePath}) => {
async buildStart () {
await Promise.all(assets.map(async ({path, replacePath}) => {
// Figure out where the asset will live in output
const outPath = ensureUniquePath(path);
@ -118,9 +118,9 @@ export function buildConfig ({
this.emitFile({
type: 'asset',
fileName: outPath,
source: readFileSync(join(manifestDirname, path)),
});
source: await readFile(join(manifestDirname, path)),
});
}));
},
// hacks to make sure the manifest is emitted as bare JSON