|
| 1 | +import _ from 'underscore'; |
| 2 | + |
| 3 | +import applyTransformers from './apply-transformers.js'; |
| 4 | +import walk from './walk.js'; |
| 5 | + |
| 6 | +const { Buffer } = globalThis; |
| 7 | + |
| 8 | +const getSharedParent = (a, b) => { |
| 9 | + while (a.depth > b.depth) a = a.parent; |
| 10 | + while (b.depth > a.depth) b = b.parent; |
| 11 | + while (a !== b) [a, b] = [a.parent, b.parent]; |
| 12 | + return a; |
| 13 | +}; |
| 14 | + |
| 15 | +const resolve = async ({ env, path }, parent, depth = 0, cache = new Map()) => { |
| 16 | + if (cache.has(path)) { |
| 17 | + const build = cache.get(path); |
| 18 | + if (build.parent) { |
| 19 | + parent = getSharedParent(build.parent, parent); |
| 20 | + if (build.parent !== parent) { |
| 21 | + build.parent.builds.delete(build); |
| 22 | + parent.builds.add(build); |
| 23 | + build.parent = parent; |
| 24 | + build.depth = build.parent.depth + 1; |
| 25 | + } |
| 26 | + } |
| 27 | + return build; |
| 28 | + } |
| 29 | + |
| 30 | + const build = { builds: new Set(), depth, parent, path }; |
| 31 | + parent?.builds.add(build); |
| 32 | + cache.set(path, build); |
| 33 | + |
| 34 | + const files = await walk({ env, path }); |
| 35 | + await Promise.all( |
| 36 | + files.flatMap(({ builds }) => |
| 37 | + builds.map(path => resolve({ env, path }, build, depth + 1, cache)) |
| 38 | + ) |
| 39 | + ); |
| 40 | + build.files = _.indexBy(files, 'path'); |
| 41 | + return build; |
| 42 | +}; |
| 43 | + |
| 44 | +const getAllFiles = build => [ |
| 45 | + ...Object.values(build.files).map(file => ({ build, file })), |
| 46 | + ...[...build.builds].flatMap(getAllFiles) |
| 47 | +]; |
| 48 | + |
| 49 | +const prune = ({ builds, files }) => { |
| 50 | + builds.forEach(prune); |
| 51 | + |
| 52 | + const seenFiles = {}; |
| 53 | + for (const build of builds) { |
| 54 | + for (const { build: fileBuild, file } of getAllFiles(build)) { |
| 55 | + const { path } = file; |
| 56 | + if (files[path]) delete fileBuild.files[path]; |
| 57 | + else if (seenFiles[path] && seenFiles[path].path !== fileBuild.path) { |
| 58 | + files[path] = file; |
| 59 | + delete fileBuild.files[path]; |
| 60 | + delete seenFiles[path].files[path]; |
| 61 | + delete seenFiles[path]; |
| 62 | + } else seenFiles[path] = fileBuild; |
| 63 | + } |
| 64 | + } |
| 65 | +}; |
| 66 | + |
| 67 | +const getBuffers = async ({ files, maxChunkSize, path, transformers }) => { |
| 68 | + let size = 0; |
| 69 | + let chunks = []; |
| 70 | + const buffers = []; |
| 71 | + for (const path in files) { |
| 72 | + const { buffer } = files[path]; |
| 73 | + if (size && maxChunkSize && size + buffer.length > maxChunkSize) { |
| 74 | + buffers.push(Buffer.concat(chunks)); |
| 75 | + size = 0; |
| 76 | + chunks = []; |
| 77 | + } |
| 78 | + size += buffer.length; |
| 79 | + chunks.push(buffer); |
| 80 | + } |
| 81 | + if (chunks.length) buffers.push(Buffer.concat(chunks)); |
| 82 | + |
| 83 | + return { |
| 84 | + path, |
| 85 | + buffers: await Promise.all( |
| 86 | + buffers.map( |
| 87 | + async buffer => |
| 88 | + ( |
| 89 | + await applyTransformers({ |
| 90 | + file: { buffer, builds: [], links: [], path, requires: [path] }, |
| 91 | + transformers |
| 92 | + }) |
| 93 | + ).buffer |
| 94 | + ) |
| 95 | + ) |
| 96 | + }; |
| 97 | +}; |
| 98 | + |
| 99 | +const flattenBuilds = build => [ |
| 100 | + build, |
| 101 | + ...[...build.builds].flatMap(flattenBuilds) |
| 102 | +]; |
| 103 | + |
| 104 | +export default async ({ env, maxChunkSize, path, transformers }) => { |
| 105 | + const build = await resolve({ env, path }); |
| 106 | + prune(build); |
| 107 | + return await Promise.all( |
| 108 | + flattenBuilds(build).map(({ path, files }) => |
| 109 | + getBuffers({ files, maxChunkSize, path, transformers }) |
| 110 | + ) |
| 111 | + ); |
| 112 | +}; |
0 commit comments