1
0

public-path.js 814 B

123456789101112131415161718192021
  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) {
  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('meta[name=cdn-host]');
  16. // eslint-disable-next-line no-undef
  17. __webpack_public_path__ = formatPublicPath(cdnHost ? cdnHost.content : '', process.env.PUBLIC_OUTPUT_PATH);