spritesmith.ts
1.25 KB
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
import fs from 'fs'
import spritesmith from 'vite-plugin-spritesmith'
export default function createSpritesmith(isBuild) {
const spriteDirnames: string[] = []
fs.readdirSync('src/assets/sprites').forEach((dirname) => {
if (fs.statSync(`src/assets/sprites/${dirname}`).isDirectory()) {
spriteDirnames.push(dirname)
}
})
const plugin: any[] = []
spriteDirnames.forEach((item) => {
plugin.push(
spritesmith({
watch: !isBuild,
src: {
cwd: `./src/assets/sprites/${item}`,
glob: '*.png',
},
target: {
image: `./src/assets/sprites/${item}.png`,
css: [
[
`./src/assets/sprites/_${item}.scss`,
{
format: 'handlebars_based_template',
},
],
],
},
apiOptions: {
cssImageRef: `@/assets/sprites/${item}.png`,
spritesheet_info: {
name: item,
format: 'handlebars_based_template',
},
},
customTemplates: {
handlebars_based_template: './scss.template.hbs',
},
spritesmithOptions: {
algorithm: 'binary-tree',
padding: 10,
},
}),
)
})
return plugin
}