Skip to content

Commit 35365e2

Browse files
Merge branch 'feat/watch-on-complete' of https://github.com/pixijs/assetpack into feat/watch-on-complete
2 parents 2c0a103 + 6f47d39 commit 35365e2

File tree

2 files changed

+9
-133
lines changed

2 files changed

+9
-133
lines changed

packages/assetpack/src/core/AssetPack.ts

+9-24
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { generateCacheName } from './utils/generateCacheName.js';
99
import { path } from './utils/path.js';
1010
import { promiseAllConcurrent } from './utils/promiseAllConcurrent.js';
1111

12-
import type { Asset, TransformStats } from './Asset.js';
12+
import type { Asset } from './Asset.js';
13+
import type { CachedAsset } from './AssetCache.js';
1314
import type { AssetPackConfig } from './config.js';
1415
import type { AssetPipe } from './pipes/AssetPipe.js';
1516
import type { AssetSettings } from './pipes/PipeSystem.js';
@@ -58,7 +59,8 @@ export class AssetPack
5859
const { pipes, cache, cacheLocation } = this.config;
5960

6061
AssetCache.location = cacheLocation!;
61-
let assetCache: AssetCache | undefined;
62+
let assetCacheData: Record<string, CachedAsset> | null = null;
63+
let assetCache: AssetCache | null = null;
6264

6365
// if there is no cache, lets just go ahead and remove the output folder
6466
// and the cached info folder
@@ -77,7 +79,9 @@ export class AssetPack
7779

7880
// read the cache data, this will be used to restore the asset graph
7981
// by the AssetWatcher
80-
if (assetCache.exists())
82+
assetCacheData = assetCache.read();
83+
84+
if (assetCacheData)
8185
{
8286
Logger.info('[AssetPack] cache found.');
8387
}
@@ -112,7 +116,7 @@ export class AssetPack
112116
// so it can be restored even if the process is terminated
113117
this._assetWatcher = new AssetWatcher({
114118
entryPath: this._entryPath,
115-
assetCache,
119+
assetCacheData,
116120
ignore: this.config.ignore,
117121
assetSettingsData: this.config.assetSettings as AssetSettings[] || [],
118122
onUpdate: async (root: Asset) =>
@@ -175,7 +179,7 @@ export class AssetPack
175179
* @param onComplete - optional callback that will be called after each time asset pack has finished transforming the assets
176180
* @returns a promise that will resolve when the first time asset pack has finished transforming the assets
177181
*/
178-
public watch(onComplete: (root: Asset) => void)
182+
public watch(onComplete?: (root: Asset) => void)
179183
{
180184
this._onWatchTransformComplete = onComplete;
181185

@@ -194,11 +198,6 @@ export class AssetPack
194198
return this._assetWatcher.stop();
195199
}
196200

197-
public get rootAsset()
198-
{
199-
return this._assetWatcher.root;
200-
}
201-
202201
private async _transform(asset: Asset)
203202
{
204203
await this._pipeSystem.start(asset);
@@ -214,25 +213,11 @@ export class AssetPack
214213
{
215214
if (asset.skip) return;
216215

217-
const stats = asset.stats = {
218-
date: Date.now(),
219-
duration: 0,
220-
success: true,
221-
} as TransformStats;
222-
223-
const now = performance.now();
224-
225216
await this._pipeSystem.transform(asset).catch((e) =>
226217
{
227-
stats.success = false;
228-
stats.error = e.message;
229-
230218
// eslint-disable-next-line max-len
231219
Logger.error(`[AssetPack] Transform failed:\ntransform: ${e.name}\nasset:${asset.path}\nerror:${e.message}`);
232220
});
233-
234-
stats.duration = performance.now() - now;
235-
236221
index++;
237222

238223
const percent = Math.round((index / assetsToTransform.length) * 100);

packages/assetpack/test/core/Assetpack.test.ts

-109
Original file line numberDiff line numberDiff line change
@@ -352,115 +352,6 @@ describe('Core', () =>
352352
expect(rootAsset.settings).toBeUndefined();
353353
});
354354

355-
it('should generate correct stats', async () =>
356-
{
357-
const testName = 'core-stats';
358-
const inputDir = `${getInputDir(pkg, testName)}/`;
359-
const outputDir = getOutputDir(pkg, testName);
360-
361-
fs.removeSync(inputDir);
362-
363-
createFolder(
364-
pkg,
365-
{
366-
name: testName,
367-
files: [{
368-
name: 'json.json',
369-
content: assetPath('json/json.json'),
370-
}],
371-
folders: [],
372-
});
373-
374-
const assetpack = new AssetPack({
375-
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
376-
output: outputDir,
377-
cache: false
378-
});
379-
380-
await assetpack.run();
381-
382-
const stats = assetpack.rootAsset.children[0].stats!;
383-
384-
expect(stats.date).toBeGreaterThan(0);
385-
expect(stats.duration).toBeGreaterThan(0);
386-
expect(stats.success).toBe(true);
387-
});
388-
389-
it('should generate correct stats when cacheing', async () =>
390-
{
391-
const testName = 'core-stats-cache';
392-
const inputDir = `${getInputDir(pkg, testName)}/`;
393-
const outputDir = getOutputDir(pkg, testName);
394-
395-
fs.removeSync(inputDir);
396-
397-
createFolder(
398-
pkg,
399-
{
400-
name: testName,
401-
files: [{
402-
name: 'json.json',
403-
content: assetPath('json/json.json'),
404-
}],
405-
folders: [],
406-
});
407-
408-
const testFile = join(inputDir, 'json.json');
409-
410-
const assetpack = new AssetPack({
411-
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
412-
output: outputDir,
413-
cache: true
414-
});
415-
416-
fs.writeJSONSync(testFile, { nice: `old value!` });
417-
418-
await assetpack.run();
419-
420-
const stats = assetpack.rootAsset.children[0].stats!;
421-
422-
expect(stats.date).toBeGreaterThan(0);
423-
expect(stats.duration).toBeGreaterThan(0);
424-
expect(stats.success).toBe(true);
425-
426-
//
427-
428-
const date = stats.date;
429-
430-
await assetpack.run();
431-
432-
// // should maintain the same stats..
433-
const stats2 = assetpack.rootAsset.children[0].stats!;
434-
435-
expect(stats2.date).toBe(date);
436-
expect(stats2.duration).toBe(stats.duration);
437-
expect(stats2.success).toBe(true);
438-
439-
fs.writeJSONSync(testFile, { nice: 'new value!' });
440-
441-
await assetpack.run();
442-
443-
const stats3 = assetpack.rootAsset.children[0].stats!;
444-
445-
expect(stats3.date).toBeGreaterThan(stats.date);
446-
expect(stats3.success).toBe(true);
447-
});
448-
449-
it('should return the root asset', () =>
450-
{
451-
const testName = 'root-asset';
452-
const inputDir = getInputDir(pkg, testName);
453-
const outputDir = getOutputDir(pkg, testName);
454-
455-
const assetpack = new AssetPack({
456-
entry: inputDir, cacheLocation: getCacheDir(pkg, testName),
457-
output: outputDir,
458-
cache: false
459-
});
460-
461-
expect(assetpack.rootAsset).toBeDefined();
462-
});
463-
464355
it('should call onComplete when the asset pack run is complete when watching', async () =>
465356
{
466357
const testName = 'watch-onComplete';

0 commit comments

Comments
 (0)