Skip to content

Commit 383c186

Browse files
authored
fix: exclude swc by default, and don't exclude sharp if included (#1745)
1 parent 178e949 commit 383c186

File tree

2 files changed

+72
-4
lines changed

2 files changed

+72
-4
lines changed

packages/runtime/src/helpers/config.ts

+33-4
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
/* eslint-disable max-lines */
12
import type { NetlifyConfig } from '@netlify/build'
23
import destr from 'destr'
34
import { readJSON, writeJSON } from 'fs-extra'
@@ -80,7 +81,28 @@ const resolveModuleRoot = (moduleName) => {
8081

8182
const 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) => {
156184
const 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
*/
166194
export 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 */

test/index.js

+39
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,7 @@ beforeEach(async () => {
162162
netlifyConfig.headers = []
163163
netlifyConfig.functions[HANDLER_FUNCTION_NAME] && (netlifyConfig.functions[HANDLER_FUNCTION_NAME].included_files = [])
164164
netlifyConfig.functions[ODB_FUNCTION_NAME] && (netlifyConfig.functions[ODB_FUNCTION_NAME].included_files = [])
165+
netlifyConfig.functions['_api_*'] && (netlifyConfig.functions['_api_*'].included_files = [])
165166
await useFixture('serverless_next_config')
166167
})
167168

@@ -517,6 +518,7 @@ describe('onBuild()', () => {
517518
'.next/BUILD_ID',
518519
'.next/static/chunks/webpack-middleware*.js',
519520
'!.next/server/**/*.js.nft.json',
521+
'!**/node_modules/@next/swc*/**/*',
520522
'!../../node_modules/next/dist/compiled/@ampproject/toolbox-optimizer/**/*',
521523
`!node_modules/next/dist/server/lib/squoosh/**/*.wasm`,
522524
`!node_modules/next/dist/next-server/server/lib/squoosh/**/*.wasm`,
@@ -533,6 +535,43 @@ describe('onBuild()', () => {
533535
expect(netlifyConfig.functions[ODB_FUNCTION_NAME].node_bundler).toEqual('nft')
534536
})
535537

538+
const excludesSharp = (includedFiles) => includedFiles.some((file) => file.startsWith('!') && file.includes('sharp'))
539+
540+
it("doesn't exclude sharp if manually included", async () => {
541+
await moveNextDist()
542+
543+
const functions = [HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME, '_api_*']
544+
545+
await nextRuntime.onBuild(defaultArgs)
546+
547+
// Should exclude by default
548+
for (const func of functions) {
549+
expect(excludesSharp(netlifyConfig.functions[func].included_files)).toBeTruthy()
550+
}
551+
552+
// ...but if the user has added it, we shouldn't exclude it
553+
for (const func of functions) {
554+
netlifyConfig.functions[func].included_files = ['node_modules/sharp/**/*']
555+
}
556+
557+
await nextRuntime.onBuild(defaultArgs)
558+
559+
for (const func of functions) {
560+
expect(excludesSharp(netlifyConfig.functions[func].included_files)).toBeFalsy()
561+
}
562+
563+
// ...even if it's in a subdirectory
564+
for (const func of functions) {
565+
netlifyConfig.functions[func].included_files = ['subdirectory/node_modules/sharp/**/*']
566+
}
567+
568+
await nextRuntime.onBuild(defaultArgs)
569+
570+
for (const func of functions) {
571+
expect(excludesSharp(netlifyConfig.functions[func].included_files)).toBeFalsy()
572+
}
573+
})
574+
536575
it('generates a file referencing all page sources', async () => {
537576
await moveNextDist()
538577
await nextRuntime.onBuild(defaultArgs)

0 commit comments

Comments
 (0)