doupang9614
doupang9614
2019-03-27 13:46

如何使用Webpack Encore配置webpack-spritesmith?

已采纳

I'm working on a Symfony 3.4 project and I use Webpack Encore to manage my assets. Until then it works fine.

I'd like to use webpack-spritesmith package to build an image sprite. But when I run yarn encore dev command, I face the error:

{ Error: EISDIR: illegal operation on a directory, read errno: -21, code: 'EISDIR', syscall: 'read' }

Here is my project's structure:

/
|-assets
| |-js
| | |-app.js
| | ...
| |-images
| | |-new.png
| | |-open.png
| | |-save.png
| | |-sprites
| | | |-foo.png
| | | |-bar.png
| | | ...
| | ...
| |-scss
| | |-app.scss
| | ...
| ...
|-web
| |-build
| ...
|-webpack.config.js

I followed the documentation of the package to configure it but I'm not sure how to do with Symfony Encore overlay.

Here is my webpack.config.js file:

let Encore            = require('@symfony/webpack-encore');
let SpritesmithPlugin = require('webpack-spritesmith');
let path              = require('path');

Encore
    .setOutputPath('web/build/')
    .setPublicPath('/build')
    .addEntry('app', './assets/js/app.js')
    .enableSingleRuntimeChunk()
    .cleanupOutputBeforeBuild()
    .enableSourceMaps(!Encore.isProduction())
    .enableVersioning(Encore.isProduction())
    .enableBuildNotifications()
    .enableSassLoader()
    .enablePostCssLoader()
    .addLoader({
        test: /\.png$/,
        use: ['file-loader?name=images/[hash].[ext]']
    })
    .autoProvidejQuery()
    .addPlugin(new SpritesmithPlugin({
        src:        {
            cwd:  path.resolve(__dirname, './assets/images/sprites'),
            glob: '*.png'
        },
        target:     {
            image: path.resolve(__dirname, './assets/images/sprite.png'),
            css:   path.resolve(__dirname, './assets/scss/sprite.scss')
        },
        apiOptions: {
            cssImageRef: "~sprite.png"
        }
    }));

let config = Encore.getWebpackConfig();
config.resolve = {
    modules: ["node_modules", "./assets/images"]
};

module.exports = config;

I expect files to be generated as following:

  • /assets/images/sprite.png
  • /assets/scss/sprite.scss

Neither the sprite.png nor the sprite.scss file is created. However, some PNGs are generated in web/buid/images dir with hashed name.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dougan1465 dougan1465 2年前

    This was a reported issue that has been closed since.

    Note that sub-directories are now simply ignored, so keep all your images in a same directory.

    点赞 评论 复制链接分享

相关推荐