Skip to content

Commit 81c2c5d

Browse files
refactor: consistent write to fs (#371)
1 parent 1762cb3 commit 81c2c5d

File tree

1 file changed

+23
-13
lines changed

1 file changed

+23
-13
lines changed

lib/fs.js

Lines changed: 23 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -24,26 +24,36 @@ module.exports = {
2424
}
2525

2626
for (const assetPath of Object.keys(assets)) {
27-
const asset = assets[assetPath];
28-
const source = asset.source();
29-
const [assetPathClean] = assetPath.split('?');
30-
const isAbsolute = path.isAbsolute(assetPathClean);
31-
const writePath = isAbsolute ? assetPathClean : path.join(outputPath, assetPathClean);
32-
const relativePath = path.relative(process.cwd(), writePath);
33-
const allowWrite = filter && typeof filter === 'function' ? filter(writePath) : true;
27+
let targetFile = assetPath;
28+
29+
const queryStringIdx = targetFile.indexOf('?');
30+
31+
if (queryStringIdx >= 0) {
32+
targetFile = targetFile.substr(0, queryStringIdx);
33+
}
34+
35+
const targetPath = path.isAbsolute(targetFile) ? targetFile : path.join(outputPath, targetFile);
36+
const allowWrite = filter && typeof filter === 'function' ? filter(targetPath) : true;
3437

3538
if (allowWrite) {
36-
let output = source;
39+
const asset = assets[assetPath];
40+
let content = asset.source();
3741

38-
mkdirp.sync(path.dirname(writePath));
42+
if (!Buffer.isBuffer(content)) {
43+
// TODO need remove in next major release
44+
if (Array.isArray(content)) {
45+
content = content.join('\n');
46+
}
3947

40-
if (Array.isArray(source)) {
41-
output = source.join('\n');
48+
content = Buffer.from(content, 'utf8');
4249
}
4350

51+
mkdirp.sync(path.dirname(targetPath));
52+
4453
try {
45-
fs.writeFileSync(writePath, output, 'utf-8');
46-
log.debug(colors.cyan(`Asset written to disk: ${relativePath}`));
54+
fs.writeFileSync(targetPath, content, 'utf-8');
55+
56+
log.debug(colors.cyan(`Asset written to disk: ${path.relative(process.cwd(), targetPath)}`));
4757
} catch (e) {
4858
log.error(`Unable to write asset to disk:\n${e}`);
4959
}

0 commit comments

Comments
 (0)