Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to override webpack.output.publicPath #44

Closed
fpsqdb opened this issue Mar 19, 2018 · 58 comments
Closed

How to override webpack.output.publicPath #44

fpsqdb opened this issue Mar 19, 2018 · 58 comments

Comments

@fpsqdb
Copy link

fpsqdb commented Mar 19, 2018

this is mini-css-extract-plugin config:

 plugins: [
        new MiniCssExtractPlugin({
            filename: "[name].css",
            publicPath: "../../../"
        })
    ]

but extract-text-webpack-plugin works as expect:

                use: extractStyle.extract({
                    fallback: "style-loader",
                    use: [cssLoader],
                    publicPath: '../../../'
                })
@alexander-akait
Copy link
Member

@fpsqdb can you describe use case?

@michael-ciniawsky
Copy link
Member

Yep, please provide a use case as this feature was one of the more 'problematic' ones of ETWP and we should very carefully consider all possible ways to solve this else wise (if possible) before adding options.publicPath back to this plugin

@itstrive
Copy link

itstrive commented Mar 23, 2018

I have the same problem, about background-image,
use url-loader limitoptions, when less than.

when npm run buld, it's result like this:

body{
    background:url(images/bdc52c4172ae217d3d74698729d4e935.jpg) no-repeat;
}

however, want't to be:

body{
    background:url(../images/bdc52c4172ae217d3d74698729d4e935.jpg) no-repeat;
}

How to config this?


webpack.config.js:

   module:{
        rules:[
            {
                test:/\.css$/,
                use:[
                    MiniCssExtractPlugin.loader,
                    'css-loader'
                ]
            },
            {
                test:/\.(png|jpg|gif)$/,
                use:[{
                    loader:'url-loader',
                    options:{
                        limit:50,  //it's important
                        outputPath:'images'
                    }
                }]
            }
        ]
    }

    ,plugins:[
        new MiniCssExtractPlugin({
            filename:'css/index.css' //it's important
        })
    ]

@7iomka
Copy link

7iomka commented Mar 23, 2018

@itstrive, Same issue!
My old sass config for webpack 3x: https://codepen.io/7iomka/pen/pLrbQM?editors=0010
My sass config modified for webpack 4: https://codepen.io/7iomka/pen/EEvyRK?editors=0010
want't to be:
from DEV in sass file I write:
background: url('../../assets/img/catalog/dcp-l5500dn/dcp-l5500dn-sm.jpg');

In webpack v3 we written something like this:

use: ExtractTextPlugin.extract({
                       publicPath: '../',
                       fallback: 'style-loader',
                       use: ...

In webpack 4 we don't have to pass publicPath: '../', in config, and
Expected result:
background: url(img/dcp-l5500dn-sm.jpg);
But real result is:
background: url(/img/dcp-l5500dn-sm.jpg);

Any help?
Thanks!

@acambas
Copy link

acambas commented Mar 26, 2018

+1 on this, i have the same issue

@alexander-akait
Copy link
Member

Use publicPath option for loader, look here https://github.com/webpack-contrib/mini-css-extract-plugin/blob/master/src/loader.js#L33. Feel free to ping me if it is not help

@troygibb
Copy link

@evilebottnawi same issue here. I'm using it like so:

plugins: [
  ...
  new MiniCssExtractPlugin({
    filename: '[name]-[contenthash].css',
    publicPath: './',
  }),
]

But I'm not seeing publicPath being attached to the query object. I.e.

const query = loaderUtils.getOptions(this) || {};

..is logging as an empty object. When I log the loader itself (MiniCssExtractPlugin.loader), it's just a relative path to the loader.js file, so I'm unclear on how to add the option there.

Thanks in advance!

@alexander-akait
Copy link
Member

@troygibb looks like feature, PR welcome 👍

@troygibb
Copy link

troygibb commented Apr 2, 2018

@evilebottnawi nice!! I'll try to get around to it in the next few weeks.

@victordidenko
Copy link

{
  test: /\.css$/,
  use: [{
    loader: MiniCssExtractPlugin.loader,
    options: {
      publicPath: '../'
    }
  }, {
    loader: 'css-loader'
  }]
}

looks like working for me

@wadouk
Copy link

wadouk commented Apr 6, 2018

To make it work:

  • use a publicPath in devserver
  • use a relative path on the plugin entry (not loader)
  • use a relative path in filename in output

@victordidenko
Copy link

victordidenko commented Apr 6, 2018

@wadouk I think you don't understand an issue.

If you use plugin like

new MiniCssExtractPlugin({
  filename:'css/index.css' //it's important
})

then all resources inside CSS file (like background images, fonts) become with wrong urls, they need to be prepended with "../", because your css file inside folder css/.

This is not publicPath on devserver, which completely different thing, and publicPath on plugin entry is not working, as reported by @troygibb . And you don't specify css files and resources as output entry, so you are wrong here too.

@kenjiqq
Copy link

kenjiqq commented Apr 10, 2018

Setting publicPath as @victordidenko mentioned worked for me as well

@yisar
Copy link

yisar commented May 3, 2018

+10086 on this, IIIIIIII have the same issue,too!

@alexander-akait
Copy link
Member

Somebody can create minimum reproducible test repo?

@victordidenko
Copy link

@evilebottnawi publicPath option for loader works as expected, as you said. See my comment above.

@alexander-akait
Copy link
Member

Hm what is problem, your want rewrite all assets in css using publicPath? It is impossible you should configure publishPath options for loader

@victordidenko
Copy link

That is exactly what my comment about. Do you follow conversation?

@alexander-akait
Copy link
Member

@victordidenko oh sorry, i look you post on email (very hard to read 😄 ). Yep close this issue because it should be set using loader. Thanks!

@alexander-akait
Copy link
Member

Also rewrite absolute URLs to relative inside your css should be done by css minificator.

@victordidenko
Copy link

I think it should be documented though. In readme there is nothing about possible options for loader.

@alexander-akait
Copy link
Member

@victordidenko yep, you are right

@alexander-akait alexander-akait changed the title How to override webpack.output.publicPath [docs]: how to override webpack.output.publicPath May 3, 2018
@ClaytonHunt
Copy link

ClaytonHunt commented Jun 5, 2018

I can't seem to get this to rewrite the urls correctly in webpack 4 anymore. in my config I have:

Plugins
------------
new MiniCssExtractPlugin({
  filename: "styles/[name].css"
})
...
Module Rules
----------------
{
      test: /\.(png|jpe?g|gif|svg|woff|woff2|ttf|eot|otf|ico|mp4)(\?\S*)?$/,
      use: {
        loader: 'file-loader',
        options: {
          outputPath: 'assets/',
          publicPath: '/',
          name: '[name].[ext]'
        }
      },
    }
...
{
      test: /\.(css|scss)$/,
      exclude: [helpers.projectRoot('node_modules')],
      include: [helpers.projectRoot('src/styles')],
      use: [{
        loader: MiniCssExtractPlugin.loader,
        options: {
          publicPath: '../assets'
        }
      }, {
        loader: 'css-loader'
      }, {
        loader: 'sass-loader'
      }]
    }

I want the urls in my scss to be changed to url(/assets/barlow-v1-latin-regular.woff) but everything is unchanged and from website root url('./web-theme/fonts/Barlow/barlow-v1-latin-regular.woff') is turning into http://lvh.me:3000/web-theme/fonts/Barlow/barlow-v1-latin-regular.woff

The actual assets are at the correct location, they just aren't loading because the url has the wrong path.

Any help would be greatly appreciated. I have tried several permutations of the suggested publicPath option for the loader but nothing seems to have any effect.

@ClaytonHunt
Copy link

For anyone who is having the same problem I was with a configuration similar to mine. Before, with Webpack 3, ExtractTextPlugin seemed to be updating the path for my assets. After, with Webpack 4, MiniCssExtractPlugin was not. The solution was to change the "publicPath" of the "file-loader" to point to my assets folder. I actually removed "publicPath" from the MiniCssExtractPlugin "options". Hope this can help someone else.

@xiaofan9
Copy link

xiaofan9 commented Jun 19, 2018

@ClaytonHunt
你的意思是,file-loader -> publicPath,MiniCssExtractPlugin -> publicPath 这两者参数多要设置?还是只需要设置file-loader 上设定publicPath的参数?
我目前只是修改了MiniCssExtractPlugin -> publicPath 这个参数,背景图引入路径已经被纠正!

You mean, file-loader.publicPath, MiniCssExtractPlugin.publicPath, both of which need to be set up. Or do you just need to set the publicPath parameter on the file-loader?
I currently only modify the MiniCssExtractPlugin.publicPath parameter, the background image introduction path has been corrected!

image

@alexander-akait
Copy link
Member

Please don't spam here, use publichPath option for mini-css-extract-plugin loader, if it is not help double check you publichPath on other loader, thanks!

@ClaytonHunt
Copy link

@xiaofan9 I only needed publicPath on file-loader.

@evilebottnawi I apologize if you think we are spamming, the fact is that the suggested fixes in this thread don't seem to fix the problem. I was originally presenting my config and asking for help. Once I got something working I thought it would be helpful to share what worked for me. I dealt with this issue for more than a day before I posted asking for help and then figured it out after 5 more days of tinkering. I wanted to save others the time I wasted.

@alexander-akait
Copy link
Member

alexander-akait commented Jun 19, 2018

@ClaytonHunt not for you, just notice about posts like "I have some problem" (without configuration or minimum reproducible test repo)

@xiaofan9
Copy link

@evilebottnawi I am sorry, I repeat the editor is afraid I am not fully expressed, I will pay attention next time.

@xiaofan9
Copy link

@ClaytonHunt Using only publicPath on file-loader, I don't know if you will encounter the problem that the src in img can't find the path?

@NithishReddy
Copy link

@evilebottnawi Hi i am using react-loadable for lazy loading js files and MiniCssExtractPlugin for lazyloading css files. now i want to load js and css files from different domains. how can achieve this problem?

@michael-ciniawsky michael-ciniawsky changed the title [docs]: how to override webpack.output.publicPath How to override webpack.output.publicPath Aug 24, 2018
gbirke added a commit to wmde/fundraising-application that referenced this issue Oct 6, 2018
This is an ongoing issue being discueed in
webpack-contrib/mini-css-extract-plugin#44

Overriding localPath as suggested in
webpack-contrib/mini-css-extract-plugin#44 (comment)
is a quick fix, but hardly seems right.
gbirke added a commit to wmde/fundraising-application that referenced this issue Oct 8, 2018
This is an ongoing issue being discueed in
webpack-contrib/mini-css-extract-plugin#44

Overriding localPath as suggested in
webpack-contrib/mini-css-extract-plugin#44 (comment)
is a quick fix, but hardly seems right.
tzhelyazkova pushed a commit to wmde/fundraising-application that referenced this issue Oct 12, 2018
This is an ongoing issue being discueed in
webpack-contrib/mini-css-extract-plugin#44

Overriding localPath as suggested in
webpack-contrib/mini-css-extract-plugin#44 (comment)
is a quick fix, but hardly seems right.
@tomatobybike
Copy link

+1 on this, i have the same issue

+2 on this, i have the same issue

@tomatobybike
Copy link

tomatobybike commented Jan 8, 2019

exports.cssLoaders = function(options) {
options = options || {}

const cssLoader = {
loader: 'css-loader',
options: {
sourceMap: options.sourceMap
}
}

const postcssLoader = {
loader: 'postcss-loader',
options: {
sourceMap: options.sourceMap
}
}

const MiniCssExtractPluginLoader = {
loader: MiniCssExtractPlugin.loader,
options: {
// you can specify a publicPath here
// by default it use publicPath in webpackOptions.output
publicPath: '../../'
}
}

// generate loader string to be used with extract text plugin
function generateLoaders(loader, loaderOptions) {
const loaders = []

// Extract CSS when that option is specified
// (which is the case during production build)
if (options.extract) {
  loaders.push(MiniCssExtractPluginLoader)
} else {
  loaders.push('vue-style-loader')
}

loaders.push(cssLoader)

if (options.usePostCSS) {
  loaders.push(postcssLoader)
}

if (loader) {
  loaders.push({
    loader: loader + '-loader',
    options: Object.assign({}, loaderOptions, {
      sourceMap: options.sourceMap
    })
  })
}
return loaders

}
// https://vue-loader.vuejs.org/en/configurations/extract-css.html
return {
css: generateLoaders(),
postcss: generateLoaders(),
less: generateLoaders('less'),
sass: generateLoaders('sass', {
indentedSyntax: true
}),
scss: generateLoaders('sass'),
stylus: generateLoaders('stylus'),
styl: generateLoaders('stylus')
}
}

@madsem
Copy link

madsem commented Mar 7, 2019

Using publicPath on loader didn't do anything for me, what solved it was to use context on the css-loader directly.

use: [
    cssHotLoad,
    MiniCssExtractPlugin.loader,
    {
        loader: 'css-loader',
        options: {
            importLoaders: 2,
            sourceMap: process.env.NODE_ENV === 'development',
            url: false,
            context: '../../',
        }
    },
]

@rbrisita
Copy link

I ended up with this configuration to get the output files to go where needed.

webpack.common.js:

    output: {
        path: helpers.root('public'),
        publicPath: '',
    },

webpack.prod.js:

    output: {
        filename: 'js/[hash].js',
        chunkFilename: 'js/[id].[hash].chunk.js'
    },
...
    plugins: [
        new MiniCSSExtractPlugin({
            filename: 'css/app.[hash].css'
        })
    ]

@alexander-akait
Copy link
Member

Here good answer #44 (comment), we will simplify this for webpack@5

@aaarichter
Copy link

hopefully @evilebottnawi

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests