images2atlas 1.0.1 → 1.0.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +1 -1
- package/dist/index.js +80 -74
- package/dist/spritesheet-templates.d.ts +6 -0
- package/dist/spritesheet-templates.js +3 -0
- package/package.json +4 -8
- package/spritesheet-templates.d.ts +0 -10
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -1,13 +1,4 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
-
});
|
|
10
|
-
};
|
|
11
2
|
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
4
|
};
|
|
@@ -17,9 +8,9 @@ const chokidar_1 = require("chokidar");
|
|
|
17
8
|
const debounce_1 = __importDefault(require("debounce"));
|
|
18
9
|
const fs_extra_1 = __importDefault(require("fs-extra"));
|
|
19
10
|
const path_1 = __importDefault(require("path"));
|
|
20
|
-
const spritesheet_templates_1 = __importDefault(require("spritesheet-templates"));
|
|
21
11
|
const spritesmith_1 = __importDefault(require("spritesmith"));
|
|
22
12
|
const web_build_utils_1 = require("web-build-utils");
|
|
13
|
+
const spritesheet_templates_1 = __importDefault(require("./spritesheet-templates"));
|
|
23
14
|
// Supported output template formats for spritesheet-templates.
|
|
24
15
|
const formatTypes = ['css', 'json', 'less', 'sass', 'scss', 'styl'];
|
|
25
16
|
/**
|
|
@@ -44,13 +35,23 @@ class Images2atlas {
|
|
|
44
35
|
if (path_1.default.extname(options.dest)) {
|
|
45
36
|
throw new Error('The options.dest must be a directory.');
|
|
46
37
|
}
|
|
47
|
-
this._options =
|
|
38
|
+
this._options = {
|
|
39
|
+
exclude: () => false,
|
|
40
|
+
include: () => true,
|
|
41
|
+
suffix: '-atlas',
|
|
42
|
+
delay: 500,
|
|
43
|
+
silent: true,
|
|
44
|
+
watch: false,
|
|
45
|
+
spritesmithOptions: {
|
|
48
46
|
padding: 2,
|
|
49
47
|
exportOpts: {
|
|
50
48
|
format: 'png',
|
|
51
49
|
quality: 100,
|
|
52
50
|
},
|
|
53
|
-
},
|
|
51
|
+
},
|
|
52
|
+
templatesOptions: {},
|
|
53
|
+
...options,
|
|
54
|
+
};
|
|
54
55
|
}
|
|
55
56
|
/**
|
|
56
57
|
* Logs messages using console if not silent.
|
|
@@ -98,39 +99,37 @@ class Images2atlas {
|
|
|
98
99
|
* @param src Source directory
|
|
99
100
|
* @param dest Destination directory
|
|
100
101
|
*/
|
|
101
|
-
pack(src, dest) {
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
pngPaths.push(srcPath);
|
|
119
|
-
}
|
|
120
|
-
else {
|
|
121
|
-
// Copy single file
|
|
122
|
-
yield fs_extra_1.default.outputFile(destPath, yield fs_extra_1.default.readFile(srcPath));
|
|
123
|
-
}
|
|
102
|
+
async pack(src, dest) {
|
|
103
|
+
this.log('pack', src);
|
|
104
|
+
const { exclude, include } = this._options;
|
|
105
|
+
const fullNames = await fs_extra_1.default.readdir(src);
|
|
106
|
+
const pngPaths = [];
|
|
107
|
+
await Promise.all(fullNames.map(async (fullName) => {
|
|
108
|
+
const srcPath = path_1.default.join(src, fullName);
|
|
109
|
+
const info = path_1.default.parse(srcPath);
|
|
110
|
+
if (!(0, web_build_utils_1.isSafeFilename)(srcPath, true) || exclude(info, srcPath)) {
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
const destPath = path_1.default.join(dest, info.base);
|
|
114
|
+
if (info.ext) {
|
|
115
|
+
// File
|
|
116
|
+
if (info.ext === '.png' && include(info, srcPath)) {
|
|
117
|
+
// Collect PNGs for atlas generation later.
|
|
118
|
+
pngPaths.push(srcPath);
|
|
124
119
|
}
|
|
125
120
|
else {
|
|
126
|
-
//
|
|
127
|
-
|
|
121
|
+
// Copy single file
|
|
122
|
+
await fs_extra_1.default.outputFile(destPath, await fs_extra_1.default.readFile(srcPath));
|
|
128
123
|
}
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
124
|
+
}
|
|
125
|
+
else {
|
|
126
|
+
// Dir
|
|
127
|
+
await this.pack(srcPath, destPath);
|
|
128
|
+
}
|
|
129
|
+
}));
|
|
130
|
+
// Generate atlas and template outputs for collected PNGs.
|
|
131
|
+
await this.packSpriteSheet(pngPaths, dest);
|
|
132
|
+
this.log('packed', dest);
|
|
134
133
|
}
|
|
135
134
|
/**
|
|
136
135
|
* Generates the spritesheet and style/template files from collected PNG paths.
|
|
@@ -138,38 +137,45 @@ class Images2atlas {
|
|
|
138
137
|
* @param pngPaths Array of PNG image paths
|
|
139
138
|
* @param dest Destination directory
|
|
140
139
|
*/
|
|
141
|
-
packSpriteSheet(pngPaths, dest) {
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
})
|
|
140
|
+
async packSpriteSheet(pngPaths, dest) {
|
|
141
|
+
if (pngPaths.length === 0) {
|
|
142
|
+
return;
|
|
143
|
+
}
|
|
144
|
+
const { suffix, spritesmithOptions, templatesOptions } = this._options;
|
|
145
|
+
const format = templatesOptions.format || 'css';
|
|
146
|
+
await new Promise((resolve, reject) => {
|
|
147
|
+
spritesmith_1.default.run({
|
|
148
|
+
...spritesmithOptions,
|
|
149
|
+
src: pngPaths,
|
|
150
|
+
}, async (err, result) => {
|
|
151
|
+
if (err) {
|
|
152
|
+
return reject(err);
|
|
153
|
+
}
|
|
154
|
+
// Map image coordinates to sprite objects
|
|
155
|
+
const sprites = Object.keys(result.coordinates).map((imgPath) => {
|
|
156
|
+
const info = path_1.default.parse(imgPath);
|
|
157
|
+
return {
|
|
158
|
+
...result.coordinates[imgPath],
|
|
159
|
+
name: info.name,
|
|
160
|
+
};
|
|
161
|
+
});
|
|
162
|
+
// Spritesheet properties and output image path
|
|
163
|
+
const spritesheet = {
|
|
164
|
+
...result.properties,
|
|
165
|
+
image: `${path_1.default.basename(dest) + suffix}.png`,
|
|
166
|
+
};
|
|
167
|
+
// Generate template/style file
|
|
168
|
+
const temp = (0, spritesheet_templates_1.default)({
|
|
169
|
+
sprites,
|
|
170
|
+
spritesheet,
|
|
171
|
+
}, templatesOptions);
|
|
172
|
+
// Determine output file extension
|
|
173
|
+
const ext = formatTypes.find((type) => format.startsWith(type)) || format;
|
|
174
|
+
await Promise.all([
|
|
175
|
+
fs_extra_1.default.outputFile(dest + suffix + '.' + ext, temp),
|
|
176
|
+
fs_extra_1.default.outputFile(dest + suffix + '.png', result.image),
|
|
177
|
+
]);
|
|
178
|
+
resolve();
|
|
173
179
|
});
|
|
174
180
|
});
|
|
175
181
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "images2atlas",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.3",
|
|
4
4
|
"description": "Plugin that converts set of images into a spritesheet by its directory and SASS/LESS/Stylus mixins",
|
|
5
5
|
"author": "Porky Ke",
|
|
6
6
|
"homepage": "https://github.com/porky-prince/web-build-tools/packages/images2atlas#readme",
|
|
@@ -9,8 +9,7 @@
|
|
|
9
9
|
"main": "dist/index.js",
|
|
10
10
|
"types": "dist/index.d.ts",
|
|
11
11
|
"files": [
|
|
12
|
-
"dist"
|
|
13
|
-
"spritesheet-templates.d.ts"
|
|
12
|
+
"dist"
|
|
14
13
|
],
|
|
15
14
|
"publishConfig": {
|
|
16
15
|
"access": "public"
|
|
@@ -27,12 +26,9 @@
|
|
|
27
26
|
"fs-extra": "^11.3.2",
|
|
28
27
|
"spritesheet-templates": "^10.5.2",
|
|
29
28
|
"spritesmith": "^3.5.1",
|
|
30
|
-
"web-build-utils": "^0.0.
|
|
31
|
-
},
|
|
32
|
-
"devDependencies": {
|
|
33
|
-
"@types/fs-extra": "^11.0.4",
|
|
34
|
-
"@types/node": "^24.6.2"
|
|
29
|
+
"web-build-utils": "^0.0.6"
|
|
35
30
|
},
|
|
31
|
+
"devDependencies": {},
|
|
36
32
|
"keywords": [
|
|
37
33
|
"icons",
|
|
38
34
|
"images",
|