Skip to content

publicPath not doing anything #403

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

Closed
HoldYourWaffle opened this issue May 25, 2019 · 17 comments · Fixed by #603
Closed

publicPath not doing anything #403

HoldYourWaffle opened this issue May 25, 2019 · 17 comments · Fixed by #603

Comments

@HoldYourWaffle
Copy link

This might be related to #222.

  • Operating System: Windows 10
  • Node Version: v10.12.0
  • NPM Version: v6.9.0
  • webpack Version: v4.32.2
  • mini-css-extract-plugin Version: v0.6.0

Expected Behavior

publicPath should overwrite the output directory specified in output.path

Actual Behavior

Using publicPath doesn't seem to do anything. If it's defined as a function it does get called, but the return value is never used.

Code

module: {
	rules: [
		{
			test: /\.tsx?$/,
			use: 'ts-loader',
			exclude: /node_modules/
		},
		
		{
			test: /\.s?css$/,
			use: [
				{
					loader: MiniCssExtractPlugin.loader,
					options: {
						publicPath: 'any/path/absolute/or/relative'
					}
				},
				'css-loader'
			]
		}
	]
}

Full config

How Do We Reproduce?

By using the configuration above.

@alexander-akait
Copy link
Member

alexander-akait commented May 27, 2019

Please create minimum reproducible test repo, i think you have invalid configuration

@Noitidart
Copy link

I also have this issue my code is here:

{
    mode: process.env.NODE_ENV === 'production' ? 'production' : 'development',
    devtool: process.env.NODE_ENV === 'production' ? undefined : 'eval-source-map',
    entry: './assets/js/index.js',
    output:
    {
        filename: 'index.bundle.js',
        path: path.resolve(__dirname, '../.tmp/public/js'),
        publicPath: '/js/'
    },
    plugins: [
        new MiniCssExtractPlugin(
        {
            filename: 'main.bundle.css'
        }),
    ],
    module:
    {
        rules: [
        {
            test: /\.js$/,
            exclude: /node_modules|dependencies/,
            loader: 'babel-loader'
        },
        {
            test: /\.css$/,
            exclude: /node_modules/,
            use: [
            {
                loader: MiniCssExtractPlugin.loader
                options:
                {
                    publicPath: '/css/'
                }
            }, 'css-loader']
        },
        {
            test: /\.less$/,
            exclude: /node_modules/,
            use: [
            {
                loader: MiniCssExtractPlugin.loader
                options:
                {
                    publicPath: '/css/'
                }
            }, 'css-loader', 'less-loader']
        }]
    }
},

Any advice would be awesome.

@bohdan-basov
Copy link

Have the same issue! When file-loader has specified puplicPath option, publicPath in MiniCssExtractPlugin.loader does nothing, when I remove file-loader's publicPath everything works fine

@alexander-akait
Copy link
Member

Please create minimum reproducible test repo

@TheBoroer
Copy link

TheBoroer commented Sep 7, 2019

I just had the same problem and found a fix when I came across this comment

The readme says Options similar to the same options in webpackOptions.output which makes us believe we can have similar options (path, publicPath, etc.)....which is WRONG.

To get it to work, just add the relative path in the filename option like this:

new MiniCssExtractPlugin({
    filename: 'frontend/public/css/bundle.css'
}),

and make sure your webpack output options's path is set to this (i included publicPath and filename for completeness):

output: {
    path: path.join(__dirname),
    filename: 'bundle.js',
    publicPath: 'frontend/public/js/',
},

publicPath in MiniCssExtractPlugin options is still ignored....but at least this is a working workaround for now.

@Noitidart
Copy link

Noitidart commented Sep 7, 2019 via email

@bitfella
Copy link

bitfella commented Sep 19, 2019

Any update on this? publicPath is useless right now. It is executed, but its value is never used. If it is supposed to be used in a different way, please update docs with a clear example about its usage. Thanks.

@iDenisM
Copy link

iDenisM commented Sep 19, 2019

Also trying to use 'publicPath' but it doesn't work right now

@mbejda
Copy link

mbejda commented Sep 25, 2019

Not working for me either.

@tylerrrkd
Copy link

tylerrrkd commented Sep 29, 2019

My version is 0.8.0 and it's the latest one. Doc mini-css-extract-plugin shows that publicPath property, but it doesn't work for me either.

Configuration here.

{
  loader: MiniCssExtractPlugin.loader,
  options: {
    publicPath: '../../',
    // only enable hot in development
    hmr: mode === MODE_TYPE.DEV,
    // if hmr does not work, this is a forceful method.
    reloadAll: true,
  },
}

@tylerrrkd
Copy link

Ooops, my problem was solved accidentally... I found that my router missing a property named path (works like ouput.publicPath I think). After then, I try to change the value of loaders.options.publicPath and it works!

@Noitidart
Copy link

Noitidart commented Sep 29, 2019 via email

@alessiofachechi
Copy link

Despite I landed here for the same reason described by @HoldYourWaffle, now that I've better read the docs of both MiniCssExtractPlugin and webpack.output I think that there's a misunderstanding about the expected behavior of the plugin's publicPath option.

What we're currently expecting from the publicPath option is what in the case of webpack.output is done by path. But webpack.output allows for publicPath too. According to the docs the former is the output directory as an absolute path, while the latter specifies the public URL of the output directory when referenced in a browser.

In a few words what we wanted here is path, not publicPath. And the path behavior can be achieved through the plugin filename option, as already said in some comments above.

  output: {
    filename: 'main.js',
    path: path.resolve(__dirname, 'dist'),
  },
  plugins: [
    new MiniCssExtractPlugin({
      // path relative to the output.path
      // e.g. assets/style/main.css => dist/assets/style/main.css
      filename: 'assets/style/[name].css',
    })
  ],

@woutrbe
Copy link

woutrbe commented Oct 20, 2019

Thanks for the explanation @alessiofachechi, it took my quite a while to figure this out.
The documentation actually says // Options similar to the same options in webpackOptions.output, however it looks like path isn't supported. Using filename to also set the output directory isn't very clear.

Hopefully someone can update the documentation with an example.

@vsylvestre
Copy link

vsylvestre commented Oct 23, 2019

But wouldn't it be useful to have publicPath as well? I mean having the same behaviour as for webpack.output.publicPath.

@mykt0ngc0
Copy link

@alexander-akait
Copy link
Member

It is not bug, it is misleading, publicPath setup publicPath inside CSS (for url/etc), not for extracted file in runtime generated code, here example how you can achieve original request #403 (comment), I will update docs

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

Successfully merging a pull request may close this issue.