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
|
|
|
|
} from '../../lib/constants'
|
2017-06-06 22:32:02 +00:00
|
|
|
|
2017-10-30 14:57:35 +00:00
|
|
|
class PageChunkTemplatePlugin {
|
|
|
|
apply (chunkTemplate) {
|
|
|
|
chunkTemplate.plugin('render', function (modules, chunk) {
|
2018-06-14 17:30:14 +00:00
|
|
|
if (!IS_BUNDLED_PAGE_REGEX.test(chunk.name)) {
|
2017-10-30 14:57:35 +00:00
|
|
|
return modules
|
|
|
|
}
|
2017-10-20 06:02:04 +00:00
|
|
|
|
2018-06-14 17:30:14 +00:00
|
|
|
let routeName = ROUTE_NAME_REGEX.exec(chunk.name)[1]
|
2017-10-20 06:02:04 +00:00
|
|
|
|
2018-03-27 18:11:03 +00:00
|
|
|
// 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.
|
2017-10-30 14:57:35 +00:00
|
|
|
if (/^win/.test(process.platform)) {
|
|
|
|
routeName = routeName.replace(/\\/g, '/')
|
|
|
|
}
|
|
|
|
|
|
|
|
routeName = `/${routeName.replace(/(^|\/)index$/, '')}`
|
|
|
|
|
|
|
|
const source = new ConcatSource()
|
|
|
|
|
2018-05-25 12:23:44 +00:00
|
|
|
source.add(`__NEXT_REGISTER_PAGE('${routeName}', function() {
|
|
|
|
var comp =
|
2017-10-30 14:57:35 +00:00
|
|
|
`)
|
|
|
|
source.add(modules)
|
|
|
|
source.add(`
|
|
|
|
return { page: comp.default }
|
|
|
|
})
|
|
|
|
`)
|
|
|
|
|
|
|
|
return source
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default class PagesPlugin {
|
|
|
|
apply (compiler) {
|
|
|
|
compiler.plugin('compilation', (compilation) => {
|
|
|
|
compilation.chunkTemplate.apply(new PageChunkTemplatePlugin())
|
2017-04-18 04:18:43 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|