-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathplugin.js
163 lines (143 loc) · 5.38 KB
/
plugin.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
/**
* @PACKAGE_INFO
* An webpack plugin that helps to create sprite image
* and generate manifest.json file contains coordinate
* of each image in sprite
*/
const Spritesmith = require("spritesmith");
const path = require("path");
const { existsSync, lstatSync, writeFile: fsWriteFile, mkdirSync } = require("fs");
const util = require("util");
const writeFile = util.promisify(fsWriteFile)
const NAMESPACE = "SPRITE_PNG_PLUGIN";
/**
* @_DEV_REFERENCE
* - Compiler docs: https://webpack.js.org/api/compiler-hooks
* - Compilation docs: https://webpack.js.org/api/compilation-hooks/
* - SpriteSmith: https://github.com/twolfson/spritesmith
* - Refer: https://github.com/naver/image-sprite-webpack-plugin
* - Usage webpack-virtual-modules: https://github.com/sysgears/webpack-virtual-modules/blob/master/examples/swagger-webpack4/webpack.config.js
*/
module.exports = class SpritePNG_Plugin {
#cwd
#spriteImagePath
#manifestPath
/**
* @OPTION
* - outputDir: sprite data output directory
* - entry: plugin will collect and watch all file in there
* - excludes: ignore these files
*/
constructor(option) {
this.#cwd = process.cwd();
this._outputDir = this.#getOutputDir(option.outputDir);
this._excludes = option.excludes; // Reg and Reg Array are allowed
this._entryPath = this.#getEntry(option.entry);
this.#spriteImagePath = path.join(this._outputDir, "sprite.png");
this.#manifestPath = path.join(this._outputDir, "manifest.json");
}
#getOutputDir(outputDir = "./") {
const _outputDir = path.join(this.#cwd, outputDir);
if (!existsSync(_outputDir) || !lstatSync(_outputDir).isDirectory()) {
mkdirSync(_outputDir)
}
return _outputDir;
}
#getEntry(entry) {
if (!entry) return [];
if (!Array.isArray(entry)) entry = [entry];
return entry;
}
#createSpriteSheet(imagesData, callback) {
Spritesmith.run({
src: imagesData
}, (err, result) => {
if (err) throw err;
callback(result);
});
}
#notExcludes(filePath) {
if (!this._excludes) return true;
const isRegExp = this._excludes instanceof RegExp;
const isArr = Array.isArray(this._excludes);
if (!(isRegExp || isArr)) throw Error(`"excludes" can be "RegExp" or "RegExp Array" only but received "${typeof this._excludes}"`);
const posixPath = this.#convert2Posix(filePath);
if (isRegExp) return !this._excludes.test(posixPath);
if (isArr) {
for (let idx = 0; idx < this._excludes.length; idx++) {
const it = this._excludes[idx];
if (!(it instanceof RegExp)) throw Error(`Item at ${idx} must be "RegExp" but received "${typeof it}"`);
if (it.test(posixPath)) return false;
}
}
return true;
}
#convert2Posix(v) {
return v ? v.split(path.sep).join(path.posix.sep) : ''
}
#generateSpriteSheet(sources, callback) {
this.#createSpriteSheet(sources, ({ coordinates, properties, image }) => {
let metadata = {};
Object.keys(coordinates).forEach(filePath => {
const fileName = `${path.dirname(filePath).split(path.sep).pop()}_${path.basename(filePath)}`;
metadata[fileName] = {
x: coordinates[filePath].x,
y: coordinates[filePath].y,
w: coordinates[filePath].width,
h: coordinates[filePath].height
}
})
if (callback) {
Promise.all([
writeFile(this.#manifestPath, JSON.stringify(metadata), "utf8"),
writeFile(this.#spriteImagePath, image, "binary")
]).finally(callback);
} else {
writeFile(this.#manifestPath, JSON.stringify(metadata), "utf8"),
writeFile(this.#spriteImagePath, image, "binary")
}
});
}
#spriteProcess(watcher) {
if (!watcher) throw Error('Watcher is not initialed');
const sourceImagesByFolder = watcher.watched();
const allSourceImages = Object.values(sourceImagesByFolder)
.flatMap(v => v.filter(p => this.#notExcludes(p) && this.#isPng(p)));
if (allSourceImages?.length > 0) {
this.#generateSpriteSheet(allSourceImages);
};
}
#pathEqual(path1, path2) {
return path.relative(path1, path2) == '';
}
#isPng(filePath) {
return filePath?.endsWith(".png")
}
apply(compiler) {
const isProd = compiler.options.mode === "production";
const scanner = isProd ? require("glob").globSync : require("gaze").Gaze;
if (isProd) {
compiler.hooks.beforeRun.tapAsync(NAMESPACE, (_, callback) => {
const allSourceImages = scanner(this._entryPath, { ignore: "node_modules/**", cwd: this.#cwd })
.map(v => path.join(this.#cwd, v))
.filter(p => this.#notExcludes(p) && this.#isPng(p))
if (allSourceImages.length > 0) this.#generateSpriteSheet(allSourceImages, callback);
})
} else {
const gazeIns = new scanner(this._entryPath, { cwd: this.#cwd, mode: "watch", debounceDelay: 400 });
gazeIns.on("ready", this.#spriteProcess.bind(this, gazeIns));
// Listening files changed
gazeIns.on("all", (event, filePath) => {
if (
event !== "renamed" &&
this.#notExcludes(filePath) &&
!this.#pathEqual(filePath, this.#spriteImagePath) &&
!this.#pathEqual(filePath, this.#manifestPath) &&
this.#isPng(filePath)
) {
this.#spriteProcess(gazeIns);
}
});
}
}
}