import path from 'path'
import get from 'lodash/get'
import merge from 'lodash/merge'

const applyStaticLoaderFix = (wpConfig, sanityConfig) => {
  // We need to fix the public path, prefixing the server URL for assets such as
  // images to work in development mode (related to blob's generated by style-loader)
  const {httpHost, httpPort, staticPath, basePath} = sanityConfig
  return get(wpConfig, 'module.loaders', []).map(loader => {
    if (typeof loader.loader !== 'string' || !loader.loader.includes('file-loader')) {
      return loader
    }

    const staticDir = path.relative(basePath, staticPath)
    const rootPath = staticDir.replace(/^\.*\/|\/+$/g, '')

    return merge({}, loader, {
      query: {
        publicPath: `http://${httpHost}:${httpPort}/${rootPath}/`
      }
    })
  })
}

export default applyStaticLoaderFix

import path from 'path'
import get from 'lodash/get'
import merge from 'lodash/merge'

const applyStaticLoaderFix = (wpConfig, sanityConfig) => {
  // We need to fix the public path, prefixing the server URL for assets such as
  // images to work in development mode (related to blob's generated by style-loader)
  const {httpHost, httpPort, staticPath, basePath} = sanityConfig

  if (!staticPath || !basePath) {
    throw new Error('`staticPath` and `basePath` must be specified in order for the static loader fix to work')
  }

  return get(wpConfig, 'module.loaders', []).map(loader => {
    if (typeof loader.loader !== 'string' || !loader.loader.includes('file-loader')) {
      return loader
    }

    const staticDir = path.relative(basePath, staticPath)
    const rootPath = staticDir.replace(/^\.*\/|\/+$/g, '')

    return merge({}, loader, {
      query: {
        publicPath: `http://${httpHost}:${httpPort}/${rootPath}/`
      }
    })
  })
}

export default applyStaticLoaderFix

Throw error if basePath or staticPath is missing
[server] Throw error if basePath or staticPath is missing
