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

__webpack_public_path__ does not work? #6024

Closed
xsteadybcgo opened this issue Dec 12, 2018 · 6 comments
Closed

__webpack_public_path__ does not work? #6024

xsteadybcgo opened this issue Dec 12, 2018 · 6 comments

Comments

@xsteadybcgo
Copy link

xsteadybcgo commented Dec 12, 2018

Is this a bug report?

yes

I want to set html files assetsPath in dist directory produced by htmlwebpackplugin at runtime, so I tried to figure out __webpack_public_path__ = "http://cdn.mydomain.com instead of setting publicUrl , but it doesnot work at all。 how can i set publicPath at runtime ? does not webpack_public_path support in CRA?

@ianschmitz
Copy link
Contributor

Can you be more specific about what you're trying to accomplish? It sounds like PUBLIC_URL is really what you're after.

@xsteadybcgo
Copy link
Author

xsteadybcgo commented Dec 12, 2018

Can you be more specific about what you're trying to accomplish? It sounds like PUBLIC_URL is really what you're after.

I konw the PUBLIC_URL works well when i want to set cdn path to script & link tags in html files produced by htmlwebpackplugin,the reason why i change to use webpack_public_path is that:
As webpack doc says:

In cases where the eventual publicPath of output files isn't known at compile time, it can be left blank and set dynamically at runtime via the webpack_public_path variable in the entry point file: __webpack_public_path__ = myRuntimePublicPath;

Why i want webpack_public_path instead of publicPath( PUBLIC_URL ) is that i want to set dynamically at runtime 。 I enconter a react-snap#issue-182410449
using react-snap 。 The vue project can hack that by using webpack_public_path ,I was depressed that it doen not work in CRA。

my code example :

src/index.html

`
import './publicPath'
import './publicPath'
import 'react-app-polyfill/ie9'
import 'raf/polyfill';
import React from 'react';
...

src/publicPath.js

// eslint-disable-next-line __webpack_public_path__ = "http://tcdn.2dubao.com/website"

but the output file is not in my expectation :

<html><link href="/static/css/6.954517dd.chunk.css" rel="stylesheet"></html>

i expect it can be build like:

<html><link href="http://tcdn.2dubao.com/website/static/css/6.954517dd.chunk.css" rel="stylesheet"></html>

@ianschmitz
Copy link
Contributor

In your example are you showing us the outputted index.html? Or are you showing a lazy loaded css chunk that is injected at runtime?

@xsteadybcgo
Copy link
Author

In your example are you showing us the outputted index.html? Or are you showing a lazy loaded css chunk that is injected at runtime?

Here is my repository ,npm run prod && npm run snapbuild , then checkout the build directorty, the script and link tags in html files does not contain my cdn path (defined in /src/publicPath.js using __webpack_public_path__ )

@ianschmitz
Copy link
Contributor

ianschmitz commented Dec 13, 2018

Gotcha. Thanks for the repository.

I think there may be some confusion between publicPath in the webpack config, versus __webpack_public_path__ which is used at runtime. I'll do my best to clear things up based on my knowledge of the two.

publicPath is used during the webpack build to help construct URLs to your assets, whether it be for loading your lazy loaded chunks via dynamic import(), or via HtmlWebpackPlugin to configure the urls to your entrypoint .js and .css for example. If you were to set PUBLIC_URL in CRA, it will internally configure the webpack publicPath, such that if you gave it an absolute URL to a CDN for example, HtmlWebpackPlugin would use this absolute URL to prefix our .js and .css files in index.html.

__webpack_public_path__ allows you to configure the webpack asset loader to tweak the publicPath at runtime. So for example if you left publicPath alone, CRA will default to the root of the host (/). So requests to load a lazy chunk would be loaded from /static/js/foo.abc123.js for example. However you can set __webpack_public_path__ in the entry point of your app, so that from that time forward in your application lifecycle, any asset requests would use __webpack_public_path__ as the prefix. So if you set __webpack_public_path__ = https://bar.com, then the chunk would be requested from https://bar.com/static/js/foo.abc123.js. You're essentially telling the webpack asset loader:

Hey i know i told you during the build that you should prefix requests with /, but i'm telling you that from this point forward instead prefix the requests with https://bar.com

Does that help clear things up?

@xsteadybcgo
Copy link
Author

@ianschmitz thank you for you patience,it was not a CRA issue but my misunderstanding , now i can fix my project by adding a webpack plugin。 thanks a lot , thank you !!! 😄

@lock lock bot locked and limited conversation to collaborators Jan 8, 2019
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants