Skip to content

Commit 60ab30b

Browse files
loader-base migrated to lerna, not yet to pixi v7
1 parent 51b7602 commit 60ab30b

File tree

7 files changed

+81
-56
lines changed

7 files changed

+81
-56
lines changed

packages/base/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@
3232
"scripts": {
3333
"build": "run-p build:*",
3434
"build:rollup": "rollup -c rollup.config.mjs --silent",
35-
"build:types": "rimraf compile && tsc -p tsconfig-api.json && api-extractor run && node scripts/injectGlobalMixins",
35+
"build:types": "rimraf compile && tsc -p tsconfig-api.json && api-extractor run && node ../../scripts/injectGlobalMixins",
3636
"lint": "eslint src/**/*.ts",
3737
"lint:fix": "eslint src/**/*.ts --fix"
3838
},

packages/loader-base/package.json

+22-6
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,36 @@
22
"name": "@pixi-spine/loader-base",
33
"version": "3.1.1",
44
"description": "Pixi loader middleware base",
5-
"main": "lib/loader-base.js",
6-
"module": "lib/loader-base.es.js",
7-
"bundle": "dist/loader-base.js",
5+
"main": "lib/index.js",
6+
"module": "lib/index.mjs",
87
"types": "./index.d.ts",
9-
"namespace": "PIXI.spine",
8+
"exports": {
9+
".": {
10+
"import": "./lib/index.mjs",
11+
"require": "./lib/index.js",
12+
"types": "./index.d.ts"
13+
}
14+
},
15+
"extensionConfig": {
16+
"namespace": "PIXI.spine",
17+
"bundle": "dist/loader-base.js",
18+
"bundleModule": "dist/loader-base.mjs",
19+
"globals": {
20+
"@pixi-spine/base": "PIXI.spine"
21+
}
22+
},
1023
"peerDependencies": {
1124
"@pixi/constants": "^6.1.0",
1225
"@pixi/core": "^6.1.0",
1326
"@pixi/loaders": "^6.1.0",
1427
"@pixi-spine/base": "*"
1528
},
1629
"scripts": {
17-
"build": "rollup -c rollup.config.js --silent",
18-
"build:types": "rimraf compile && tsc -p tsconfig-api.json && api-extractor run && node scripts/injectGlobalMixins"
30+
"build": "run-p build:*",
31+
"build:rollup": "rollup -c rollup.config.mjs --silent",
32+
"build:types": "rimraf compile && tsc -p tsconfig-api.json && api-extractor run && node ../../scripts/injectGlobalMixins",
33+
"lint": "eslint src/**/*.ts",
34+
"lint:fix": "eslint src/**/*.ts --fix"
1935
},
2036
"repository": {
2137
"type": "git",

packages/loader-base/rollup.config.js

-7
This file was deleted.
+4
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import configBuilder from "@pixi-spine/rollup-config";
2+
import pkg from "./package.json" assert { type: "json" };
3+
4+
export default configBuilder(pkg.extensionConfig, pkg);

packages/loader-base/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,4 @@
1+
/* eslint-disable spaced-comment */
2+
// eslint-disable-next-line @typescript-eslint/triple-slash-reference
13
/// <reference path="../global.d.ts" />
24
export * from './loaders';

packages/loader-base/src/loaders.ts

+52-42
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
import {IResourceMetadata, Loader, LoaderResource} from "@pixi/loaders";
2-
import {BaseTexture, Texture} from "@pixi/core";
3-
import {ISkeletonParser, TextureAtlas} from "@pixi-spine/base";
4-
import {ALPHA_MODES} from "@pixi/constants";
1+
import { IResourceMetadata, Loader, LoaderResource } from '@pixi/loaders';
2+
import { BaseTexture, IAutoDetectOptions, Resource, Texture } from '@pixi/core';
3+
import { ISkeletonParser, TextureAtlas } from '@pixi-spine/base';
4+
import { ALPHA_MODES } from '@pixi/constants';
55

66
function isJson(resource: LoaderResource) {
77
return resource.type === LoaderResource.TYPE.JSON;
@@ -24,6 +24,7 @@ export abstract class AbstractSpineParser {
2424
abstract parseData(resource: LoaderResource, parser: ISkeletonParser, atlas: TextureAtlas, dataToParse: any): void;
2525

2626
genMiddleware() {
27+
// eslint-disable-next-line @typescript-eslint/no-this-alias
2728
const self = this;
2829

2930
return {
@@ -34,8 +35,7 @@ export abstract class AbstractSpineParser {
3435
}
3536

3637
const isJsonSpineModel = isJson(resource) && resource.data.bones;
37-
const isBinarySpineModel = isBuffer(resource) && (resource.extension === 'skel' || resource.metadata
38-
&& (resource.metadata as any).spineMetadata);
38+
const isBinarySpineModel = isBuffer(resource) && (resource.extension === 'skel' || (resource.metadata && (resource.metadata as any).spineMetadata));
3939

4040
if (!isJsonSpineModel && !isBinarySpineModel) {
4141
return next();
@@ -61,11 +61,13 @@ export abstract class AbstractSpineParser {
6161
}
6262

6363
const metadataAtlas = metadata.spineAtlas;
64+
6465
if (metadataAtlas === false) {
6566
return next();
6667
}
6768
if (metadataAtlas && metadataAtlas.pages) {
6869
self.parseData(resource, parser, metadataAtlas, dataToParse);
70+
6971
return next();
7072
}
7173

@@ -77,83 +79,91 @@ export abstract class AbstractSpineParser {
7779
* have the same name
7880
*/
7981
let atlasPath = resource.url;
80-
let queryStringPos = atlasPath.indexOf('?');
82+
const queryStringPos = atlasPath.indexOf('?');
83+
8184
if (queryStringPos > 0) {
82-
//remove querystring
83-
atlasPath = atlasPath.substr(0, queryStringPos)
85+
// remove querystring
86+
atlasPath = atlasPath.substr(0, queryStringPos);
8487
}
8588
atlasPath = atlasPath.substr(0, atlasPath.lastIndexOf('.')) + metadataAtlasSuffix;
86-
// use atlas path as a params. (no need to use same atlas file name with json file name)
89+
// use atlas path as a params. (no need to use same atlas file name with json file name)
8790
if (metadata.spineAtlasFile) {
8891
atlasPath = metadata.spineAtlasFile;
8992
}
9093

91-
//remove the baseUrl
94+
// remove the baseUrl
9295
atlasPath = atlasPath.replace(this.baseUrl, '');
9396

9497
const atlasOptions = {
9598
crossOrigin: resource.crossOrigin,
9699
xhrType: LoaderResource.XHR_RESPONSE_TYPE.TEXT,
97100
metadata: metadata.spineMetadata || null,
98-
parentResource: resource
101+
parentResource: resource,
99102
};
100103
const imageOptions = {
101104
crossOrigin: resource.crossOrigin,
102105
metadata: metadata.imageMetadata || null,
103-
parentResource: resource
106+
parentResource: resource,
104107
};
105108
let baseUrl = resource.url.substr(0, resource.url.lastIndexOf('/') + 1);
106-
//remove the baseUrl
109+
// remove the baseUrl
110+
107111
baseUrl = baseUrl.replace(this.baseUrl, '');
108112

109-
const namePrefix = metadata.imageNamePrefix || (resource.name + '_atlas_page_');
113+
const namePrefix = metadata.imageNamePrefix || `${resource.name}_atlas_page_`;
114+
115+
let adapter: (line: string, callback: (baseTexture: BaseTexture<Resource, IAutoDetectOptions>) => any) => void;
110116

111-
const adapter = metadata.images ? staticImageLoader(metadata.images)
112-
: metadata.image ? staticImageLoader({'default': metadata.image})
113-
: metadata.imageLoader ? metadata.imageLoader(this, namePrefix, baseUrl, imageOptions)
114-
: imageLoaderAdapter(this, namePrefix, baseUrl, imageOptions);
117+
if (metadata.images) adapter = staticImageLoader(metadata.images);
118+
else if (metadata.image) adapter = staticImageLoader({ default: metadata.image });
119+
else if (metadata.imageLoader) adapter = metadata.imageLoader(this, namePrefix, baseUrl, imageOptions);
120+
else adapter = imageLoaderAdapter(this, namePrefix, baseUrl, imageOptions);
115121

116-
function createSkeletonWithRawAtlas(rawData: string) {
117-
new TextureAtlas(rawData, adapter, function(spineAtlas) {
122+
const createSkeletonWithRawAtlas = (rawData: string) => {
123+
// eslint-disable-next-line no-new
124+
new TextureAtlas(rawData, adapter, (spineAtlas) => {
118125
if (spineAtlas) {
119126
self.parseData(resource, parser, spineAtlas, dataToParse);
120127
}
121128
next();
122129
});
123-
}
130+
};
124131

125132
if (metadata.atlasRawData) {
126-
createSkeletonWithRawAtlas(metadata.atlasRawData)
133+
createSkeletonWithRawAtlas(metadata.atlasRawData);
127134
} else {
128-
this.add(resource.name + '_atlas', atlasPath, atlasOptions, function (atlasResource: any) {
135+
this.add(`${resource.name}_atlas`, atlasPath, atlasOptions, (atlasResource: any) => {
129136
if (!atlasResource.error) {
130137
createSkeletonWithRawAtlas(atlasResource.data);
131138
} else {
132139
next();
133140
}
134141
});
135142
}
136-
}
137-
}
143+
},
144+
};
138145
}
139146
}
140147

141148
/**
142149
* @public
143150
*/
144151
export function imageLoaderAdapter(loader: any, namePrefix: any, baseUrl: any, imageOptions: any) {
145-
if (baseUrl && baseUrl.lastIndexOf('/') !== (baseUrl.length - 1)) {
152+
if (baseUrl && baseUrl.lastIndexOf('/') !== baseUrl.length - 1) {
146153
baseUrl += '/';
147154
}
148-
return function (line: string, callback: (baseTexture: BaseTexture) => any) {
155+
156+
return (line: string, callback: (baseTexture: BaseTexture) => any) => {
149157
const name = namePrefix + line;
150158
const url = baseUrl + line;
151159

152160
const cachedResource = loader.resources[name];
161+
153162
if (cachedResource) {
154163
const done = () => {
155-
callback(cachedResource.texture.baseTexture)
156-
}
164+
callback(cachedResource.texture.baseTexture);
165+
};
166+
157167
if (cachedResource.texture) {
158168
done();
159169
} else {
@@ -172,30 +182,30 @@ export function imageLoaderAdapter(loader: any, namePrefix: any, baseUrl: any, i
172182
}
173183
});
174184
}
175-
}
185+
};
176186
}
177187

178188
/**
179189
* @public
180190
*/
181191
export function syncImageLoaderAdapter(baseUrl: any, crossOrigin: any) {
182-
if (baseUrl && baseUrl.lastIndexOf('/') !== (baseUrl.length - 1)) {
192+
if (baseUrl && baseUrl.lastIndexOf('/') !== baseUrl.length - 1) {
183193
baseUrl += '/';
184194
}
185-
return function (line: any, callback: any) {
195+
196+
return (line: any, callback: any) => {
186197
callback(BaseTexture.from(line, crossOrigin));
187-
}
198+
};
188199
}
189200

190201
/**
191202
* @public
192203
*/
193-
export function staticImageLoader(pages: { [key: string]: (BaseTexture | Texture) }) {
194-
return function (line: any, callback: any) {
195-
let page = pages[line] || pages['default'] as any;
196-
if (page && page.baseTexture)
197-
callback(page.baseTexture);
198-
else
199-
callback(page);
200-
}
204+
export function staticImageLoader(pages: { [key: string]: BaseTexture | Texture }) {
205+
return (line: any, callback: any) => {
206+
const page = pages[line] || (pages.default as any);
207+
208+
if (page && page.baseTexture) callback(page.baseTexture);
209+
else callback(page);
210+
};
201211
}
File renamed without changes.

0 commit comments

Comments
 (0)