|
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
|
15 | 15 | // - Between the build and postbuild steps, any functions are bundled
|
16 | 16 |
|
17 | 17 | module.exports = {
|
18 | 18 | async onPreBuild({ constants, netlifyConfig, utils }) {
|
19 |
| - if (!(await hasFramework('next'))) { |
| 19 | + if (!(await hasFramework("next"))) { |
20 | 20 | return failBuild(`This application does not use Next.js.`);
|
21 | 21 | }
|
22 | 22 |
|
23 | 23 | const { build } = netlifyConfig;
|
24 | 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 |
| - // Require the project's package.json for access to its scripts |
31 |
| - // and dependencies in order to check existing project configuration |
32 |
| - let packageJson; |
33 |
| - if (existsSync(path.resolve(constants.PUBLISH_DIR, 'package.json'))) { |
34 |
| - packageJson = require(path.resolve(constants.PUBLISH_DIR, 'package.json')); |
35 |
| - } else if (existsSync(path.resolve(constants.PUBLISH_DIR, '..', 'package.json'))) { |
36 |
| - packageJson = require(path.resolve(constants.PUBLISH_DIR, '..', 'package.json')); |
37 |
| - } else { |
38 |
| - failBuild(`Cannot locate your package.json file. Please make sure your package.json is |
39 |
| - at the root of your project or in your publish directory.` |
40 |
| - ); |
41 |
| - } |
| 30 | + // Require the project's package.json for access to its scripts |
| 31 | + // and dependencies in order to check existing project configuration |
| 32 | + let packageJson; |
| 33 | + if (existsSync(path.resolve(constants.PUBLISH_DIR, "package.json"))) { |
| 34 | + packageJson = require(path.resolve( |
| 35 | + constants.PUBLISH_DIR, |
| 36 | + "package.json" |
| 37 | + )); |
| 38 | + } else if ( |
| 39 | + existsSync(path.resolve(constants.PUBLISH_DIR, "..", "package.json")) |
| 40 | + ) { |
| 41 | + packageJson = require(path.resolve( |
| 42 | + constants.PUBLISH_DIR, |
| 43 | + "..", |
| 44 | + "package.json" |
| 45 | + )); |
| 46 | + } else { |
| 47 | + failBuild(`Cannot locate your package.json file. Please make sure your package.json is |
| 48 | + at the root of your project or in your publish directory.`); |
| 49 | + } |
42 | 50 |
|
43 |
| - const { scripts, dependencies } = packageJson; |
| 51 | + const { scripts, dependencies } = packageJson; |
44 | 52 |
|
45 |
| - if (isStaticExportProject({ build, scripts })) { |
46 |
| - failBuild(`** Static HTML export next.js projects do not require this plugin **`); |
47 |
| - } |
| 53 | + if (isStaticExportProject({ build, scripts })) { |
| 54 | + failBuild( |
| 55 | + `** Static HTML export next.js projects do not require this plugin **` |
| 56 | + ); |
| 57 | + } |
48 | 58 |
|
49 |
| - // TO-DO: check scripts to make sure the app isn't manually running NoN |
50 |
| - // For now, we'll make it clear in the README |
51 |
| - // const isAlreadyUsingNextOnNetlify = Object.keys(dependencies).find((dep) => dep === 'next-on-netlify'); |
52 |
| - // if (isAlreadyUsingNextOnNetlify) { |
53 |
| - // failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`); |
54 |
| - // } |
| 59 | + // TO-DO: check scripts to make sure the app isn't manually running NoN |
| 60 | + // For now, we'll make it clear in the README |
| 61 | + // const isAlreadyUsingNextOnNetlify = Object.keys(dependencies).find((dep) => dep === 'next-on-netlify'); |
| 62 | + // if (isAlreadyUsingNextOnNetlify) { |
| 63 | + // failBuild(`This plugin cannot support apps that manually use next-on-netlify. Uninstall next-on-netlify as a dependency to resolve.`); |
| 64 | + // } |
55 | 65 |
|
56 |
| - const isFunctionsDirectoryCorrect = build && build.functions && build.functions === path.resolve('out_functions'); |
57 |
| - if (!isFunctionsDirectoryCorrect) { |
58 |
| - // to do rephrase |
59 |
| - 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`); |
60 |
| - } |
| 66 | + const isFunctionsDirectoryCorrect = |
| 67 | + build && |
| 68 | + build.functions && |
| 69 | + build.functions === path.resolve("out_functions"); |
| 70 | + if (!isFunctionsDirectoryCorrect) { |
| 71 | + // to do rephrase |
| 72 | + failBuild( |
| 73 | + `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` |
| 74 | + ); |
| 75 | + } |
61 | 76 |
|
62 |
| - if (existsSync('next.config.js')) { |
63 |
| - // If the next config exists, fail build if target isnt in acceptableTargets |
64 |
| - const acceptableTargets = ['serverless', 'experimental-serverless-trace']; |
65 |
| - const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve('.')); |
66 |
| - const isValidTarget = acceptableTargets.includes(nextConfig.target); |
67 |
| - if (!isValidTarget) { |
68 |
| - failBuild(`next.config.js must be one of: ${acceptableTargets.join(', ')}`); |
69 |
| - } |
70 |
| - } else { |
71 |
| - // Create the next config file with target set to serverless by default |
72 |
| - const nextConfig = ` |
| 77 | + if (existsSync("next.config.js")) { |
| 78 | + // If the next config exists, fail build if target isnt in acceptableTargets |
| 79 | + const acceptableTargets = ["serverless", "experimental-serverless-trace"]; |
| 80 | + const nextConfig = loadConfig(PHASE_PRODUCTION_BUILD, path.resolve(".")); |
| 81 | + const isValidTarget = acceptableTargets.includes(nextConfig.target); |
| 82 | + if (!isValidTarget) { |
| 83 | + failBuild( |
| 84 | + `next.config.js must be one of: ${acceptableTargets.join(", ")}` |
| 85 | + ); |
| 86 | + } |
| 87 | + } else { |
| 88 | + // Create the next config file with target set to serverless by default |
| 89 | + const nextConfig = ` |
73 | 90 | module.exports = {
|
74 | 91 | target: 'serverless'
|
75 | 92 | }
|
76 | 93 | `;
|
77 |
| - await appendFile('next.config.js', nextConfig); |
78 |
| - console.log(`** Adding next.config.js with target set to 'serverless' **`); |
79 |
| - } |
| 94 | + await appendFile("next.config.js", nextConfig); |
| 95 | + console.log( |
| 96 | + `** Adding next.config.js with target set to 'serverless' **` |
| 97 | + ); |
| 98 | + } |
80 | 99 | },
|
81 | 100 | async onBuild({ constants }) {
|
82 |
| - console.log(`** Running Next on Netlify package **`); |
83 |
| - nextOnNetlify(); |
| 101 | + console.log(`** Running Next on Netlify package **`); |
| 102 | + nextOnNetlify(); |
84 | 103 |
|
85 |
| - // Next-on-netlify puts its files into out_functions and out_publish |
86 |
| - // Copy files from next-on-netlify's output to the right functions/publish dirs |
| 104 | + // Next-on-netlify puts its files into out_functions and out_publish |
| 105 | + // Copy files from next-on-netlify's output to the right functions/publish dirs |
87 | 106 |
|
88 |
| - // TO-DO: use FUNCTIONS_DIST when internal bug is fixed |
89 |
| - const { PUBLISH_DIR } = constants; |
90 |
| - // if (!existsSync(FUNCTIONS_DIST)) { |
91 |
| - // await makeDir(FUNCTIONS_DIST); |
92 |
| - // } |
93 |
| - if (!existsSync(PUBLISH_DIR)) { |
94 |
| - await makeDir(PUBLISH_DIR); |
95 |
| - } |
| 107 | + // TO-DO: use FUNCTIONS_DIST when internal bug is fixed |
| 108 | + const { PUBLISH_DIR } = constants; |
| 109 | + // if (!existsSync(FUNCTIONS_DIST)) { |
| 110 | + // await makeDir(FUNCTIONS_DIST); |
| 111 | + // } |
| 112 | + if (!existsSync(PUBLISH_DIR)) { |
| 113 | + await makeDir(PUBLISH_DIR); |
| 114 | + } |
96 | 115 |
|
97 |
| - // TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict |
98 |
| - // with function names that next-on-netlify can generate |
99 |
| - // cpx.copySync('out_functions/**/*', FUNCTIONS_SRC); |
100 |
| - cpx.copySync('out_publish/**/*', PUBLISH_DIR); |
101 |
| - } |
102 |
| -} |
| 116 | + // TO-DO: make sure FUNCTIONS_DIST doesnt have a custom function name conflict |
| 117 | + // with function names that next-on-netlify can generate |
| 118 | + // cpx.copySync('out_functions/**/*', FUNCTIONS_SRC); |
| 119 | + cpx.copySync("out_publish/**/*", PUBLISH_DIR); |
| 120 | + }, |
| 121 | +}; |
0 commit comments