-
Notifications
You must be signed in to change notification settings - Fork 56
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to passed prefix to customTemplates #105
Comments
adrianyanglinde
changed the title
How to passed params to customTemplates
How to passed prefix to customTemplates
May 7, 2021
Wrap your template in a closure I guess? const templateFunction = prefix => data => {
console.log(data.sprites[0].image);
var shared = `@mixin sprite_${prefix} { background-image: url(I); background-size: SWpx SHpx; }`
.replace('I', data.sprites[0].image)
.replace('SW', data.spritesheet.width)
.replace('SH', data.spritesheet.height);
var perSprite = data.sprites
.map(function(sprite) {
return '@mixin N { width: Wpx; height: Hpx;background-position: Xpx Ypx; }'
.replace('N', sprite.name)
.replace('W', sprite.width)
.replace('H', sprite.height)
.replace('X', sprite.offset_x)
.replace('Y', sprite.offset_y);
})
.join('\n');
return shared + '\n' + perSprite;
};
function getWebpackSpriteConfig() {
const filesName = getAllDirs(getDevPath('./images/'));
const plugins = filesName.map((fileName) => {
const prefix = /^sprite_(\w+)/.exec(fileName)[1];
return new SpritesmithPlugin({
src: {
cwd: getDevPath(`./images/sprite_${prefix}`),
glob: '*.png'
},
target: {
image: getDevPath(`./images/sprite_${prefix}.png`),
css: [
[
getDevPath(`./sass/sprite_${prefix}.scss`),
{
format: 'function_based_template'
}
]
]
},
customTemplates: {
function_based_template: templateFunction(prefix)
},
apiOptions: {
cssImageRef: `../images/sprite_${prefix}.png?v=` + Date.parse(new Date())
},
spritesmithOptions: {
algorithm: 'binary-tree',
padding: 8
}
});
});
const config = {
mode: 'development',
plugins: [...plugins]
};
return config;
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
const templateFunction = function (data) {
console.log(data.sprites[0].image);
var shared =
@mixin sprite_${prefix} { background-image: url(I); background-size: SWpx SHpx; }
.replace('I', data.sprites[0].image)
.replace('SW', data.spritesheet.width)
.replace('SH', data.spritesheet.height);
var perSprite = data.sprites
.map(function (sprite) {
return '@mixin N { width: Wpx; height: Hpx;background-position: Xpx Ypx; }'
.replace('N', sprite.name)
.replace('W', sprite.width)
.replace('H', sprite.height)
.replace('X', sprite.offset_x)
.replace('Y', sprite.offset_y);
})
.join('\n');
};
function getWebpackSpriteConfig() {
const filesName = getAllDirs(getDevPath('./images/'));
const plugins = filesName.map((fileName) => {
const prefix = /^sprite_(\w+)/.exec(fileName)[1];
}
The text was updated successfully, but these errors were encountered: