|
1 | 1 | const { join } = require('path')
|
2 | 2 |
|
3 |
| -const { copySync, readdirSync } = require('fs-extra') |
| 3 | +const { copySync, existsSync, readdirSync } = require('fs-extra') |
4 | 4 |
|
5 | 5 | const getNextDistDir = require('./getNextDistDir')
|
6 | 6 | const { logTitle } = require('./logger')
|
7 | 7 |
|
8 | 8 | // Check if there are dynamic import chunks and copy to the necessary function dir
|
9 | 9 | const copyDynamicImportChunks = async (functionPath) => {
|
10 | 10 | const nextDistDir = await getNextDistDir()
|
11 |
| - const chunksPath = join(nextDistDir, 'serverless') |
12 |
| - const files = readdirSync(chunksPath) |
13 |
| - const chunkRegex = new RegExp(/^(\.?[-$~\w]+)+\.js$/g) |
| 11 | + const chunksPathWebpack4 = join(nextDistDir, 'serverless') |
| 12 | + const filesWP4 = readdirSync(chunksPathWebpack4) |
| 13 | + const chunkRegexWP4 = new RegExp(/^(\.?[-$~\w]+)+\.js$/g) |
14 | 14 | const excludeFiles = new Set(['init-server.js.js', 'on-error-server.js.js'])
|
15 |
| - files.forEach((file) => { |
16 |
| - if (!excludeFiles.has(file) && chunkRegex.test(file)) { |
17 |
| - logTitle('💼 Copying dynamic import chunks to', functionPath) |
18 |
| - copySync(join(chunksPath, file), join(functionPath, file), { |
| 15 | + filesWP4.forEach((file) => { |
| 16 | + if (!excludeFiles.has(file) && chunkRegexWP4.test(file)) { |
| 17 | + // WP4 files are looked for one level up (../) in runtime |
| 18 | + // This is a hack to make the file one level up i.e. with |
| 19 | + // nextPage/nextPage/index.js, the chunk is moved to the inner nextPage |
| 20 | + const copyPath = join(functionPath, 'nextPage') |
| 21 | + logTitle('💼 Copying WP4 dynamic import chunks to', copyPath) |
| 22 | + copySync(join(chunksPathWebpack4, file), join(copyPath, file), { |
19 | 23 | overwrite: false,
|
20 | 24 | errorOnExist: true,
|
21 | 25 | })
|
22 | 26 | }
|
23 | 27 | })
|
| 28 | + const chunksPathWebpack5 = join(nextDistDir, 'serverless', 'chunks') |
| 29 | + const filesWP5 = existsSync(chunksPathWebpack5) ? readdirSync(chunksPathWebpack5) : [] |
| 30 | + filesWP5.forEach((file) => { |
| 31 | + // WP5 files are looked for two levels up (../../chunks) in runtime |
| 32 | + // This is a hack to make the file one level up i.e. with |
| 33 | + // nextPage/nextPage/index.js, the chunk is moved to outer nextPage in a /chunks dir |
| 34 | + const copyPath = join(functionPath, 'chunks') |
| 35 | + logTitle('💼 Copying WB5 dynamic import chunks to', copyPath) |
| 36 | + copySync(join(chunksPathWebpack5, file), join(copyPath, file), { |
| 37 | + overwrite: false, |
| 38 | + errorOnExist: true, |
| 39 | + }) |
| 40 | + }) |
24 | 41 | }
|
25 | 42 |
|
26 | 43 | module.exports = copyDynamicImportChunks
|
0 commit comments