Skip to content

Commit 105b99e

Browse files
committed
fix: dynamic imports with webpack 5
1 parent c02b0dd commit 105b99e

File tree

5 files changed

+34
-23
lines changed

5 files changed

+34
-23
lines changed

Diff for: package-lock.json

+1-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Diff for: package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@
7777
"husky": "^4.3.0",
7878
"jest": "^26.6.1",
7979
"netlify-cli": "^3.29.5",
80-
"next": "^10.0.8",
80+
"next": "^10.2.0",
8181
"path-exists": "^4.0.0",
8282
"prettier": "^2.1.2",
8383
"react": "^17.0.1",

Diff for: src/lib/helpers/copyDynamicImportChunks.js

+25-8
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,43 @@
11
const { join } = require('path')
22

3-
const { copySync, readdirSync } = require('fs-extra')
3+
const { copySync, existsSync, readdirSync } = require('fs-extra')
44

55
const getNextDistDir = require('./getNextDistDir')
66
const { logTitle } = require('./logger')
77

88
// Check if there are dynamic import chunks and copy to the necessary function dir
99
const copyDynamicImportChunks = async (functionPath) => {
1010
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)
1414
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), {
1923
overwrite: false,
2024
errorOnExist: true,
2125
})
2226
}
2327
})
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+
})
2441
}
2542

2643
module.exports = copyDynamicImportChunks

Diff for: src/lib/helpers/setupNetlifyFunctionForPage.js

+3-2
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ const setupNetlifyFunctionForPage = async ({ filePath, functionsPath, isApiPage
3838
// Copy any dynamic import chunks
3939
await copyDynamicImportChunks(functionDirectory)
4040

41-
// Copy page
42-
const nextPageCopyPath = join(functionDirectory, 'nextPage', 'index.js')
41+
// Copy page to our custom path
42+
// (hack needed for dynamic imports, see: copyDynamicImportChunks.js)
43+
const nextPageCopyPath = join(functionDirectory, 'nextPage', 'nextPage', 'index.js')
4344
const nextDistDir = await getNextDistDir()
4445
copySync(join(nextDistDir, 'serverless', filePath), nextPageCopyPath, {
4546
overwrite: false,

Diff for: src/tests/dynamicImports.test.js

+4-8
Original file line numberDiff line numberDiff line change
@@ -38,13 +38,9 @@ describe('next-on-netlify', () => {
3838
expect(buildOutput).toMatch('Success! All done!')
3939
})
4040

41-
test('copies chunk files to ', () => {
42-
const functionFiles = readdirSync(join(functionsDir, 'next_index'))
43-
const chunkRegex = new RegExp(/(\.?[-_$~A-Z0-9a-z]+){1,}\.js$/g)
44-
let chunkFileExists
45-
functionFiles.forEach((file) => {
46-
chunkFileExists = chunkRegex.test(file)
47-
})
48-
expect(chunkFileExists).toBe(true)
41+
test('copies chunk files to functions dir', () => {
42+
// This only tests WP5 as Next is -D installed @ 10.2.0
43+
const chunkFiles = readdirSync(join(functionsDir, 'next_index', 'chunks'))
44+
expect(chunkFiles.length).toEqual(1) // just the header
4945
})
5046
})

0 commit comments

Comments
 (0)