|
| 1 | +import { ConcatSource } from 'webpack-sources' |
1 | 2 | import { |
2 | 3 | IS_BUNDLED_PAGE, |
3 | 4 | MATCH_ROUTE_NAME |
4 | 5 | } from '../../utils' |
5 | 6 |
|
6 | 7 | export default class PagesPlugin { |
7 | 8 | apply (compiler) { |
8 | | - compiler.plugin('after-compile', (compilation, callback) => { |
9 | | - const pages = Object |
10 | | - .keys(compilation.namedChunks) |
11 | | - .map(key => compilation.namedChunks[key]) |
12 | | - .filter(chunk => IS_BUNDLED_PAGE.test(chunk.name)) |
13 | | - |
14 | | - pages.forEach((chunk) => { |
15 | | - const page = compilation.assets[chunk.name] |
16 | | - const pageName = MATCH_ROUTE_NAME.exec(chunk.name)[1] |
17 | | - let routeName = pageName |
18 | | - |
19 | | - // We need to convert \ into / when we are in windows |
20 | | - // to get the proper route name |
21 | | - // Here we need to do windows check because it's possible |
22 | | - // to have "\" in the filename in unix. |
23 | | - // Anyway if someone did that, he'll be having issues here. |
24 | | - // But that's something we cannot avoid. |
25 | | - if (/^win/.test(process.platform)) { |
26 | | - routeName = routeName.replace(/\\/g, '/') |
27 | | - } |
28 | | - |
29 | | - routeName = `/${routeName.replace(/(^|\/)index$/, '')}` |
30 | | - |
31 | | - const content = page.source() |
32 | | - const newContent = ` |
33 | | - window.__NEXT_REGISTER_PAGE('${routeName}', function() { |
34 | | - var comp = ${content} |
35 | | - return { page: comp.default } |
36 | | - }) |
37 | | - ` |
38 | | - // Replace the exisiting chunk with the new content |
39 | | - compilation.assets[chunk.name] = { |
40 | | - source: () => newContent, |
41 | | - size: () => newContent.length |
42 | | - } |
| 9 | + compiler.plugin('compilation', (compilation) => { |
| 10 | + compilation.plugin('optimize-chunk-assets', (chunks, callback) => { |
| 11 | + const pages = chunks.filter(chunk => IS_BUNDLED_PAGE.test(chunk.name)) |
| 12 | + |
| 13 | + pages.forEach((chunk) => { |
| 14 | + const pageName = MATCH_ROUTE_NAME.exec(chunk.name)[1] |
| 15 | + let routeName = pageName |
| 16 | + |
| 17 | + // We need to convert \ into / when we are in windows |
| 18 | + // to get the proper route name |
| 19 | + // Here we need to do windows check because it's possible |
| 20 | + // to have "\" in the filename in unix. |
| 21 | + // Anyway if someone did that, he'll be having issues here. |
| 22 | + // But that's something we cannot avoid. |
| 23 | + if (/^win/.test(process.platform)) { |
| 24 | + routeName = routeName.replace(/\\/g, '/') |
| 25 | + } |
| 26 | + |
| 27 | + routeName = `/${routeName.replace(/(^|\/)index$/, '')}` |
| 28 | + |
| 29 | + // Replace the exisiting chunk with the new content |
| 30 | + const asset = compilation.assets[chunk.name] |
| 31 | + if (!asset) return |
| 32 | + |
| 33 | + const concat = new ConcatSource() |
| 34 | + |
| 35 | + concat.add(` |
| 36 | + __NEXT_REGISTER_PAGE('${routeName}', function() { |
| 37 | + var comp = |
| 38 | + `) |
| 39 | + concat.add(asset) |
| 40 | + concat.add(` |
| 41 | + return { page: comp.default } |
| 42 | + }) |
| 43 | + `) |
| 44 | + |
| 45 | + // Replace the exisiting chunk with the new content |
| 46 | + compilation.assets[chunk.name] = concat |
| 47 | + }) |
| 48 | + |
| 49 | + callback() |
43 | 50 | }) |
44 | | - callback() |
45 | 51 | }) |
46 | 52 | } |
47 | 53 | } |
0 commit comments