Skip to content

Commit 1e785a1

Browse files
fix: use webpack fs (#105)
1 parent 01b3812 commit 1e785a1

File tree

2 files changed

+9
-12
lines changed

2 files changed

+9
-12
lines changed

src/index.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
MIT License http://www.opensource.org/licenses/mit-license.php
33
Author Tobias Koppers @sokra
44
*/
5-
import fs from 'fs';
65
import path from 'path';
76

87
import { promisify } from 'util';
@@ -42,8 +41,9 @@ export default async function loader(input, inputMap) {
4241
return;
4342
}
4443

45-
const { context, resolve, addDependency, emitWarning } = this;
44+
const { fs, context, resolve, addDependency, emitWarning } = this;
4645
const resolver = promisify(resolve);
46+
const reader = promisify(fs.readFile).bind(fs);
4747

4848
if (url.toLowerCase().startsWith('data:')) {
4949
const dataURL = parseDataURL(url);
@@ -102,9 +102,9 @@ export default async function loader(input, inputMap) {
102102
}
103103

104104
urlResolved = urlResolved.toString();
105+
105106
addDependency(urlResolved);
106107

107-
const reader = promisify(fs.readFile);
108108
const content = await reader(urlResolved);
109109
let map;
110110

@@ -146,7 +146,7 @@ export default async function loader(input, inputMap) {
146146
if (path.isAbsolute(fullPath)) {
147147
return originalData
148148
? { source: fullPath, content: originalData }
149-
: readFile(fullPath, 'utf-8', emitWarning);
149+
: readFile(fullPath, emitWarning, reader);
150150
}
151151

152152
let fullPathResolved;
@@ -172,7 +172,7 @@ export default async function loader(input, inputMap) {
172172
source: fullPathResolved,
173173
content: originalData,
174174
}
175-
: readFile(fullPathResolved, 'utf-8', emitWarning);
175+
: readFile(fullPathResolved, emitWarning, reader);
176176
})
177177
);
178178
} catch (error) {

src/utils.js

+4-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,5 @@
1-
import fs from 'fs';
21
import path from 'path';
32

4-
import { promisify } from 'util';
5-
63
import urlUtils from 'url';
74

85
import sourceMap from 'source-map';
@@ -72,18 +69,18 @@ async function flattenSourceMap(map) {
7269
return generatedMap.toJSON();
7370
}
7471

75-
async function readFile(fullPath, charset, emitWarning) {
76-
const reader = promisify(fs.readFile);
72+
async function readFile(fullPath, emitWarning, reader) {
7773
let content;
7874

7975
try {
80-
content = await reader(fullPath, charset);
81-
return { source: fullPath, content };
76+
content = await reader(fullPath);
8277
} catch (readFileError) {
8378
emitWarning(`Cannot open source file '${fullPath}': ${readFileError}`);
8479

8580
return { source: null, content: null };
8681
}
82+
83+
return { source: fullPath, content: content.toString() };
8784
}
8885

8986
function getContentFromSourcesContent(consumer, source) {

0 commit comments

Comments
 (0)