-
Notifications
You must be signed in to change notification settings - Fork 1.2k
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
Feature request: __webpack_public_path__
-like variable
#1441
Comments
Shouldn't this just be userland? I wouldn't want this, ever, for example. Might be as simple as: globalThis.__webpack_public_path__ = ASSET_PATH; With a |
I don't see how the code you mention would do anything unless ESBuild was generating code that uses const pathToImg = './assets/icon-a456dfe.svg'; Since this path is static, it's impossible to change its public path at runtime. Instead, ESBuild would need to generate something like: globalThis.__webpack_public_path__ = './assets';
const pathToImg = globalThis.__webpack_public_path__ + '/icon-a456dfe.svg'; The only other option would be for every single place that makes use of the imported images to add the public path themselves - which would go against the point of having a public path. |
Oh I see, sorry. I thought you were specifically asking for esbuild to output the webpack variable for compatibility purposes with imported code. Nvm :) |
__webpack_public_path__
__webpack_public_path__
-like variable
This can be implemented as a plugin, but it feels a bit clunky. There may be a better way, but this is what I came up with: let dynamicPublicPathPlugin = {
name: "dynamic-public-path",
setup(build) {
build.onResolve({ filter: /\.txt/ }, (args) => {
return {
path: path.join(args.resolveDir, args.path),
namespace:
args.pluginData === "via-dynamic-file-loader"
? "file"
: "dynamic-asset-path",
};
});
build.onLoad(
{ filter: /\.*/, namespace: "dynamic-asset-path" },
async (args) => {
return {
pluginData: "via-dynamic-file-loader",
contents: `
import assetPath from ${JSON.stringify(args.path)};
const path = __webpack_public_path__ + assetPath;
export default path;
`,
loader: "js",
};
}
);
},
}; A few thoughts:
{
publicPath: '"http://example.com/v1"',
// or
publicPath: '__webpack_public_path__',
} |
Public paths can also end up in CSS files. This proposal essentially means esbuild would have to completely change its approach to CSS bundling from what it currently does (CSS goes in separate files) to something Webpack-like (CSS is generated at run-time using JavaScript) so that the run-time public path variable is respected. That's not a change that I'm planning to make, so this proposal could be considered out of scope. |
CSS is much less of a problem than JS. The reason why By contrast, relative paths in CSS files are relative to the CSS file itself, so they'd already work even when switching from a CDN to another. Having |
just wanted to add that without this feature, for me, this great project is unusable. |
Would it? To me it sounds like |
@evanw Is there any chance this could be reevaluated if scoped exclusively to the JS world (like Webpack)? |
Normally, one would "extract" CSS in Webpack using mini-css-extract-plugin, which results in CSS loaded as CSS. I'm under the impression that a runtime-set Thought I'm not totally sure whether a feature like For JS-initated loading, it should be possible to determine the correct path based on |
another use case: |
We have the same requirement for something like |
@rtsao I'm having a hard time to fully understand your plugin and just wanted to ask, if you have successfully used your plugin approach before I dig into it? |
any news about this? |
End up here on a similar issue - using
|
➕ 1 on this 🙏 Being unable to set the public path at runtime means we have to break Rule III of the Twelve-Factor App and re-build our JS for each environment rather than having it simply adapt to the environment in which it's deployed. |
This is the one thing that actually blocks me from migrating from webpack to esbuild my entire app so I had to do compromises and split it, for now :) Would love to see it, my scenario is hosting app inside another app (i.e. SharePoint) which has many different site collections that are independent of each other and hold assets on their own( But I love the speed of esbuild, amazing. |
I have the same issue with retrieving the public path for my microfrontends. Recently I've used Webpack to bundle the applications and server them on the different ports. @doberkofler I think you had the same issue. Have you found the workaround? Because I don't believe it will be implemented. It's a big downside for ESBuild as for me. |
This would resolve relative to the module. |
We wanted to migrate from webpack to esbuild but we're also blocked because of this issue, as we're using different CDNs in different environments. Is there any plan to add this feature @evanw? Or do you perhaps have suggestions about how this could be achieved currently? |
This issue blocks us migrating from webpack to esbuild as well :( Any news about this feature? @evanw 🙏🏻 |
We'd be in favor of this feature, too. 🙏 |
My resources have been deployed on two CDNs, one server in China and one overseas. When accessing the website, determine the resource path based on the user's IP address. If the access address is overseas, use the overseas CDN. So we need a variable similar to webpack_public_cth. |
We also build our applications once and deploy them to multiple environments. This requires some sort of public path configuration. Not having this feature would result in having to build the applications for each environment. I hope there is a way to support this, as it prevents us from upgrading to a current version of angular. |
Adding to previous calls to have some kind of public path configuration. We have a need to decouple the domains from which resources are loaded and the page context in which they are executed. This is sth we can achieve with Webpack, and not with ESBuild. The limitation prevents us from migrating to the newer and better builder for our Angular components. |
Related: #459
In Webpack,
__webpack_public_path__
is a runtime variable that contains the public path used to load the assets. Unlike the staticpublicPath
configuration (or what's--asset-path
for esbuild), it can be freely modified by the program.Having this variable is handy when the public path is expected to change from one environment to another (for example when the same assets are meant to be uploaded inside both an internal + external cdn). Without it, one has to rebuild the assets for each environment.
The text was updated successfully, but these errors were encountered: