|  | 
|  | 1 | +import fs from 'fs-extra'; | 
|  | 2 | +import { findAssets, path } from '../core/index.js'; | 
|  | 3 | +import { type PixiManifest, type PixiManifestOptions } from '../manifest/pixiManifest.js'; | 
|  | 4 | +import { getFileSizeInKB } from '../manifest/utils.js'; | 
|  | 5 | + | 
|  | 6 | +import type { Asset, AssetPipe } from '../core/index.js'; | 
|  | 7 | + | 
|  | 8 | +export type TexturePackerManifestOptions = Pick<PixiManifestOptions, 'output' | 'includeFileSizes'>; | 
|  | 9 | + | 
|  | 10 | +/** | 
|  | 11 | + * This pipe will modify the manifest generated by 'pixiManifest'. It will replace the progressSize | 
|  | 12 | + * of each src to ensure it includes the size of the texture and the json combined | 
|  | 13 | + * | 
|  | 14 | + * Once done, it rewrites the manifest. | 
|  | 15 | + * | 
|  | 16 | + * This should be added after the `pixiManifest` pipe. | 
|  | 17 | + * | 
|  | 18 | + * ensure that the same output path is passed to the pipe as the `pixiManifest` pipe. Otherwise | 
|  | 19 | + * the manifest will not be found. | 
|  | 20 | + * | 
|  | 21 | + * As this pipe needs to know about all the textures in the texture files most of the work is done | 
|  | 22 | + * in the finish method. | 
|  | 23 | + * | 
|  | 24 | + * Kind of like applying a patch at the end of the manifest process. | 
|  | 25 | + * | 
|  | 26 | + * @param _options | 
|  | 27 | + * @returns | 
|  | 28 | + */ | 
|  | 29 | +export function texturePackerManifestMod( | 
|  | 30 | +    _options: TexturePackerManifestOptions = {}, | 
|  | 31 | +): AssetPipe<TexturePackerManifestOptions> { | 
|  | 32 | +    const defaultOptions: TexturePackerManifestOptions = { | 
|  | 33 | +        output: 'manifest.json', | 
|  | 34 | +        includeFileSizes: false, | 
|  | 35 | +        ..._options, | 
|  | 36 | +    }; | 
|  | 37 | + | 
|  | 38 | +    return { | 
|  | 39 | +        folder: false, | 
|  | 40 | +        name: 'texture-packer-manifest', | 
|  | 41 | +        defaultOptions, | 
|  | 42 | + | 
|  | 43 | +        async finish(asset: Asset, options, pipeSystem) { | 
|  | 44 | +            if (options.includeFileSizes === false) { | 
|  | 45 | +                return; | 
|  | 46 | +            } | 
|  | 47 | +            const spritesheets = findAssets( | 
|  | 48 | +                (asset) => asset.extension === '.json' && asset.allMetaData.tps && asset.transformChildren.length === 0, | 
|  | 49 | +                asset, | 
|  | 50 | +                true, | 
|  | 51 | +            ); | 
|  | 52 | + | 
|  | 53 | +            const manifestLocation = options.output; | 
|  | 54 | + | 
|  | 55 | +            const newFileName = | 
|  | 56 | +                path.dirname(manifestLocation) === '.' | 
|  | 57 | +                    ? path.joinSafe(pipeSystem.outputPath, manifestLocation) | 
|  | 58 | +                    : manifestLocation; | 
|  | 59 | + | 
|  | 60 | +            const manifest = fs.readJsonSync(newFileName); | 
|  | 61 | + | 
|  | 62 | +            spritesheets.forEach((atlasAsset) => { | 
|  | 63 | +                const buffer = atlasAsset.buffer; | 
|  | 64 | +                const json = JSON.parse(buffer.toString()); | 
|  | 65 | + | 
|  | 66 | +                if (!json.meta || !json.meta.image) { | 
|  | 67 | +                    return; | 
|  | 68 | +                } | 
|  | 69 | + | 
|  | 70 | +                const textures: string[] = Array.isArray(json.meta.image) ? json.meta.image : [json.meta.image]; | 
|  | 71 | + | 
|  | 72 | +                textures.forEach((texture) => { | 
|  | 73 | +                    let size = 0; | 
|  | 74 | +                    const manifestPath = path.relative(pipeSystem.outputPath, atlasAsset.path); | 
|  | 75 | +                    const texturePath = path.resolve(atlasAsset.directory, texture); | 
|  | 76 | + | 
|  | 77 | +                    if (texturePath) { | 
|  | 78 | +                        size = getFileSizeInKB(texturePath, options.includeFileSizes === 'raw'); | 
|  | 79 | +                    } | 
|  | 80 | +                    findAndAddManifestAsset(manifest, `${manifestPath}`, size); | 
|  | 81 | +                }); | 
|  | 82 | +            }); | 
|  | 83 | + | 
|  | 84 | +            fs.writeJSONSync(newFileName, manifest, { spaces: 2 }); | 
|  | 85 | +        }, | 
|  | 86 | +    }; | 
|  | 87 | +} | 
|  | 88 | + | 
|  | 89 | +function findAndAddManifestAsset(manifest: PixiManifest, assetPath: string, totalSize: number) { | 
|  | 90 | +    for (let i = 0; i < manifest.bundles.length; i++) { | 
|  | 91 | +        const assets = manifest.bundles[i].assets; | 
|  | 92 | + | 
|  | 93 | +        const manifestAsset = assets.find((asset) => { | 
|  | 94 | +            return asset.src.find((src) => (typeof src === 'string' ? src : src.src) === assetPath); | 
|  | 95 | +        }); | 
|  | 96 | + | 
|  | 97 | +        if (manifestAsset) { | 
|  | 98 | +            const src = manifestAsset.src.find((src) => (typeof src === 'string' ? src : src.src) === assetPath); | 
|  | 99 | + | 
|  | 100 | +            if (typeof src === 'object') { | 
|  | 101 | +                src.progressSize = Number(((src.progressSize || 0) + totalSize).toFixed(2)); | 
|  | 102 | +            } else { | 
|  | 103 | +                // convert to object | 
|  | 104 | +                const index = manifestAsset.src.indexOf(src!); | 
|  | 105 | + | 
|  | 106 | +                manifestAsset.src[index] = { | 
|  | 107 | +                    src: src!, | 
|  | 108 | +                    progressSize: totalSize, | 
|  | 109 | +                }; | 
|  | 110 | +            } | 
|  | 111 | +            break; | 
|  | 112 | +        } | 
|  | 113 | +    } | 
|  | 114 | +} | 
0 commit comments