2017-10-30 14:57:35 +00:00
|
|
|
import { ConcatSource } from 'webpack-sources'
|
2017-06-06 22:32:02 +00:00
|
|
|
import {
|
|
|
|
IS_BUNDLED_PAGE,
|
|
|
|
MATCH_ROUTE_NAME
|
|
|
|
} from '../../utils'
|
|
|
|
|
2017-10-30 14:57:35 +00:00
|
|
|
class PageChunkTemplatePlugin {
|
|
|
|
apply (chunkTemplate) {
|
|
|
|
chunkTemplate.plugin('render', function (modules, chunk) {
|
|
|
|
if (!IS_BUNDLED_PAGE.test(chunk.name)) {
|
|
|
|
return modules
|
|
|
|
}
|
2017-10-20 06:02:04 +00:00
|
|
|
|
2017-10-30 14:57:35 +00:00
|
|
|
let routeName = MATCH_ROUTE_NAME.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()
|
|
|
|
|
|
|
|
source.add(`
|
|
|
|
__NEXT_REGISTER_PAGE('${routeName}', function() {
|
|
|
|
var comp =
|
|
|
|
`)
|
|
|
|
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
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|