1
1
// @ts -check
2
- const { join } = require ( 'path' )
2
+ const { join, dirname , relative } = require ( 'path' )
3
3
4
4
const { readJSON } = require ( 'fs-extra' )
5
5
@@ -100,17 +100,29 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {
100
100
101
101
exports . getNextConfig = async function getNextConfig ( { publish, failBuild = defaultFailBuild } ) {
102
102
try {
103
- const { config, appDir } = await readJSON ( join ( publish , 'required-server-files.json' ) )
103
+ const { config, appDir, ignore } = await readJSON ( join ( publish , 'required-server-files.json' ) )
104
104
if ( ! config ) {
105
105
return failBuild ( 'Error loading your Next config' )
106
106
}
107
- return { ...config , appDir }
107
+ return { ...config , appDir, ignore }
108
108
} catch ( error ) {
109
109
return failBuild ( 'Error loading your Next config' , { error } )
110
110
}
111
111
}
112
112
113
- exports . configureHandlerFunctions = ( { netlifyConfig, publish } ) => {
113
+ const resolveModuleRoot = ( moduleName ) => {
114
+ try {
115
+ return dirname ( relative ( process . cwd ( ) , require . resolve ( `${ moduleName } /package.json` ) ) )
116
+ } catch ( error ) {
117
+ return null
118
+ }
119
+ }
120
+
121
+ exports . configureHandlerFunctions = ( { netlifyConfig, publish, ignore = [ ] } ) => {
122
+ /* eslint-disable no-underscore-dangle */
123
+ netlifyConfig . functions . _ipx ||= { }
124
+ netlifyConfig . functions . _ipx . node_bundler = 'esbuild'
125
+ /* eslint-enable no-underscore-dangle */
114
126
; [ HANDLER_FUNCTION_NAME , ODB_FUNCTION_NAME ] . forEach ( ( functionName ) => {
115
127
netlifyConfig . functions [ functionName ] ||= { included_files : [ ] , external_node_modules : [ ] }
116
128
netlifyConfig . functions [ functionName ] . node_bundler = 'nft'
@@ -120,16 +132,33 @@ exports.configureHandlerFunctions = ({ netlifyConfig, publish }) => {
120
132
`${ publish } /serverless/**` ,
121
133
`${ publish } /*.json` ,
122
134
`${ publish } /BUILD_ID` ,
123
- '!node_modules/@next/swc-*/**/*' ,
124
- '!node_modules/next/dist/compiled/@ampproject/toolbox-optimizer/**/*' ,
125
- '!node_modules/next/dist/pages/**/*' ,
126
- `!node_modules/next/dist/server/lib/squoosh/**/*.wasm` ,
127
- `!node_modules/next/dist/next-server/server/lib/squoosh/**/*.wasm` ,
128
- '!node_modules/next/dist/compiled/webpack/(bundle4|bundle5).js' ,
129
- '!node_modules/react/**/*.development.js' ,
130
- '!node_modules/react-dom/**/*.development.js' ,
131
- '!node_modules/use-subscription/**/*.development.js' ,
132
- '!node_modules/sharp/**/*' ,
135
+ ...ignore . map ( ( path ) => `!${ path } ` ) ,
133
136
)
137
+
138
+ const nextRoot = resolveModuleRoot ( 'next' )
139
+ if ( nextRoot ) {
140
+ netlifyConfig . functions [ functionName ] . included_files . push (
141
+ `!${ nextRoot } /dist/pages/**/*` ,
142
+ `!${ nextRoot } /dist/server/lib/squoosh/**/*.wasm` ,
143
+ `!${ nextRoot } /dist/next-server/server/lib/squoosh/**/*.wasm` ,
144
+ `!${ nextRoot } /dist/compiled/webpack/bundle4.js` ,
145
+ `!${ nextRoot } /dist/compiled/webpack/bundle5.js` ,
146
+ `!${ nextRoot } /dist/compiled/terser/bundle.min.js` ,
147
+ )
148
+ }
149
+
150
+ const reactRoot = resolveModuleRoot ( 'react' )
151
+ if ( reactRoot ) {
152
+ netlifyConfig . functions [ functionName ] . included_files . push ( `!${ reactRoot } /**/*.development.js` )
153
+ }
154
+ const reactDomRoot = resolveModuleRoot ( 'react-dom' )
155
+ if ( reactDomRoot ) {
156
+ netlifyConfig . functions [ functionName ] . included_files . push ( `!${ reactDomRoot } /**/*.development.js` )
157
+ }
158
+
159
+ const sharpRoot = resolveModuleRoot ( 'sharp' )
160
+ if ( sharpRoot ) {
161
+ netlifyConfig . functions [ functionName ] . included_files . push ( `!${ sharpRoot } /**/*` )
162
+ }
134
163
} )
135
164
}
0 commit comments