1
- const fs = require ( 'fs' ) ;
2
- const { existsSync, readFileSync } = require ( 'fs' ) ;
3
- const path = require ( 'path' ) ;
4
- const { appendFile, readdir } = require ( 'fs' ) . promises ;
5
- const { hasFramework } = require ( '@netlify/framework-info' ) ;
6
- const nextOnNetlify = require ( 'next-on-netlify' ) ;
7
- const { PHASE_PRODUCTION_BUILD } = require ( 'next/constants' ) ;
8
- const { default : loadConfig } = require ( 'next/dist/next-server/server/config' ) ;
9
- const makeDir = require ( 'make-dir' ) ;
10
- const cpx = require ( 'cpx' ) ;
11
- const isStaticExportProject = require ( './helpers/isStaticExportProject' ) ;
1
+ const fs = require ( 'fs' )
2
+ const { existsSync, readFileSync } = require ( 'fs' )
3
+ const path = require ( 'path' )
4
+ const { appendFile, readdir } = require ( 'fs' ) . promises
5
+ const { hasFramework } = require ( '@netlify/framework-info' )
6
+ const nextOnNetlify = require ( 'next-on-netlify' )
7
+ const { PHASE_PRODUCTION_BUILD } = require ( 'next/constants' )
8
+ const { default : loadConfig } = require ( 'next/dist/next-server/server/config' )
9
+ const makeDir = require ( 'make-dir' )
10
+ const cpx = require ( 'cpx' )
11
+ const isStaticExportProject = require ( './helpers/isStaticExportProject' )
12
12
13
13
// * Helpful Plugin Context *
14
14
// - Between the prebuild and build steps, the project's build command is run
@@ -17,71 +17,73 @@ const isStaticExportProject = require('./helpers/isStaticExportProject');
17
17
module . exports = {
18
18
async onPreBuild ( { netlifyConfig, packageJson : { scripts = { } , dependencies = { } } , utils } ) {
19
19
if ( ! ( await hasFramework ( 'next' ) ) ) {
20
- return failBuild ( `This application does not use Next.js.` ) ;
20
+ return failBuild ( `This application does not use Next.js.` )
21
21
}
22
22
23
- const { build } = netlifyConfig ;
24
- const { failBuild } = utils . build ;
23
+ const { build } = netlifyConfig
24
+ const { failBuild } = utils . build
25
25
26
- // TO-DO: Post alpha, try to remove this workaround for missing deps in
27
- // the next-on-netlify function template
28
- await utils . run . command ( 'npm install next-on-netlify@latest' ) ;
26
+ // TO-DO: Post alpha, try to remove this workaround for missing deps in
27
+ // the next-on-netlify function template
28
+ await utils . run . command ( 'npm install next-on-netlify@latest' )
29
29
30
- if ( isStaticExportProject ( { build, scripts } ) ) {
31
- failBuild ( `** Static HTML export next.js projects do not require this plugin **` ) ;
32
- }
30
+ if ( isStaticExportProject ( { build, scripts } ) ) {
31
+ failBuild ( `** Static HTML export next.js projects do not require this plugin **` )
32
+ }
33
33
34
- // TO-DO: check scripts to make sure the app isn't manually running NoN
35
- // For now, we'll make it clear in the README
36
- // const isAlreadyUsingNextOnNetlify = Object.keys(dependencies).find((dep) => dep === 'next-on-netlify');
37
- // if (isAlreadyUsingNextOnNetlify) {
38
- // failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`);
39
- // }
34
+ // TO-DO: check scripts to make sure the app isn't manually running NoN
35
+ // For now, we'll make it clear in the README
36
+ // const isAlreadyUsingNextOnNetlify = Object.keys(dependencies).find((dep) => dep === 'next-on-netlify');
37
+ // if (isAlreadyUsingNextOnNetlify) {
38
+ // failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`);
39
+ // }
40
40
41
- const isFunctionsDirectoryCorrect = build && build . functions && build . functions === path . resolve ( 'out_functions' ) ;
42
- if ( ! isFunctionsDirectoryCorrect ) {
43
- // to do rephrase
44
- failBuild ( `You must designate a functions directory named "out_functions" in your netlify.toml or in your app's build settings on Netlify. See docs for more info: https://docs.netlify.com/functions/configure-and-deploy/#configure-the-functions-folder` ) ;
45
- }
41
+ const isFunctionsDirectoryCorrect = build && build . functions && build . functions === path . resolve ( 'out_functions' )
42
+ if ( ! isFunctionsDirectoryCorrect ) {
43
+ // to do rephrase
44
+ failBuild (
45
+ `You must designate a functions directory named "out_functions" in your netlify.toml or in your app's build settings on Netlify. See docs for more info: https://docs.netlify.com/functions/configure-and-deploy/#configure-the-functions-folder` ,
46
+ )
47
+ }
46
48
47
- if ( existsSync ( 'next.config.js' ) ) {
48
- // If the next config exists, fail build if target isnt in acceptableTargets
49
- const acceptableTargets = [ 'serverless' , 'experimental-serverless-trace' ] ;
50
- const nextConfig = loadConfig ( PHASE_PRODUCTION_BUILD , path . resolve ( '.' ) ) ;
51
- const isValidTarget = acceptableTargets . includes ( nextConfig . target ) ;
52
- if ( ! isValidTarget ) {
53
- failBuild ( `next.config.js must be one of: ${ acceptableTargets . join ( ', ' ) } ` ) ;
54
- }
55
- } else {
56
- // Create the next config file with target set to serverless by default
57
- const nextConfig = `
49
+ if ( existsSync ( 'next.config.js' ) ) {
50
+ // If the next config exists, fail build if target isnt in acceptableTargets
51
+ const acceptableTargets = [ 'serverless' , 'experimental-serverless-trace' ]
52
+ const nextConfig = loadConfig ( PHASE_PRODUCTION_BUILD , path . resolve ( '.' ) )
53
+ const isValidTarget = acceptableTargets . includes ( nextConfig . target )
54
+ if ( ! isValidTarget ) {
55
+ failBuild ( `next.config.js must be one of: ${ acceptableTargets . join ( ', ' ) } ` )
56
+ }
57
+ } else {
58
+ // Create the next config file with target set to serverless by default
59
+ const nextConfig = `
58
60
module.exports = {
59
61
target: 'serverless'
60
62
}
61
- ` ;
62
- await appendFile ( 'next.config.js' , nextConfig ) ;
63
- console . log ( `** Adding next.config.js with target set to 'serverless' **` ) ;
64
- }
63
+ `
64
+ await appendFile ( 'next.config.js' , nextConfig )
65
+ console . log ( `** Adding next.config.js with target set to 'serverless' **` )
66
+ }
65
67
} ,
66
68
async onBuild ( { constants } ) {
67
- console . log ( `** Running Next on Netlify package **` ) ;
68
- nextOnNetlify ( ) ;
69
+ console . log ( `** Running Next on Netlify package **` )
70
+ nextOnNetlify ( )
69
71
70
- // Next-on-netlify puts its files into out_functions and out_publish
71
- // Copy files from next-on-netlify's output to the right functions/publish dirs
72
+ // Next-on-netlify puts its files into out_functions and out_publish
73
+ // Copy files from next-on-netlify's output to the right functions/publish dirs
72
74
73
- // TO-DO: use FUNCTIONS_DIST when internal bug is fixed
74
- const { PUBLISH_DIR } = constants ;
75
- // if (!existsSync(FUNCTIONS_DIST)) {
76
- // await makeDir(FUNCTIONS_DIST);
77
- // }
78
- if ( ! existsSync ( PUBLISH_DIR ) ) {
79
- await makeDir ( PUBLISH_DIR ) ;
80
- }
75
+ // TO-DO: use FUNCTIONS_DIST when internal bug is fixed
76
+ const { PUBLISH_DIR } = constants
77
+ // if (!existsSync(FUNCTIONS_DIST)) {
78
+ // await makeDir(FUNCTIONS_DIST);
79
+ // }
80
+ if ( ! existsSync ( PUBLISH_DIR ) ) {
81
+ await makeDir ( PUBLISH_DIR )
82
+ }
81
83
82
- // TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict
83
- // with function names that next-on-netlify can generate
84
- // cpx.copySync('out_functions/**/*', FUNCTIONS_SRC);
85
- cpx . copySync ( 'out_publish/**/*' , PUBLISH_DIR ) ;
86
- }
84
+ // TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict
85
+ // with function names that next-on-netlify can generate
86
+ // cpx.copySync('out_functions/**/*', FUNCTIONS_SRC);
87
+ cpx . copySync ( 'out_publish/**/*' , PUBLISH_DIR )
88
+ } ,
87
89
}
0 commit comments