vue.config.js
2.38 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
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
const SpritesmithPlugin = require("webpack-spritesmith");
const spritesmithFunction = (data) => {
function toRpx (val) {
return `calc(100vmin * (1173 / 768) * ${val / 2 / 2346})`
}
let { spritesheet, sprites } = data,
spritesheetWidth = toRpx(spritesheet.width),
spritesheetHeight = toRpx(spritesheet.height)
let shared = `.sprite-icon { background-image: url(${data.sprites[0].image
});background-size: ${spritesheetWidth} ${spritesheetHeight};}`;
let perSprite = sprites
.map((sprite) => {
let { name, width, height, offset_x, offset_y } = sprite
width = toRpx(width),
height = toRpx(height),
offset_x = toRpx(offset_x),
offset_y = toRpx(offset_y)
return `.icon-${name} { width: ${width}; height: ${height};
background-position: ${offset_x} ${offset_y};}`;
})
.join("\n");
// let shared = `.sprite-icon { background-image: url(${
// data.sprites[0].image
// });background-size: ${spritesheet.width / 2}rpx ${
// spritesheet.height / 2
// }rpx;}`;
// let perSprite = sprites
// .map((sprite) => {
// let { name, width, height, offset_x, offset_y } = sprite;
// return `.icon-${name} { width: ${width / 2}rpx; height: ${
// height / 2
// }rpx; background-position: ${offset_x / 2}rpx ${offset_y / 2}rpx;}`;
// })
// .join("\n");
return `${shared}\n${perSprite}`;
};
const path = require("path");
module.exports = {
devServer: {
disableHostCheck: true,
},
configureWebpack: {
plugins: [
new SpritesmithPlugin({
// 引入图片文件夹
src: {
cwd: path.resolve(__dirname, "./assets/spritesmith"),
glob: ["*.png", "*.jpg"],
},
// 生成图片的路径
target: {
image: path.resolve(__dirname, "./assets/images/sprite.png"),
// 生成SCSS
css: [
[
path.resolve(__dirname, "./assets/css/sprite.scss"),
{
format: "function_based_template",
},
],
],
},
customTemplates: {
function_based_template: spritesmithFunction,
},
//background引用路径
apiOptions: {
cssImageRef: "@/assets/images/sprite.png",
},
// 每个图片间距
spritesmithOptions: {
padding: 30,
},
}),
],
},
};