2018-06-16 17:23:02 +00:00
|
|
|
// @flow
|
2017-10-30 14:57:35 +00:00
|
|
|
import { ConcatSource } from 'webpack-sources'
|
2017-06-06 22:32:02 +00:00
|
|
|
import {
|
2018-06-14 17:30:14 +00:00
|
|
|
IS_BUNDLED_PAGE_REGEX,
|
|
|
|
ROUTE_NAME_REGEX
|
2018-06-16 17:23:02 +00:00
|
|
|
} from '../../../lib/constants'
|
2017-06-06 22:32:02 +00:00
|
|
|
|
2017-10-30 14:57:35 +00:00
|
|
|
export default class PagesPlugin {
|
2018-06-16 17:23:02 +00:00
|
|
|
apply (compiler: any) {
|
2018-07-24 09:24:40 +00:00
|
|
|
compiler.hooks.compilation.tap('PagesPlugin', (compilation) => {
|
|
|
|
compilation.chunkTemplate.hooks.render.tap('PagesPluginRenderPageRegister', (modules, chunk) => {
|
|
|
|
if (!IS_BUNDLED_PAGE_REGEX.test(chunk.name)) {
|
|
|
|
return modules
|
|
|
|
}
|
|
|
|
|
|
|
|
let routeName = ROUTE_NAME_REGEX.exec(chunk.name)[1]
|
|
|
|
|
|
|
|
// We need to convert \ into / when we are in windows
|
|
|
|
// to get the proper route name
|
|
|
|
// Here we need to do windows check because it's possible
|
|
|
|
// to have "\" in the filename in unix.
|
|
|
|
// Anyway if someone did that, he'll be having issues here.
|
|
|
|
// But that's something we cannot avoid.
|
|
|
|
if (/^win/.test(process.platform)) {
|
|
|
|
routeName = routeName.replace(/\\/g, '/')
|
|
|
|
}
|
|
|
|
|
|
|
|
routeName = `/${routeName.replace(/(^|\/)index$/, '')}`
|
|
|
|
|
|
|
|
const source = new ConcatSource()
|
|
|
|
|
|
|
|
source.add(`__NEXT_REGISTER_PAGE('${routeName}', function() {
|
|
|
|
var comp =
|
|
|
|
`)
|
|
|
|
source.add(modules)
|
|
|
|
source.add(`
|
|
|
|
return { page: comp.default }
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
|
|
|
|
return source
|
|
|
|
})
|
2017-04-18 04:18:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|