Webpack [url/file-loader] is not resolving the Relative Path of URL

前端 未结 3 1555
梦毁少年i
梦毁少年i 2021-02-05 20:19

I am facing a problem in Webpack regarding Relative Path. Let me try to explain the scenario :

I have 2 separate project in Workspace directory :

相关标签:
3条回答
  • 2021-02-05 21:05

    you need to set the publicPath as a relative path to get the relative path in file-loader.

    0 讨论(0)
  • 2021-02-05 21:16

    It seems like it's css-loader fault and the way it resolves paths in @import and url(). It tries to resolve all paths — even those from imported stylesheets — relative to the main CSS file — which in your case is /Project_B/src/assets/stylesheets/index.scss.

    Don't cry! There's a solution!

    Maybe it's not perfect, but it's the best one I came with so far.

    Create a global variable $assetsPath holding a path to assets relative to the current stylesheet. Then prepend this variable to all your url() values.

    In your /Project_A/assets/stylesheets/index.scss you'd write:

    /*/ Using !default we can override this variable even before the following line: /*/
    $assetsPath: '../' !default;
    
    .container {
        /*/ ... /*/
        .content-wrapper {
            /*/ ... /*/
            background-image: url($assetsPath + "images/content-bg.jpg");
        }
    }
    

    In your /Project_B/src/assets/stylesheets/index.scss you'd write:

    /*/ The following variable will override $assetsPath defined in the imported file: /*/
    $assetsPath: '../../../../Project_A/assets/';
    
    /*/ Import Project A SCSS [Common Varibles, Classes, Styling etc] /*/
    @import "../../../../Project_A/assets/stylesheets/index";
    

    The Aftermath

    If you bundle Project-A with Gulp it's gonna see Project-A's code as:

            /*/ ... /*/
            background-image: url("../images/content-bg.jpg");
    

    Although, when you bundle Project-B with Webpack it's gonna see Project-A's code as:

            /*/ ... /*/
            background-image: url("../../../../Project_A/assets/images/content-bg.jpg");
    

    Therefore, you are saved.

    Most definitely I'll look closer at this issue. All of this could be avoided if url-loader would respect a path in the @import statement and apply it to referenced assets accordingly. Either I'm missing something or it should be considered as a bug.

    I hope you have a wonderful day!
    ~Wiktor

    0 讨论(0)
  • 2021-02-05 21:18

    So, finally after so much struggle, got a proper SOLUTION.

    It turns out to be an issue with CSS-loader i.e it is not able to resolve the URL with respective to current file.

    Using resolve-url-loader solved this problem. https://www.npmjs.com/package/resolve-url-loader

     // Old Loader Config in Webpack-entry
     loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap!sass-loader?sourceMap')
    
     // New [Fixed] Loader Config in Webpack-entry
     loader: ExtractTextPlugin.extract('style-loader', 'css-loader?sourceMap!resolve-url-loader!sass-loader?sourceMap')
    

    Here is updated Code-Repo with solution : https://github.com/raviroshan/webpack-build-issue

    Note : Don't omit -loader Your Webpack.config.js should always use the long-form of the loader name (i.e. the -loader suffix).

    There is another package called resolve-url which Webpack can confuse with resolve-url-loader.

    0 讨论(0)
提交回复
热议问题