Skip to content

Commit

Permalink
scanImports() function reads all files at once, can cause EMFILE in l…
Browse files Browse the repository at this point in the history
…arger projects #1245 (#1256)

* Recovers webpack magic comments and adds to import

Webpack's magic comments must be extracted from dynamic imports before the spec is passed to `matchDynamicImportValue`. If a match is found for these types of comments, they are appended to the rewritten import.

* scanImports() function reads all files at once, can cause EMFILE in larger projects #1245

See #1245
Adds 'p-queue' dependency
Creates `getLoadedFiles` mapper function to improve type legibility

* Bumps CONCURRENT_FILE_READS to 1000 and removes onIdle call #1256
  • Loading branch information
N8-B committed Oct 10, 2020
1 parent 65b1193 commit b6df4e1
Showing 1 changed file with 9 additions and 3 deletions.
12 changes: 9 additions & 3 deletions snowpack/src/scan-imports.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ import {
readFile,
SVELTE_VUE_REGEX,
} from './util';
import PQueue from 'p-queue';

const CONCURRENT_FILE_READS = 1000;

// [@\w] - Match a word-character or @ (valid package name)
// (?!.*(:\/\/)) - Ignore if previous match was a protocol (ex: http://)
Expand Down Expand Up @@ -248,16 +251,19 @@ export async function scanImports(
}

// Scan every matched JS file for web dependency imports
const loadedFiles: (SnowpackSourceFile | null)[] = await Promise.all(
includeFiles.map(async (filePath) => {
const loadFileQueue = new PQueue({concurrency: CONCURRENT_FILE_READS});
const getLoadedFiles = async (filePath: string): Promise<SnowpackSourceFile | null> =>
loadFileQueue.add(async () => {
const {baseExt, expandedExt} = getExt(filePath);
return {
baseExt,
expandedExt,
locOnDisk: filePath,
contents: await readFile(filePath),
};
}),
});
const loadedFiles: (SnowpackSourceFile | null)[] = await Promise.all(
includeFiles.map(getLoadedFiles),
);

return scanImportsFromFiles(loadedFiles.filter(isTruthy), config);
Expand Down

1 comment on commit b6df4e1

@vercel
Copy link

@vercel vercel bot commented on b6df4e1 Oct 10, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.