import { resolve } from 'path'
import { webpack } from '@healthline/six-million'
import getConfig from './config'

export default async function createCompiler (dir, { buildId = '-', dev = false } = {}) {
  dir = resolve(dir)
  const config = getConfig(dir)
  let sites = []
  if (config.sites) {
    config.sites.forEach(site => {
      sites.push({
        name: site,
        buildId: `${buildId}/${site}`,
        define: { 'process.env.SITE': JSON.stringify(site) }
      })
      sites.push({
        name: `${site}-legacy`,
        buildId: `${buildId}/${site}`,
        isLegacy: true,
        define: { 'process.env.SITE': JSON.stringify(site) }
      })
    })
  } else {
    sites = [ { name: 'default', buildId }, { name: 'default', buildId, isLegacy: true } ]
  }
  const mainJS = dev
    ? require.resolve('../../browser/client/next-dev') : require.resolve('../../browser/client/next')

  // Filter to a single site at dev time for reduced build overhead
  if (dev) {
    const sitename = process.env.SITE || sites[0].name
    sites = sites.filter(({ name }) => name.startsWith(sitename))
  }

  return webpack(sites, {
    mainJS: [`./pages/_error.js`, mainJS],

    async entries (entryPages, buildEntry) {
      const entries = {
        'pages/_error.js': buildEntry([])
      }
      for (const p of entryPages) {
        entries[p.replace(/^.*?\/pages\//, 'pages/').replace(/^(pages\/.*)\/index.js$/, '$1')] = buildEntry(p)
      }

      return entries
    },

    dir,
    dev,
    configModule: require.resolve('./config')
  })
}

@@ -1212,16 +1212,29 @@
 ildEntry
+, buildConfig
 ) %7B%0A    
@@ -1303,16 +1303,166 @@
       %7D%0A
+%0A      // Allow for per-site page filtering%0A      if (config.filterPages) %7B%0A        entryPages = config.filterPages(buildConfig, entryPages)%0A      %7D%0A%0A
       fo

Add optional filterPages config callback