Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/webpack/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Symfony 使用网页包encore缩小图像_Symfony_Webpack_Symfony4_Webpack Encore - Fatal编程技术网

Symfony 使用网页包encore缩小图像

Symfony 使用网页包encore缩小图像,symfony,webpack,symfony4,webpack-encore,Symfony,Webpack,Symfony4,Webpack Encore,我一直在尝试为一个新项目配置Encore。CSS和JS可以正常工作,但我想在图像方面做得更进一步。到目前为止,我只在我的构建中创建了一个与相同架构相关的图像副本: //file: webpack.config.js let Encore = require('@symfony/webpack-encore'); Encore // the project directory where compiled assets will be stored .setOutputPath('pub

我一直在尝试为一个新项目配置Encore。CSS和JS可以正常工作,但我想在图像方面做得更进一步。到目前为止,我只在我的构建中创建了一个与相同架构相关的图像副本:

//file: webpack.config.js
let Encore = require('@symfony/webpack-encore');

Encore
// the project directory where compiled assets will be stored
    .setOutputPath('public/build/')
    // the public path used by the web server to access the previous directory
    .setPublicPath('/build')
    .enableSourceMaps(!Encore.isProduction())
    // uncomment to create hashed filenames (e.g. app.abc123.css)
    .enableVersioning(Encore.isProduction())

    // uncomment to define the assets of the project
    .addEntry('js/app', './assets/js/app.js')
    .addEntry('js/admin', './assets/js/admin.js')
    .addStyleEntry('css/app', './assets/css/app.scss')
    .addStyleEntry('css/bootstrap-datepicker', './node_modules/bootstrap-datepicker/dist/css/bootstrap-datepicker.css')

    //assures compatibility in case of same file name
    .configureFilenames({
        images: '[path][name].[hash:8].[ext]',
    })

    // uncomment if you use Sass/SCSS files
    .enableSassLoader(function (options) {
        // https://github.com/sass/node-sass#options
        options.includePaths = ['assets/compass_src'];
    })

    // uncomment for legacy applications that require $/jQuery as a global variable
    .autoProvidejQuery()

    // show OS notifications when builds finish/fail
    .enableBuildNotifications()
    .cleanupOutputBeforeBuild()
;

module.exports = Encore.getWebpackConfig();
我是这样管理我的图像的:

//file: app.js
const imagesContext = require.context('../images', true, /\.(png|jpg|jpeg|gif|ico|svg|webp)$/);
imagesContext.keys().forEach(imagesContext);
我尝试使用glob缩小(无损压缩)我的图像,但没有成功

如何缩小图像?

您可以使用该插件并将其添加到您的
webpack.config.js

.addLoader({
   test: /\.(gif|png|jpe?g|svg)$/i,
   loader: 'image-webpack-loader',
   options: {
      bypassOnDebug: true, //only minify during production
      plugins: [
          {removeTitle: true},
          {convertColors: {shorthex: false}},
          {convertPathData: false}
         ]
      },
  })
要管理图像,您可以创建一个
image.js
文件并加载图像:

require.context('../images', true, /\.jpe?g$|.png$|.svg$|.gif$/);
可以找到更多信息