使用 webpack encore 缩小图像
Minify images using 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('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 缩小(无损压缩)我的图像,但没有成功。
如何缩小图像?
您可以使用 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
我一直在尝试为新项目配置 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 缩小(无损压缩)我的图像,但没有成功。
如何缩小图像?
您可以使用 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