Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get the webpack publicPath

I'm using the following code add icons to the MdIconRegistry:

constructor(iconRegistry: MdIconRegistry, sanitizer: DomSanitizer) {
  iconRegistry.addSvgIconSet(
    sanitizer.bypassSecurityTrustResourceUrl('/assets/svg-sprite-action-symbol.svg'));
  iconRegistry.addSvgIconSet(
    sanitizer.bypassSecurityTrustResourceUrl('/assets/svg-sprite-content-symbol.svg'));
  iconRegistry.addSvgIconSet(
    sanitizer.bypassSecurityTrustResourceUrl('/assets/svg-sprite-navigation-symbol.svg'));
}

This unfortunately breaks when using ng [build|serve] --deploy-url=/public/ (In production I would like to deploy the ng build output to a CDN)

Is there a way that I can access the webpack publicPath variable from my angular component so that I can specify the urls for the svg icons relative to that?

I had a look in the ng build output, and I can see the variable is set in inline.bundle.js, but I'm not sure how to correctly access it.

/******/    __webpack_require__.p = "/public/";
like image 381
Gary van der Merwe Avatar asked Aug 01 '17 10:08

Gary van der Merwe


2 Answers

You can get it from __webpack_public_path__ global variable.

like image 152
Metinerk Avatar answered Nov 10 '22 00:11

Metinerk


if you have access to webpack-configration file then you can easely get the public path

suppose you have webpack.config.js in your project root and you have component file in /src/component/mycomponent.js

and your webpack.config.js is something like this

module.exports = {
.
.
.
 output: {
   path: "somthing",
   filename: "somthing",
   publicPath: "something"
 }
.
.
.
}

then /src/component/mycomponent.js would be like

import webpackConfig from "../../webpack.config"

const publickPath = webpackConfig.output.publicPath

webpack config file is just like another javascript file you can import content from it

like image 22
Tripurari Shankar Avatar answered Nov 10 '22 01:11

Tripurari Shankar