Skip to content
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

Open
adrianyanglinde opened this issue May 7, 2021 · 1 comment
Open

How to passed prefix to customTemplates #105

adrianyanglinde opened this issue May 7, 2021 · 1 comment

Comments

@adrianyanglinde
Copy link

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');

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
        },
        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;

}

@adrianyanglinde adrianyanglinde changed the title How to passed params to customTemplates How to passed prefix to customTemplates May 7, 2021
@mixtur
Copy link
Owner

mixtur commented 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
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants