我一直在尝试为新项目配置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来缩小(无损压缩)我的图像,但没有成功。
如何缩小图片?
答案 0 :(得分:2)
您可以使用image-webpack-loader插件并将其添加到您的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$/);
可以找到更多信息here