1+ /* eslint-disable max-lines */
12import type { NetlifyConfig } from '@netlify/build'
23import destr from 'destr'
34import { readJSON , writeJSON } from 'fs-extra'
@@ -80,7 +81,28 @@ const resolveModuleRoot = (moduleName) => {
8081
8182const DEFAULT_EXCLUDED_MODULES = [ 'sharp' , 'electron' ]
8283
83- export const configureHandlerFunctions = async ( { netlifyConfig, publish, ignore = [ ] } ) => {
84+ export const hasManuallyAddedModule = ( {
85+ netlifyConfig,
86+ moduleName,
87+ } : {
88+ netlifyConfig : NetlifyConfig
89+ moduleName : string
90+ } ) =>
91+ /* eslint-disable camelcase */
92+ Object . values ( netlifyConfig . functions ) . some ( ( { included_files = [ ] } ) =>
93+ included_files . some ( ( inc ) => inc . includes ( `node_modules/${ moduleName } ` ) ) ,
94+ )
95+ /* eslint-enable camelcase */
96+
97+ export const configureHandlerFunctions = async ( {
98+ netlifyConfig,
99+ publish,
100+ ignore = [ ] ,
101+ } : {
102+ netlifyConfig : NetlifyConfig
103+ publish : string
104+ ignore : Array < string >
105+ } ) => {
84106 const config = await getRequiredServerFiles ( publish )
85107 const files = config . files || [ ]
86108 const cssFilesToInclude = files . filter ( ( f ) => f . startsWith ( `${ publish } /static/css/` ) )
@@ -91,6 +113,11 @@ export const configureHandlerFunctions = async ({ netlifyConfig, publish, ignore
91113 netlifyConfig . functions . _ipx . node_bundler = 'nft'
92114 }
93115
116+ // If the user has manually added the module to included_files, then don't exclude it
117+ const excludedModules = DEFAULT_EXCLUDED_MODULES . filter (
118+ ( moduleName ) => ! hasManuallyAddedModule ( { netlifyConfig, moduleName } ) ,
119+ )
120+
94121 /* eslint-enable no-underscore-dangle */
95122 ; [ HANDLER_FUNCTION_NAME , ODB_FUNCTION_NAME , '_api_*' ] . forEach ( ( functionName ) => {
96123 netlifyConfig . functions [ functionName ] ||= { included_files : [ ] , external_node_modules : [ ] }
@@ -109,6 +136,7 @@ export const configureHandlerFunctions = async ({ netlifyConfig, publish, ignore
109136 `${ publish } /BUILD_ID` ,
110137 `${ publish } /static/chunks/webpack-middleware*.js` ,
111138 `!${ publish } /server/**/*.js.nft.json` ,
139+ '!**/node_modules/@next/swc*/**/*' ,
112140 ...cssFilesToInclude ,
113141 ...ignore . map ( ( path ) => `!${ slash ( path ) } ` ) ,
114142 )
@@ -123,7 +151,7 @@ export const configureHandlerFunctions = async ({ netlifyConfig, publish, ignore
123151 )
124152 }
125153
126- DEFAULT_EXCLUDED_MODULES . forEach ( ( moduleName ) => {
154+ excludedModules . forEach ( ( moduleName ) => {
127155 const moduleRoot = resolveModuleRoot ( moduleName )
128156 if ( moduleRoot ) {
129157 netlifyConfig . functions [ functionName ] . included_files . push ( `!${ moduleRoot } /**/*` )
@@ -156,11 +184,11 @@ const buildHeader = (buildHeaderParams: BuildHeaderParams) => {
156184const sanitizePath = ( path : string ) => path . replace ( / : [ ^ * / ] + \* $ / , '*' )
157185
158186/**
159- * Persist NEXT .js custom headers to the Netlify configuration so the headers work with static files
187+ * Persist Next .js custom headers to the Netlify configuration so the headers work with static files
160188 * See {@link https://nextjs.org/docs/api-reference/next.config.js/headers} for more information on custom
161189 * headers in Next.js
162190 *
163- * @param nextConfig - The NextJS configuration
191+ * @param nextConfig - The Next.js configuration
164192 * @param netlifyHeaders - Existing headers that are already configured in the Netlify configuration
165193 */
166194export const generateCustomHeaders = ( nextConfig : NextConfig , netlifyHeaders : NetlifyHeaders = [ ] ) => {
@@ -210,3 +238,4 @@ export const generateCustomHeaders = (nextConfig: NextConfig, netlifyHeaders: Ne
210238 }
211239 }
212240}
241+ /* eslint-enable max-lines */
0 commit comments