1 | const path = require("path");
|
2 |
|
3 | module.exports = context => {
|
4 | const { load, cwd } = context;
|
5 | const gulp = load("gulp");
|
6 | const spritesmith = load("gulp.spritesmith");
|
7 |
|
8 | const config = require(path.join(cwd, "./ynw.config.js"));
|
9 | let gulpConfig = config.gulp;
|
10 | if (!gulpConfig) {
|
11 | console.log(`ynw.config未配置gulp:{src,dist}参数`.red);
|
12 | return;
|
13 | }
|
14 |
|
15 | const cssTemplate = images => images.sprites.map(transfer).join("");
|
16 | const spriteData = gulp.src(gulpConfig.src + "/*.*").pipe(
|
17 | spritesmith({
|
18 | imgName: "sprite.png",
|
19 | cssName: "sprite.css",
|
20 | padding: 10,
|
21 | cssTemplate
|
22 | })
|
23 | );
|
24 | spriteData.pipe(gulp.dest(gulpConfig.dist));
|
25 |
|
26 | function transfer(item) {
|
27 | const name = item.name;
|
28 | const bg = item.escaped_image;
|
29 | const x = item.px.offset_x;
|
30 | const y = item.px.offset_y;
|
31 | const w = item.px.width;
|
32 | const h = item.px.height;
|
33 | return `
|
34 | .sp-${name}
|
35 | { background-image: url(./${bg});
|
36 | background-position: ${x} ${y};
|
37 | width: ${w};height: ${h};}
|
38 | `;
|
39 | }
|
40 | };
|