-
Notifications
You must be signed in to change notification settings - Fork 8
/
index.js
190 lines (155 loc) · 5.24 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
const fs = require('fs');
const pathModule = require('path');
const chokidar = require('chokidar');
const texturePacker = require('free-tex-packer-core');
const appInfo = require('./package.json');
const SUPPORTED_EXT = ['png', 'jpg', 'jpeg'];
function isFolder(path) {
if (isExists(path)) {
return fs.statSync(path).isDirectory();
}
else {
path = fixPath(path);
let name = getNameFromPath(path);
let parts = name.split('.');
return parts.length === 1;
}
}
function isExists(path) {
return fs.existsSync(path);
}
function fixPath(path) {
return path.trim().split('\\').join('/');
}
function getNameFromPath(path) {
return path.trim().split('/').pop();
}
function getExtFromPath(path) {
return path.trim().split('.').pop().toLowerCase();
}
function getFolderFilesList(dir, base = '', list = []) {
let files = fs.readdirSync(dir);
for (let file of files) {
let path = pathModule.resolve(dir, file);
if (isFolder(path) && path.toUpperCase().indexOf('__MACOSX') < 0) {
list = getFolderFilesList(path, base + file + '/', list);
}
else {
list.push({
name: (base ? base : '') + file,
path: path
});
}
}
return list;
}
function getSubFoldersList(dir, list = []) {
let files = fs.readdirSync(dir);
for (let file of files) {
let path = pathModule.resolve(dir, file);
if (isFolder(path) && path.toUpperCase().indexOf('__MACOSX') < 0) {
list.push(path);
list = getSubFoldersList(path, list);
}
}
return list;
}
class WebpackFreeTexPacker {
constructor(src, dest = '.', options = {}) {
if (!Array.isArray(src)) src = [src];
this.src = src;
this.dest = dest;
this.options = options;
this.options.appInfo = appInfo;
this.changed = true;
this.watcher = null;
this.watchStarted = false;
this.onFsChanges = this.onFsChanges.bind(this);
}
addDependencie(dependencies, path) {
if (Array.isArray(dependencies)) dependencies.push(path);
else dependencies.add(path);
this.addToWatch(path);
}
addToWatch(path) {
if (!this.watcher) {
this.watcher = chokidar.watch(path, { ignoreInitial: true });
this.watcher.on('all', this.onFsChanges);
}
else {
this.watcher.add(path);
}
}
onFsChanges() {
this.changed = true;
}
apply(compiler) {
if (compiler.hooks && compiler.hooks.emit) {
// Webpack 4
compiler.hooks.emit.tapAsync('WebpackFreeTexPacker', this.emitHookHandler.bind(this));
} else {
// Webpack 3
compiler.plugin('emit', this.emitHookHandler.bind(this))
}
}
emitHookHandler(compilation, callback) {
let files = {};
let devMode = (!compilation.options || compilation.options.mode === 'development');
for (let srcPath of this.src) {
let path = fixPath(srcPath);
let name = getNameFromPath(path);
if (name === '.' || name === '*' || name === '*.*') {
srcPath = srcPath.substr(0, srcPath.length - name.length - 1);
path = fixPath(srcPath);
name = '';
}
if (isFolder(path)) {
if (isExists(srcPath)) {
let list = getFolderFilesList(path, (name ? name + '/' : ''));
for (let file of list) {
let ext = getExtFromPath(file.path);
if (SUPPORTED_EXT.indexOf(ext) >= 0) files[file.name] = file.path;
}
}
if (devMode) this.addDependencie(compilation.contextDependencies, srcPath);
let subFolders = getSubFoldersList(srcPath);
for (let folder of subFolders) {
if (devMode) this.addDependencie(compilation.contextDependencies, folder);
}
}
else {
if (isExists(srcPath)) {
files[getNameFromPath(path)] = path;
}
if (devMode) this.addDependencie(compilation.fileDependencies, srcPath);
}
}
if (this.watchStarted && !this.changed) {
callback();
return;
}
let images = [];
let names = Object.keys(files);
for (let name of names) {
images.push({ path: name, contents: fs.readFileSync(files[name]) });
}
texturePacker(images, this.options, (files) => {
for (let item of files) {
(function (item, dest) {
compilation.assets[dest + '/' + item.name] = {
source: function () {
return item.buffer;
},
size: function () {
return item.buffer.length;
}
};
})(item, this.dest);
}
callback();
});
this.changed = false;
this.watchStarted = true;
}
}
module.exports = WebpackFreeTexPacker;