1
0

public-path.ts 809 B

1234567891011121314151617181920212223
  1. // Dynamically set webpack's loading path depending on a meta header, in order
  2. // to share the same assets regardless of instance configuration.
  3. // See https://webpack.js.org/guides/public-path/#on-the-fly
  4. function removeOuterSlashes(string: string) {
  5. return string.replace(/^\/*/, '').replace(/\/*$/, '');
  6. }
  7. function formatPublicPath(host = '', path = '') {
  8. let formattedHost = removeOuterSlashes(host);
  9. if (formattedHost && !/^http/i.test(formattedHost)) {
  10. formattedHost = `//${formattedHost}`;
  11. }
  12. const formattedPath = removeOuterSlashes(path);
  13. return `${formattedHost}/${formattedPath}/`;
  14. }
  15. const cdnHost = document.querySelector<HTMLMetaElement>('meta[name=cdn-host]');
  16. __webpack_public_path__ = formatPublicPath(
  17. cdnHost ? cdnHost.content : '',
  18. process.env.PUBLIC_OUTPUT_PATH,
  19. );