From b02296a619967b0c4bd8e1bdfa5cae3b367b726d Mon Sep 17 00:00:00 2001 From: Ulf Nilsson Date: Tue, 2 Jul 2019 07:10:25 +0000 Subject: [PATCH] Use async readFile in getRollupDefaultPlugin (#2965) --- browser/fs.ts | 2 +- src/utils/defaultPlugin.ts | 4 ++-- src/utils/fs.ts | 5 +++++ 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/browser/fs.ts b/browser/fs.ts index 91f9356c180..5cc22ebd873 100644 --- a/browser/fs.ts +++ b/browser/fs.ts @@ -4,6 +4,6 @@ const nope = (method: string) => (..._args: any[]): any => { export const lstatSync = nope('lstatSync'); export const readdirSync = nope('readdirSync'); -export const readFileSync = nope('readFileSync'); +export const readFile = nope('readFile'); export const realpathSync = nope('realpathSync'); export const writeFile = nope('writeFile'); diff --git a/src/utils/defaultPlugin.ts b/src/utils/defaultPlugin.ts index 939da211047..4b27c4c1546 100644 --- a/src/utils/defaultPlugin.ts +++ b/src/utils/defaultPlugin.ts @@ -1,6 +1,6 @@ import { Plugin, ResolveIdHook } from '../rollup/types'; import { error } from './error'; -import { lstatSync, readdirSync, readFileSync, realpathSync } from './fs'; +import { lstatSync, readdirSync, readFile, realpathSync } from './fs'; import { basename, dirname, isAbsolute, resolve } from './path'; export function getRollupDefaultPlugin(preserveSymlinks: boolean): Plugin { @@ -8,7 +8,7 @@ export function getRollupDefaultPlugin(preserveSymlinks: boolean): Plugin { name: 'Rollup Core', resolveId: createResolveId(preserveSymlinks) as ResolveIdHook, load(id) { - return readFileSync(id, 'utf-8'); + return readFile(id); }, resolveFileUrl({ relativePath, format }) { return relativeUrlMechanisms[format](relativePath); diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 5afe9465d88..f7550e5cad0 100644 --- a/src/utils/fs.ts +++ b/src/utils/fs.ts @@ -3,6 +3,11 @@ import { dirname } from './path'; export * from 'fs'; +export const readFile = (file: string) => + new Promise((fulfil, reject) => + fs.readFile(file, 'utf-8', (err, contents) => (err ? reject(err) : fulfil(contents))) + ); + function mkdirpath(path: string) { const dir = dirname(path); try {