images2atlas 1.0.0 → 1.0.2

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.
Files changed (2) hide show
  1. package/dist/index.js +81 -75
  2. package/package.json +3 -6
package/dist/index.js CHANGED
@@ -1,24 +1,15 @@
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
  };
14
5
  Object.defineProperty(exports, "__esModule", { value: true });
15
6
  exports.images2atlas = images2atlas;
16
7
  const chokidar_1 = require("chokidar");
17
- const path_1 = __importDefault(require("path"));
8
+ const debounce_1 = __importDefault(require("debounce"));
18
9
  const fs_extra_1 = __importDefault(require("fs-extra"));
10
+ const path_1 = __importDefault(require("path"));
19
11
  const spritesheet_templates_1 = __importDefault(require("spritesheet-templates"));
20
12
  const spritesmith_1 = __importDefault(require("spritesmith"));
21
- const debounce_1 = __importDefault(require("debounce"));
22
13
  const web_build_utils_1 = require("web-build-utils");
23
14
  // Supported output template formats for spritesheet-templates.
24
15
  const formatTypes = ['css', 'json', 'less', 'sass', 'scss', 'styl'];
@@ -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 = Object.assign({ exclude: () => false, include: () => true, suffix: '-atlas', delay: 500, silent: true, watch: false, spritesmithOptions: {
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
- }, templatesOptions: {} }, options);
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
- return __awaiter(this, void 0, void 0, function* () {
103
- this.log('pack', src);
104
- const { exclude, include } = this._options;
105
- const fullNames = yield fs_extra_1.default.readdir(src);
106
- const pngPaths = [];
107
- yield Promise.all(fullNames.map((fullName) => __awaiter(this, void 0, void 0, function* () {
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);
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
- // Dir
127
- yield this.pack(srcPath, destPath);
121
+ // Copy single file
122
+ await fs_extra_1.default.outputFile(destPath, await fs_extra_1.default.readFile(srcPath));
128
123
  }
129
- })));
130
- // Generate atlas and template outputs for collected PNGs.
131
- yield this.packSpriteSheet(pngPaths, dest);
132
- this.log('packed', dest);
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
- return __awaiter(this, void 0, void 0, function* () {
143
- if (pngPaths.length === 0) {
144
- return;
145
- }
146
- const { suffix, spritesmithOptions, templatesOptions } = this._options;
147
- const format = templatesOptions.format || 'css';
148
- yield new Promise((resolve, reject) => {
149
- spritesmith_1.default.run(Object.assign(Object.assign({}, spritesmithOptions), { src: pngPaths }), (err, result) => __awaiter(this, void 0, void 0, function* () {
150
- if (err) {
151
- return reject(err);
152
- }
153
- // Map image coordinates to sprite objects
154
- const sprites = Object.keys(result.coordinates).map((imgPath) => {
155
- const info = path_1.default.parse(imgPath);
156
- return Object.assign(Object.assign({}, result.coordinates[imgPath]), { name: info.name });
157
- });
158
- // Spritesheet properties and output image path
159
- const spritesheet = Object.assign(Object.assign({}, result.properties), { image: `${path_1.default.basename(dest) + suffix}.png` });
160
- // Generate template/style file
161
- const temp = (0, spritesheet_templates_1.default)({
162
- sprites,
163
- spritesheet,
164
- }, templatesOptions);
165
- // Determine output file extension
166
- const ext = formatTypes.find((type) => format.startsWith(type)) || format;
167
- yield Promise.all([
168
- fs_extra_1.default.outputFile(dest + suffix + '.' + ext, temp),
169
- fs_extra_1.default.outputFile(dest + suffix + '.png', result.image),
170
- ]);
171
- resolve();
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.0",
3
+ "version": "1.0.2",
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",
@@ -27,12 +27,9 @@
27
27
  "fs-extra": "^11.3.2",
28
28
  "spritesheet-templates": "^10.5.2",
29
29
  "spritesmith": "^3.5.1",
30
- "web-build-utils": "^0.0.4"
31
- },
32
- "devDependencies": {
33
- "@types/fs-extra": "^11.0.4",
34
- "@types/node": "^24.6.2"
30
+ "web-build-utils": "^0.0.6"
35
31
  },
32
+ "devDependencies": {},
36
33
  "keywords": [
37
34
  "icons",
38
35
  "images",