【问题标题】:Webpack Encore delete my imagesWebpack Encore 删除我的图片
【发布时间】:2018-07-17 09:48:29
【问题描述】:

我遇到了 Webpack Encore 和 SCSS 的问题。

我的项目中有一个 SCSS 文件、一个图像和 Webpack Encore。但是当我用Webpack编译时,它会生成我原始文件logo.png的副本到logo.<hash>.png,正常,但是图像不显示,我什至无法打开它,图像被删除了。

另一方面,我使用 CopyWebpackPlugin 生成的所有图像都是可读的,但我无法从我的 SCSS 访问它...

结构

/assets
    /images
        logo.png
    /scss
        app.scss
/public
    /build
        /css
            app.<hash>.css
        /images
            logo.<hash>.png

app.scss

div {
    background-image: url('../images/logo.png');
}

webpack.config.js

var Encore = require('@symfony/webpack-encore');
var webpack = require('webpack');
const CopyWebpackPlugin = require('copy-webpack-plugin');

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

    .cleanupOutputBeforeBuild()

    .enableSourceMaps(!Encore.isProduction())

    // uncomment to create hashed filenames (e.g. app.abc123.css)
    .enableVersioning()

    // IMAGES
    .addPlugin(new CopyWebpackPlugin([
        {
            from: './assets/images',
            to: 'images',
            force: true
        }
    ]))

    // CSS
    .addStyleEntry('css/app', [
        './assets/scss/app.scss',
    ])

    // uncomment if you use Sass/SCSS files
    .enableSassLoader(function(options) {
        // https://github.com/sass/node-sass#options
    })

    .addLoader({loader: 'shebang-loader'})

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

    .enableBuildNotifications()
;

module.exports = Encore.getWebpackConfig();

谁能告诉我我做错了什么

【问题讨论】:

  • .addPlugin(new CopyWebpackPlugin([ 没有这个你能试试吗?我的任何项目中都没有这条线,我的图像被复制、散列和完美地使用。

标签: symfony webpack sass webpack-encore


【解决方案1】:

你可以删除

.cleanupOutputBeforeBuild()

或 添加类似的内容:

.cleanupOutputBeforeBuild(['static'], (options) => {
        options.verbose = true;
        options.root = __dirname;
        options.exclude = ['img'];
    })

【讨论】:

  • 这回答了问题,但这不是一个正确的解决方案。对 OP 问题的评论是正确答案。
猜你喜欢
  • 2020-03-17
  • 1970-01-01
  • 2020-10-27
  • 2020-01-29
  • 1970-01-01
  • 2019-04-28
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多