Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: fs.promises, move mkdir to writeoutputfile, Part 3 #4376

Merged
merged 5 commits into from Feb 2, 2022
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions browser/fs.ts
@@ -1,7 +1,7 @@
import { throwNoFileSystem } from './error';

export const promises = {
readFile: throwNoFileSystem('fs.readFile')
mkdir: throwNoFileSystem('fs.mkdir'),
readFile: throwNoFileSystem('fs.readFile'),
writeFile: throwNoFileSystem('fs.writeFile')
};

export const writeFile = throwNoFileSystem('fs.writeFile');
12 changes: 8 additions & 4 deletions src/rollup/rollup.ts
Expand Up @@ -4,7 +4,7 @@ import Graph from '../Graph';
import type { PluginDriver } from '../utils/PluginDriver';
import { ensureArray } from '../utils/ensureArray';
import { errAlreadyClosed, errCannotEmitFromOptionsHook, error } from '../utils/error';
import { writeFile } from '../utils/fs';
import { promises as fs } from '../utils/fs';
import { normalizeInputOptions } from '../utils/options/normalizeInputOptions';
import { normalizeOutputOptions } from '../utils/options/normalizeOutputOptions';
import type { GenericConfigObject } from '../utils/options/options';
Expand Down Expand Up @@ -259,11 +259,15 @@ function getSortingFileType(file: OutputAsset | OutputChunk): SortingFileType {
return SortingFileType.SECONDARY_CHUNK;
}

function writeOutputFile(
async function writeOutputFile(
outputFile: OutputAsset | OutputChunk,
outputOptions: NormalizedOutputOptions
): Promise<unknown> {
const fileName = resolve(outputOptions.dir || dirname(outputOptions.file!), outputFile.fileName);

// 'recursive: true' does not throw if the folder structure, or parts of it, already exist
await fs.mkdir(dirname(fileName), { recursive: true });

let writeSourceMapPromise: Promise<void> | undefined;
let source: string | Uint8Array;
if (outputFile.type === 'asset') {
Expand All @@ -276,15 +280,15 @@ function writeOutputFile(
url = outputFile.map.toUrl();
} else {
url = `${basename(outputFile.fileName)}.map`;
writeSourceMapPromise = writeFile(`${fileName}.map`, outputFile.map.toString());
writeSourceMapPromise = fs.writeFile(`${fileName}.map`, outputFile.map.toString());
}
if (outputOptions.sourcemap !== 'hidden') {
source += `//# ${SOURCEMAPPING_URL}=${url}\n`;
}
}
}

return Promise.all([writeFile(fileName, source), writeSourceMapPromise]);
return Promise.all([fs.writeFile(fileName, source), writeSourceMapPromise]);
}

/**
Expand Down
8 changes: 0 additions & 8 deletions src/utils/fs.ts
@@ -1,9 +1 @@
import { promises as fs } from 'fs';
import { dirname } from './path';

export * from 'fs';

export async function writeFile(dest: string, data: string | Uint8Array): Promise<void> {
await fs.mkdir(dirname(dest), { recursive: true });
await fs.writeFile(dest, data);
}
15 changes: 7 additions & 8 deletions src/utils/resolveId.ts
Expand Up @@ -45,13 +45,12 @@ export async function resolveId(
);
}

function addJsExtensionIfNecessary(file: string, preserveSymlinks: boolean) {
let found = findFile(file, preserveSymlinks);
if (found) return found;
found = findFile(file + '.mjs', preserveSymlinks);
if (found) return found;
found = findFile(file + '.js', preserveSymlinks);
return found;
function addJsExtensionIfNecessary(file: string, preserveSymlinks: boolean): string | undefined {
return (
findFile(file, preserveSymlinks) ??
findFile(file + '.mjs', preserveSymlinks) ??
findFile(file + '.js', preserveSymlinks)
);
}

function findFile(file: string, preserveSymlinks: boolean): string | undefined {
Expand All @@ -64,7 +63,7 @@ function findFile(file: string, preserveSymlinks: boolean): string | undefined {
const name = basename(file);
const files = readdirSync(dirname(file));

if (files.indexOf(name) !== -1) return file;
if (files.includes(name)) return file;
}
} catch {
// suppress
Expand Down
@@ -1,14 +1,14 @@
const assert = require('assert');
const { promises } = require('fs');
const { promises: fs } = require('fs');

const fsReadFile = promises.readFile;
const fsReadFile = fs.readFile;
let currentReads = 0;
let maxReads = 0;

module.exports = {
description: 'maxParallelFileReads not set',
before() {
promises.readFile = async (path, options) => {
fs.readFile = async (path, options) => {
currentReads++;
maxReads = Math.max(maxReads, currentReads);
const content = await fsReadFile(path, options);
Expand All @@ -17,7 +17,7 @@ module.exports = {
};
},
after() {
promises.readFile = fsReadFile;
fs.readFile = fsReadFile;
assert.strictEqual(maxReads, 5, 'Wrong number of parallel file reads: ' + maxReads);
}
};
@@ -1,8 +1,8 @@
const { promises } = require('fs');
const { promises: fs } = require('fs');
const { join } = require('path');
const { loader } = require('../../../utils.js');

const fsReadFile = promises.readFile;
const fsReadFile = fs.readFile;

module.exports = {
description: 'maxParallelFileReads: fileRead error is forwarded',
Expand All @@ -13,7 +13,7 @@ module.exports = {
})
},
before() {
promises.readFile = (path, options) => {
fs.readFile = (path, options) => {
if (path.endsWith('dep.js')) {
throw new Error('broken');
}
Expand All @@ -22,7 +22,7 @@ module.exports = {
};
},
after() {
promises.readFile = fsReadFile;
fs.readFile = fsReadFile;
},
error: {
message: `Could not load ${join(__dirname, 'dep.js')} (imported by main): broken`,
Expand Down
@@ -1,7 +1,7 @@
const assert = require('assert');
const { promises } = require('fs');
const { promises: fs } = require('fs');

const fsReadFile = promises.readFile;
const fsReadFile = fs.readFile;
let currentReads = 0;
let maxReads = 0;

Expand All @@ -11,7 +11,7 @@ module.exports = {
maxParallelFileReads: 0
},
before() {
promises.readFile = async (path, options) => {
fs.readFile = async (path, options) => {
currentReads++;
maxReads = Math.max(maxReads, currentReads);
const content = await fsReadFile(path, options);
Expand All @@ -20,7 +20,7 @@ module.exports = {
};
},
after() {
promises.readFile = fsReadFile;
fs.readFile = fsReadFile;
assert.strictEqual(maxReads, 5, 'Wrong number of parallel file reads: ' + maxReads);
}
};
8 changes: 4 additions & 4 deletions test/function/samples/max-parallel-file-reads-set/_config.js
@@ -1,7 +1,7 @@
const assert = require('assert');
const { promises } = require('fs');
const { promises: fs } = require('fs');

const fsReadFile = promises.readFile;
const fsReadFile = fs.readFile;
let currentReads = 0;
let maxReads = 0;

Expand All @@ -11,7 +11,7 @@ module.exports = {
maxParallelFileReads: 3
},
before() {
promises.readFile = async (path, options) => {
fs.readFile = async (path, options) => {
currentReads++;
maxReads = Math.max(maxReads, currentReads);
const content = await fsReadFile(path, options);
Expand All @@ -20,7 +20,7 @@ module.exports = {
};
},
after() {
promises.readFile = fsReadFile;
fs.readFile = fsReadFile;
assert.strictEqual(maxReads, 3, 'Wrong number of parallel file reads: ' + maxReads);
}
};
@@ -1,7 +1,7 @@
const assert = require('assert');
const { promises } = require('fs');
const { promises: fs } = require('fs');

const fsReadFile = promises.readFile;
const fsReadFile = fs.readFile;
let currentReads = 0;
let maxReads = 0;

Expand All @@ -12,13 +12,13 @@ module.exports = {
plugins: [
{
load(id) {
return promises.readFile(id, 'utf-8');
return fs.readFile(id, 'utf-8');
}
}
]
},
before() {
promises.readFile = async (path, options) => {
fs.readFile = async (path, options) => {
currentReads++;
maxReads = Math.max(maxReads, currentReads);
const content = await fsReadFile(path, options);
Expand All @@ -27,7 +27,7 @@ module.exports = {
};
},
after() {
promises.readFile = fsReadFile;
fs.readFile = fsReadFile;
assert.strictEqual(maxReads, 3, 'Wrong number of parallel file reads: ' + maxReads);
}
};