Skip to content

Commit e974077

Browse files
committed
fix: preserve function directory structure
1 parent ce4308a commit e974077

File tree

3 files changed

+8
-5
lines changed

3 files changed

+8
-5
lines changed

src/lib/helpers/copyDynamicImportChunks.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ const copyDynamicImportChunks = async (functionPath) => {
3131
// WP5 files are looked for two levels up (../../chunks) in runtime
3232
// This is a hack to make the file one level up i.e. with
3333
// nextPage/nextPage/index.js, the chunk is moved to outer nextPage in a /chunks dir
34-
const copyPath = join(functionPath, 'chunks')
34+
const copyPath = join(functionPath, 'nextPage', 'chunks')
3535
logTitle('💼 Copying WB5 dynamic import chunks to', copyPath)
3636
copySync(join(chunksPathWebpack5, file), join(copyPath, file), {
3737
overwrite: false,

src/lib/helpers/setupNetlifyFunctionForPage.js

+6-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
const { join } = require('path')
22

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

55
const { TEMPLATES_DIR, FUNCTION_TEMPLATE_PATH } = require('../config')
66

@@ -39,13 +39,16 @@ const setupNetlifyFunctionForPage = async ({ filePath, functionsPath, isApiPage
3939
await copyDynamicImportChunks(functionDirectory)
4040

4141
// Copy page to our custom path
42-
// (hack needed for dynamic imports, see: copyDynamicImportChunks.js)
43-
const nextPageCopyPath = join(functionDirectory, 'nextPage', 'nextPage', 'index.js')
42+
const nextPageCopyPath = join(functionDirectory, 'nextPage', filePath)
4443
const nextDistDir = await getNextDistDir()
44+
4545
copySync(join(nextDistDir, 'serverless', filePath), nextPageCopyPath, {
4646
overwrite: false,
4747
errorOnExist: true,
4848
})
49+
50+
// Write the import entry point
51+
await writeFile(join(functionDirectory, 'nextPage', 'index.js'), `module.exports = require("./${filePath}")`)
4952
}
5053

5154
module.exports = setupNetlifyFunctionForPage

src/tests/dynamicImports.test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ describe('next-on-netlify', () => {
4040

4141
test('copies chunk files to functions dir', () => {
4242
// This only tests WP5 as Next is -D installed @ 10.2.0
43-
const chunkFiles = readdirSync(join(functionsDir, 'next_index', 'chunks'))
43+
const chunkFiles = readdirSync(join(functionsDir, 'next_index', 'nextPage', 'chunks'))
4444
expect(chunkFiles.length).toEqual(1) // just the header
4545
})
4646
})

0 commit comments

Comments
 (0)