Skip to content

Commit

Permalink
fix: assets not being deleted correctly (#31)
Browse files Browse the repository at this point in the history
  • Loading branch information
Zyie authored May 15, 2024
1 parent b45642b commit 5c94a6f
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/core/Asset.ts
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@ export class Asset
this.children.splice(index, 1);
asset.parent = null;
}

asset.releaseChildrenBuffers();
}

addTransformChild(asset: Asset)
Expand Down
4 changes: 2 additions & 2 deletions src/core/AssetPack.ts
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ export class AssetPack
{
if (asset.state === 'deleted')
{
deleteAsset(asset);
deleteAssetFiles(asset);
}
else
{
Expand All @@ -218,7 +218,7 @@ export class AssetPack
}
}

async function deleteAsset(asset: Asset)
export async function deleteAssetFiles(asset: Asset)
{
asset.transformChildren.forEach((child) =>
{
Expand Down
2 changes: 2 additions & 0 deletions src/core/AssetWatcher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import chokidar from 'chokidar';
import fs from 'fs-extra';
import { Asset } from './Asset.js';
import { AssetIgnore } from './AssetIgnore.js';
import { deleteAssetFiles } from './AssetPack.js';
import { Logger } from './logger/Logger.js';
import { applySettingToAsset } from './utils/applySettingToAsset.js';
import { path } from './utils/path.js';
Expand Down Expand Up @@ -222,6 +223,7 @@ export class AssetWatcher
else if (asset.state === 'normal')
{
asset.state = 'modified';
deleteAssetFiles(asset);
}

// flag all folders as modified..
Expand Down
2 changes: 2 additions & 0 deletions src/manifest/pixiManifest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,8 @@ function collectAssets(
)
{
if (asset.skip) return;
// an item may have been deleted, so we don't want to add it to the manifest!
if (asset.state === 'deleted') return;

let localBundle = bundle;

Expand Down
70 changes: 70 additions & 0 deletions test/core/Assetpack.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ import fs from 'fs-extra';
import { existsSync } from 'node:fs';
import { join } from 'path';
import { describe, expect, it } from 'vitest';
import { cacheBuster } from '../../src/cache-buster/cacheBuster.js';
import { AssetPack } from '../../src/core/AssetPack.js';
import { getHash } from '../../src/core/index.js';
import { logAssetGraph } from '../../src/core/utils/logAssetGraph.js';
import { pixiManifest } from '../../src/manifest/pixiManifest.js';
import {
assetPath,
createAssetPipe,
Expand Down Expand Up @@ -125,6 +128,73 @@ describe('Core', () =>
expect(fs.readJSONSync(join(outputDir, 'json.json'))).toStrictEqual({ nice: 'test' });
});

it('should delete previously hashed versions of an asset', { timeout: 10000 }, async () =>
{
const testName = 'watch-delete-hash';
const inputDir = `${getInputDir(pkg, testName)}/`;
const outputDir = getOutputDir(pkg, testName);

createFolder(
pkg,
{
name: testName,
files: [{
name: 'json.json',
content: assetPath('json/json.json'),
}],
folders: [],
});

const testFile = join(inputDir, 'json.json');

const assetpack = new AssetPack({
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
output: outputDir,
cache: true,
pipes: [
cacheBuster(),
pixiManifest(),
]
});

await assetpack.watch();

const origHash = getHash(join(inputDir, 'json.json'));

expect(existsSync(join(outputDir, `json-${origHash}.json`))).toBe(true);

fs.writeJSONSync(testFile, { nice: 'test' });

await new Promise((resolve) =>
{
setTimeout(resolve, 1500);
});

expect(existsSync(join(outputDir, `json-${origHash}.json`))).toBe(false);
const newHash = getHash(join(inputDir, 'json.json'));

expect(existsSync(join(outputDir, `json-${newHash}.json`))).toBe(true);

fs.removeSync(testFile);

await new Promise((resolve) =>
{
setTimeout(resolve, 1500);
});

await assetpack.stop();

expect(existsSync(join(outputDir, `json-${origHash}.json`))).toBe(false);
expect(existsSync(join(outputDir, `json-${newHash}.json`))).toBe(false);
expect(fs.readJSONSync(join(outputDir, 'manifest.json'))).toStrictEqual({
bundles: [
{
name: 'default',
assets: []
}]
});
});

it('should ignore specified files when watching', async () =>
{
const testName = 'watch-ignore';
Expand Down

0 comments on commit 5c94a6f

Please sign in to comment.