diff --git a/browser/fs.ts b/browser/fs.ts index 4f6ce519593..61c07e2dee8 100644 --- a/browser/fs.ts +++ b/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'); diff --git a/src/rollup/rollup.ts b/src/rollup/rollup.ts index fc9eacf5028..037986872e4 100644 --- a/src/rollup/rollup.ts +++ b/src/rollup/rollup.ts @@ -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'; @@ -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 { 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 | undefined; let source: string | Uint8Array; if (outputFile.type === 'asset') { @@ -276,7 +280,7 @@ 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`; @@ -284,7 +288,7 @@ function writeOutputFile( } } - return Promise.all([writeFile(fileName, source), writeSourceMapPromise]); + return Promise.all([fs.writeFile(fileName, source), writeSourceMapPromise]); } /** diff --git a/src/utils/fs.ts b/src/utils/fs.ts index 42b3535756a..b301bda0b0a 100644 --- a/src/utils/fs.ts +++ b/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 { - await fs.mkdir(dirname(dest), { recursive: true }); - await fs.writeFile(dest, data); -} diff --git a/src/utils/resolveId.ts b/src/utils/resolveId.ts index 9654d8f85ab..02fc9d496bb 100644 --- a/src/utils/resolveId.ts +++ b/src/utils/resolveId.ts @@ -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 { @@ -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 diff --git a/test/function/samples/max-parallel-file-reads-default/_config.js b/test/function/samples/max-parallel-file-reads-default/_config.js index 6284f3cb199..950df6ddb82 100644 --- a/test/function/samples/max-parallel-file-reads-default/_config.js +++ b/test/function/samples/max-parallel-file-reads-default/_config.js @@ -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); @@ -17,7 +17,7 @@ module.exports = { }; }, after() { - promises.readFile = fsReadFile; + fs.readFile = fsReadFile; assert.strictEqual(maxReads, 5, 'Wrong number of parallel file reads: ' + maxReads); } }; diff --git a/test/function/samples/max-parallel-file-reads-error/_config.js b/test/function/samples/max-parallel-file-reads-error/_config.js index 08e4c8d3e85..9aeadc4f067 100644 --- a/test/function/samples/max-parallel-file-reads-error/_config.js +++ b/test/function/samples/max-parallel-file-reads-error/_config.js @@ -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', @@ -13,7 +13,7 @@ module.exports = { }) }, before() { - promises.readFile = (path, options) => { + fs.readFile = (path, options) => { if (path.endsWith('dep.js')) { throw new Error('broken'); } @@ -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`, diff --git a/test/function/samples/max-parallel-file-reads-infinity/_config.js b/test/function/samples/max-parallel-file-reads-infinity/_config.js index 82cad65d3ca..590ee496622 100644 --- a/test/function/samples/max-parallel-file-reads-infinity/_config.js +++ b/test/function/samples/max-parallel-file-reads-infinity/_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; @@ -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); @@ -20,7 +20,7 @@ module.exports = { }; }, after() { - promises.readFile = fsReadFile; + fs.readFile = fsReadFile; assert.strictEqual(maxReads, 5, 'Wrong number of parallel file reads: ' + maxReads); } }; diff --git a/test/function/samples/max-parallel-file-reads-set/_config.js b/test/function/samples/max-parallel-file-reads-set/_config.js index 6b247837538..eaf474541ac 100644 --- a/test/function/samples/max-parallel-file-reads-set/_config.js +++ b/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; @@ -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); @@ -20,7 +20,7 @@ module.exports = { }; }, after() { - promises.readFile = fsReadFile; + fs.readFile = fsReadFile; assert.strictEqual(maxReads, 3, 'Wrong number of parallel file reads: ' + maxReads); } }; diff --git a/test/function/samples/max-parallel-file-reads-with-plugin/_config.js b/test/function/samples/max-parallel-file-reads-with-plugin/_config.js index b4df04b0a34..aeffa9637a7 100644 --- a/test/function/samples/max-parallel-file-reads-with-plugin/_config.js +++ b/test/function/samples/max-parallel-file-reads-with-plugin/_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; @@ -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); @@ -27,7 +27,7 @@ module.exports = { }; }, after() { - promises.readFile = fsReadFile; + fs.readFile = fsReadFile; assert.strictEqual(maxReads, 3, 'Wrong number of parallel file reads: ' + maxReads); } };