Skip to content

Commit

Permalink
Merge branch '0.0.3'
Browse files Browse the repository at this point in the history
  • Loading branch information
mayakwd committed Dec 25, 2019
2 parents b12c943 + f430586 commit 9a50a1b
Show file tree
Hide file tree
Showing 10 changed files with 96 additions and 70 deletions.
22 changes: 11 additions & 11 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "webpack-texture-packer",
"version": "0.0.2",
"version": "0.0.3",
"main": "dist/index.js",
"typings": "dist/index.d.ts",
"license": "MIT",
Expand All @@ -16,22 +16,22 @@
"test": "jest"
},
"devDependencies": {
"@microsoft/tsdoc": "^0.12.14",
"@types/jest": "^24.0.18",
"@microsoft/tsdoc": "^0.12.16",
"@types/jest": "^24.0.24",
"@types/minimatch": "^3.0.3",
"@types/node": "^12.7.2",
"@types/webpack": "^4.39.1",
"@types/node": "^13.1.0",
"@types/webpack": "^4.41.0",
"jest": "^24.9.0",
"ts-jest": "^24.1.0",
"tslint": "^5.19.0",
"typescript": "^3.6.4",
"webpack": "^4.39.2",
"ts-jest": "^24.2.0",
"tslint": "^5.20.1",
"typescript": "^3.7.4",
"webpack": "^4.41.4",
"webpack-recompilation-simulator": "^3.0.0"
},
"dependencies": {
"free-tex-packer-core": "^0.2.4",
"free-tex-packer-core": "^0.2.9",
"minimatch": "^3.0.4",
"readdir-enhanced": "^4.0.2",
"readdir-enhanced": "^5.1.1",
"webpack-log": "^3.0.1"
},
"peerDependencies": {
Expand Down
75 changes: 45 additions & 30 deletions spec/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,43 +1,58 @@
import * as fs from "fs";
import * as path from "path";
import {Stats} from "webpack";
import {PackerExporterType, ScaleMethod, WebpackTexturePackerPlugin} from "../src";
import {IAssetsConfiguration} from "../src/plugin/config/IAssetsConfiguration";
import webpack = require("webpack");
import {PackerExporterType, WebpackTexturePackerPlugin} from "../src";

jest.setTimeout(30000);

describe("plugin tests", () => {
it("running", (done) => {
const assetsConfiguration = {
rootDir: path.join(__dirname, "fixtures/simple/assets"),
items: [
{
outDir: "assets/atlases",
name: "enemies",
source: "enemies",
packerOptions: {exporter: PackerExporterType.PIXI, removeFileExtension: true},
},
const assetsConfiguration: IAssetsConfiguration = {
rootDir: path.join(__dirname, "fixtures/simple/assets"),
items: [
{
outDir: "assets/atlases",
name: "enemies",
source: "enemies",
packerOptions: {
scale: 2,
scaleMethod: ScaleMethod.NEAREST_NEIGHBOR,
exporter: PackerExporterType.PIXI,
removeFileExtension: true,
},
overwrite: false,
},
{
name: "environment",
outDir: "assets/atlases",
source: [
{
name: "environment",
outDir: "assets/atlases",
source: [
{
path: "environment",
},
{
path: "weapons",
exclude: "weapon_r*",
recursive: true,
},
],
path: "environment",
},
{
rootDir: "ui",
name: "ui",
outDir: "assets/ui",
path: "weapons",
exclude: "weapon_r*",
recursive: true,
},
],
};
packerOptions: {
scale: 0.75,
scaleMethod: ScaleMethod.BEZIER,
},
},
{
rootDir: "ui",
name: "ui",
outDir: "assets/ui",
packerOptions: {
scale: 4,
scaleMethod: ScaleMethod.NEAREST_NEIGHBOR,
},
},
],
};
describe("plugin tests", () => {
it("running", (done) => {
const webpackOutDir = path.join(__dirname, "temp");
webpack({
mode: "production",
Expand All @@ -53,8 +68,8 @@ describe("plugin tests", () => {
.toBeNull();
for (const atlas of assetsConfiguration.items) {
const {name, outDir} = atlas;
expect(fs.existsSync(path.join(webpackOutDir, outDir, name + ".png"))).toBeTruthy();
expect(fs.existsSync(path.join(webpackOutDir, outDir, name + ".json"))).toBeTruthy();
expect(fs.existsSync(path.join(webpackOutDir, outDir!, name + ".png"))).toBeTruthy();
expect(fs.existsSync(path.join(webpackOutDir, outDir!, name + ".json"))).toBeTruthy();
}
done();
});
Expand Down
1 change: 0 additions & 1 deletion src/declarations/free-text-packer-core.d.ts

This file was deleted.

2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from "./plugin/texture-packer/ITexturePackerOptions";
export * from "./plugin/texture-packer/TexturePackerOptions";
export * from "./plugin/WebpackTexturePackerPlugin";
14 changes: 6 additions & 8 deletions src/plugin/AtlasBuilder.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import texPacker from "free-tex-packer-core";
import {packAsync} from "free-tex-packer-core";
import * as fs from "fs";
import {Minimatch} from "minimatch";
import * as path from "path";
import {readdirAsync, Stats} from "readdir-enhanced";
import {AssetsSource, IAssetsSourceConfiguration, IAtlasConfiguration} from "./config/IAtlasConfiguration";
import {defaultTexturePackerOptions} from "./texture-packer/DefaultTexturePackerOptions";
import {ITexturePackerOptions} from "./texture-packer/ITexturePackerOptions";
import {TexturePackerOptions} from "./texture-packer/TexturePackerOptions";

/** @internal */
export class AtlasBuilder {
Expand All @@ -20,7 +20,7 @@ export class AtlasBuilder {

public constructor(
private readonly atlasConfig: IAtlasConfiguration,
private readonly globalPackerOptions?: ITexturePackerOptions,
private readonly globalPackerOptions?: TexturePackerOptions,
globalAssetsRootDir: string = "",
) {
const {rootDir = "", source = ""} = atlasConfig;
Expand All @@ -30,19 +30,17 @@ export class AtlasBuilder {
this.assetsRootDir = path.join(globalAssetsRootDir, rootDir);
}

public async build(): Promise<Array<{ name: string, buffer: Buffer }>> {
public async build(): Promise<IPackedAtlasPair[]> {
const {source = ""} = this.atlasConfig;
const assetsList = await this.readAssetsListFromSource(source);
const texturePackerOptions = this.getTexturePackerOptions();
if (assetsList.length === 0) {
return [];
}
return new Promise((resolve, reject) => texPacker(
return await packAsync(
this.readFilesFromAssetsList(assetsList),
texturePackerOptions,
(result: IPackedAtlasPair[], err?: Error) => {
if (!err) { resolve(result); } else { reject(err); }
}));
);
}

private getTexturePackerOptions() {
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/config/IAssetsConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ITexturePackerOptions} from "../..";
import {TexturePackerOptions} from "../..";
import {IAtlasConfiguration} from "./IAtlasConfiguration";

export interface IAssetsConfiguration {
Expand Down Expand Up @@ -29,5 +29,5 @@ export interface IAssetsConfiguration {
* @see ITexturePackerOptions
* @see defaultTexturePackerOptions
*/
packerOptions?: ITexturePackerOptions;
packerOptions?: TexturePackerOptions;
}
4 changes: 2 additions & 2 deletions src/plugin/config/IAtlasConfiguration.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import {ITexturePackerOptions} from "../..";
import {TexturePackerOptions} from "../..";
import {IAssetsConfiguration} from "./IAssetsConfiguration";

/**
Expand Down Expand Up @@ -56,7 +56,7 @@ export interface IAtlasConfiguration {
* Texture packer options
* @see ITexturePackerOptions
*/
packerOptions?: ITexturePackerOptions;
packerOptions?: TexturePackerOptions;
/**
* Value indicates whether overwrite global texture packer options for this atlas.
* - If `true` - global texture packer options will be replaced
Expand Down
4 changes: 2 additions & 2 deletions src/plugin/texture-packer/DefaultTexturePackerOptions.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {ITexturePackerOptions, MaxRectsPackerMethod, PackerExporterType, PackerType} from "./ITexturePackerOptions";
import {MaxRectsPackerMethod, PackerExporterType, PackerType, TexturePackerOptions} from "./TexturePackerOptions";

export const defaultTexturePackerOptions: ITexturePackerOptions = {
export const defaultTexturePackerOptions: TexturePackerOptions = {
fixedSize: false,
padding: 1,
allowRotation: true,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
/**
* Trim mode for sprites
*
* @see ITexturePackerOptions.trimMode
* @see ITexturePackerOptions.allowTrim
* @see TexturePackerOptions.trimMode
* @see TexturePackerOptions.allowTrim
*/
export enum TrimMode {
/**
Expand All @@ -26,7 +26,7 @@ export enum TrimMode {
/**
* Output atlas texture format
*
* @see ITexturePackerOptions.textureFormat
* @see TexturePackerOptions.textureFormat
*/
export enum TextureFormat {
PNG = "png",
Expand All @@ -37,8 +37,8 @@ export enum TextureFormat {
* Atlas packer type.
* There are two implementations which could be used
*
* @see ITexturePackerOptions.packer
* @see ITexturePackerOptions.packerMethod
* @see TexturePackerOptions.packer
* @see TexturePackerOptions.packerMethod
* @see MaxRectsBinMethod
* @see MaxRectsPackerMethod
*/
Expand All @@ -50,7 +50,7 @@ export enum PackerType {
/**
* MaxRectsBin packer method
*
* @see ITexturePackerOptions.packerMethod
* @see TexturePackerOptions.packerMethod
*/
export enum MaxRectsBinMethod {
BEST_SHORT_SIDE_FIT = "BestShortSideFit",
Expand All @@ -63,7 +63,7 @@ export enum MaxRectsBinMethod {
/**
* MaxRectsPacker packer method
*
* @see ITexturePackerOptions.packerMethod
* @see TexturePackerOptions.packerMethod
*/
export enum MaxRectsPackerMethod {
SMART = "Smart",
Expand All @@ -76,7 +76,7 @@ export enum MaxRectsPackerMethod {
* Predefined exporter types (supported popular formats)
* Instead of predefined type you could use custom exporter
*
* @see ITexturePackerOptions.exporter
* @see TexturePackerOptions.exporter
* @see IPackerExporter
*/
export enum PackerExporterType {
Expand All @@ -100,7 +100,7 @@ export enum PackerExporterType {
/**
* Bitmap filter, applicable to output atlas texture
*
* @see ITexturePackerOptions.filter
* @see TexturePackerOptions.filter
*/
export enum BitmapFilterType {
GRAYSCALE = "grayscale",
Expand All @@ -111,7 +111,7 @@ export enum BitmapFilterType {
/**
* Texture packer options
*/
export interface ITexturePackerOptions {
export interface TexturePackerOptions {
/**
* Name of output files.
*
Expand Down Expand Up @@ -217,6 +217,12 @@ export interface ITexturePackerOptions {
* @default 1
*/
scale?: number;
/**
* Texture scaling method
*
* @default ScaleMethod.BILINEAR
*/
scaleMethod?: ScaleMethod;
/**
* "Tinify" texture using TinyPNG
*
Expand Down Expand Up @@ -248,7 +254,7 @@ export interface ITexturePackerOptions {
*
* @default JsonHash
*/
exporter?: PackerExporterType | IPackerExporter;
exporter?: PackerExporterType | PackerExporter;
/**
* Bitmap filter type
*
Expand All @@ -265,12 +271,19 @@ export interface ITexturePackerOptions {
appInfo?: any;
}

export enum ScaleMethod {
BILINEAR = "BILINEAR",
NEAREST_NEIGHBOR = "NEAREST_NEIGHBOR",
HERMITE = "HERMITE",
BEZIER = "BEZIER",
}

/**
* Texture packer uses {@link http://mustache.github.io/ | mustache} template engine.
* Look at documentation how to create custom exporter:
* {@link https://www.npmjs.com/package/free-tex-packer-core#custom-exporter}
*/
export interface IPackerExporter {
export interface PackerExporter {
/**
* File extension
*/
Expand Down
3 changes: 2 additions & 1 deletion tslint.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@
"object-literal-sort-keys": false,
"member-ordering": [true, {
"order": "statics-first"
}]
}],
"interface-name": false
},

"rulesDirectory": []
Expand Down

0 comments on commit 9a50a1b

Please sign in to comment.