1

I have test.css file which have a lot of dependencies, but I want to ignore all these dependencies in webpack 2.

test.css

.foo {
   background: url('../clear.png')
}

I tryed to use webpack.IgnorePlugin, but still have an error Module not found: Error: Can't resolve '../clear.png'

var webpack = require('webpack')
module.exports = {
    ...
    plugins: [
        new webpack.IgnorePlugin(/\.(jpe?g|png|gif|svg|ico|ttf|otf|eot|svg|woff(2)?)$/, /test.css/),
    ]
}

My whole webpack.js file is here.

1 Answer 1

1

Problem: as webpack will not understand relative path if image is not there.

Soltuion:

1. use absolute path
2. use options and set url to false
3. use raw loder as it will loder url as string

Here are the example use cases.

module:{
    rules:[
        {
            test: /\.s[ac]ss$/,
            use: ExtractTextPlugin.extract({
                //use: ['css-loader', ‘sass-loader’]        ,
                use:[
                    {
                        loader: ‘css-loader’,
                        options: {url: false}
                    },

                    ‘sass-loader’
                ]
            })
        }   
    ]
}

Example with raw-loader

module: {
    rules:[
        test: /\.s[ac]ss$/,
        use: ExtractTextPlugin.extract({
            use: ['raw-loader', ‘sass-loader’],
            fallback: ‘style-loader’
        })
    ]
}

also install raw-loader npm install raw-loader --save-dev

Want to use relative path then you should specify image urls too and use file-loader

it will move file and rename with some hash you can specify your own renaming options.

{

test: /\.(png|jpg|gif|svg|eot|ttf|woff|woff2)$/,
loader: ‘file-loader’, 
options: {
    name: ‘images/[name].[hash].[ext]’
}

}

Remember when using “options” field you have to use “loader” field instead of “use” field

So rule of the thumb is when using relative path make sure image exits there otherwise use absolute path.

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you. I'll try it now.
Yes. I don't have these img files in this relative path. I want just copy this urls without image files.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.