diff --git a/src/index.js b/src/index.js index 6a167b1..64a5211 100644 --- a/src/index.js +++ b/src/index.js @@ -2,7 +2,6 @@ MIT License http://www.opensource.org/licenses/mit-license.php Author Tobias Koppers @sokra */ -import fs from 'fs'; import path from 'path'; import { promisify } from 'util'; @@ -42,8 +41,9 @@ export default async function loader(input, inputMap) { return; } - const { context, resolve, addDependency, emitWarning } = this; + const { fs, context, resolve, addDependency, emitWarning } = this; const resolver = promisify(resolve); + const reader = promisify(fs.readFile).bind(fs); if (url.toLowerCase().startsWith('data:')) { const dataURL = parseDataURL(url); @@ -102,9 +102,9 @@ export default async function loader(input, inputMap) { } urlResolved = urlResolved.toString(); + addDependency(urlResolved); - const reader = promisify(fs.readFile); const content = await reader(urlResolved); let map; @@ -146,7 +146,7 @@ export default async function loader(input, inputMap) { if (path.isAbsolute(fullPath)) { return originalData ? { source: fullPath, content: originalData } - : readFile(fullPath, 'utf-8', emitWarning); + : readFile(fullPath, emitWarning, reader); } let fullPathResolved; @@ -172,7 +172,7 @@ export default async function loader(input, inputMap) { source: fullPathResolved, content: originalData, } - : readFile(fullPathResolved, 'utf-8', emitWarning); + : readFile(fullPathResolved, emitWarning, reader); }) ); } catch (error) { diff --git a/src/utils.js b/src/utils.js index d6da206..283ef62 100644 --- a/src/utils.js +++ b/src/utils.js @@ -1,8 +1,5 @@ -import fs from 'fs'; import path from 'path'; -import { promisify } from 'util'; - import urlUtils from 'url'; import sourceMap from 'source-map'; @@ -72,18 +69,18 @@ async function flattenSourceMap(map) { return generatedMap.toJSON(); } -async function readFile(fullPath, charset, emitWarning) { - const reader = promisify(fs.readFile); +async function readFile(fullPath, emitWarning, reader) { let content; try { - content = await reader(fullPath, charset); - return { source: fullPath, content }; + content = await reader(fullPath); } catch (readFileError) { emitWarning(`Cannot open source file '${fullPath}': ${readFileError}`); return { source: null, content: null }; } + + return { source: fullPath, content: content.toString() }; } function getContentFromSourcesContent(consumer, source) {