Skip to content

fix: correctly exclude files #720

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Oct 19, 2021
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"fs-extra": "^10.0.0",
"moize": "^6.1.0",
"outdent": "^0.8.0",
"pathe": "^0.2.0",
"semver": "^7.3.5",
"slash": "^3.0.0",
"tiny-glob": "^0.2.9"
Expand Down
48 changes: 33 additions & 15 deletions src/helpers/config.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
// @ts-check
const { join } = require('path')

const { readJSON } = require('fs-extra')
const { join, dirname, relative } = require('pathe')

const defaultFailBuild = (message, { error }) => {
throw new Error(`${message}\n${error && error.stack}`)
Expand Down Expand Up @@ -100,17 +99,29 @@ exports.generateRedirects = async ({ netlifyConfig, basePath, i18n }) => {

exports.getNextConfig = async function getNextConfig({ publish, failBuild = defaultFailBuild }) {
try {
const { config, appDir } = await readJSON(join(publish, 'required-server-files.json'))
const { config, appDir, ignore } = await readJSON(join(publish, 'required-server-files.json'))
if (!config) {
return failBuild('Error loading your Next config')
}
return { ...config, appDir }
return { ...config, appDir, ignore }
} catch (error) {
return failBuild('Error loading your Next config', { error })
}
}

exports.configureHandlerFunctions = ({ netlifyConfig, publish }) => {
const resolveModuleRoot = (moduleName) => {
try {
return dirname(relative(process.cwd(), require.resolve(`${moduleName}/package.json`, { paths: [process.cwd()] })))
} catch (error) {
return null
}
}

exports.configureHandlerFunctions = ({ netlifyConfig, publish, ignore = [] }) => {
/* eslint-disable no-underscore-dangle */
netlifyConfig.functions._ipx ||= {}
netlifyConfig.functions._ipx.node_bundler = 'esbuild'
/* eslint-enable no-underscore-dangle */
;[HANDLER_FUNCTION_NAME, ODB_FUNCTION_NAME].forEach((functionName) => {
netlifyConfig.functions[functionName] ||= { included_files: [], external_node_modules: [] }
netlifyConfig.functions[functionName].node_bundler = 'nft'
Expand All @@ -120,16 +131,23 @@ exports.configureHandlerFunctions = ({ netlifyConfig, publish }) => {
`${publish}/serverless/**`,
`${publish}/*.json`,
`${publish}/BUILD_ID`,
'!node_modules/@next/swc-*/**/*',
'!node_modules/next/dist/compiled/@ampproject/toolbox-optimizer/**/*',
'!node_modules/next/dist/pages/**/*',
`!node_modules/next/dist/server/lib/squoosh/**/*.wasm`,
`!node_modules/next/dist/next-server/server/lib/squoosh/**/*.wasm`,
'!node_modules/next/dist/compiled/webpack/(bundle4|bundle5).js',
'!node_modules/react/**/*.development.js',
'!node_modules/react-dom/**/*.development.js',
'!node_modules/use-subscription/**/*.development.js',
'!node_modules/sharp/**/*',
...ignore.map((path) => `!${path}`),
)

const nextRoot = resolveModuleRoot('next')
if (nextRoot) {
netlifyConfig.functions[functionName].included_files.push(
`!${nextRoot}/dist/server/lib/squoosh/**/*.wasm`,
`!${nextRoot}/dist/next-server/server/lib/squoosh/**/*.wasm`,
`!${nextRoot}/dist/compiled/webpack/bundle4.js`,
`!${nextRoot}/dist/compiled/webpack/bundle5.js`,
`!${nextRoot}/dist/compiled/terser/bundle.min.js`,
)
}

const sharpRoot = resolveModuleRoot('sharp')
if (sharpRoot) {
netlifyConfig.functions[functionName].included_files.push(`!${sharpRoot}/**/*`)
}
})
}
4 changes: 2 additions & 2 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ module.exports = {

checkNextSiteHasBuilt({ publish, failBuild })

const { appDir, basePath, i18n, images, target } = await getNextConfig({ publish, failBuild })
const { appDir, basePath, i18n, images, target, ignore } = await getNextConfig({ publish, failBuild })

verifyBuildTarget(target)

configureHandlerFunctions({ netlifyConfig, publish: relative(process.cwd(), publish) })
configureHandlerFunctions({ netlifyConfig, ignore, publish: relative(process.cwd(), publish) })

await generateFunctions(constants, appDir)
await generatePagesResolver({ netlifyConfig, target, constants })
Expand Down
31 changes: 20 additions & 11 deletions test/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const { writeJSON, unlink, existsSync, readFileSync, copy } = require('fs-extra')
const { writeJSON, unlink, existsSync, readFileSync, copy, ensureDir } = require('fs-extra')
const path = require('path')
const process = require('process')

const os = require('os')
const cpy = require('cpy')
const { dir: getTmpDir } = require('tmp-promise')

Expand Down Expand Up @@ -42,9 +42,18 @@ const onBuildHasRun = (netlifyConfig) =>

// Move .next from sample project to current directory
const moveNextDist = async function () {
await stubModules(['next', 'react', 'react-dom', 'sharp'])
await copy(path.join(SAMPLE_PROJECT_DIR, '.next'), path.join(process.cwd(), '.next'))
}

const stubModules = async function (modules) {
for (const mod of modules) {
const dir = path.join(process.cwd(), 'node_modules', mod)
await ensureDir(dir)
await writeJSON(path.join(dir, 'package.json'), { name: mod })
}
}

// Copy fixture files to the current directory
const useFixture = async function (fixtureName) {
const fixtureDir = `${FIXTURES_DIR}/${fixtureName}`
Expand Down Expand Up @@ -185,19 +194,19 @@ describe('onBuild()', () => {
'.next/serverless/**',
'.next/*.json',
'.next/BUILD_ID',
'!node_modules/@next/swc-*/**/*',
'!node_modules/next/dist/compiled/@ampproject/toolbox-optimizer/**/*',
'!node_modules/next/dist/pages/**/*',
'!../node_modules/next/dist/compiled/@ampproject/toolbox-optimizer/**/*',
`!node_modules/next/dist/server/lib/squoosh/**/*.wasm`,
`!node_modules/next/dist/next-server/server/lib/squoosh/**/*.wasm`,
'!node_modules/next/dist/compiled/webpack/(bundle4|bundle5).js',
'!node_modules/react/**/*.development.js',
'!node_modules/react-dom/**/*.development.js',
'!node_modules/use-subscription/**/*.development.js',
'!node_modules/next/dist/compiled/webpack/bundle4.js',
'!node_modules/next/dist/compiled/webpack/bundle5.js',
'!node_modules/next/dist/compiled/terser/bundle.min.js',
'!node_modules/sharp/**/*',
]
expect(netlifyConfig.functions[HANDLER_FUNCTION_NAME].included_files).toEqual(includes)
expect(netlifyConfig.functions[ODB_FUNCTION_NAME].included_files).toEqual(includes)
// Relative paths in Windows are different
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It was not worth the effort to make the snapshots match on Windows. I tried. I gave up. I'm sorry.

if (os.platform() !== 'win32') {
expect(netlifyConfig.functions[HANDLER_FUNCTION_NAME].included_files).toEqual(includes)
expect(netlifyConfig.functions[ODB_FUNCTION_NAME].included_files).toEqual(includes)
}
expect(netlifyConfig.functions[HANDLER_FUNCTION_NAME].node_bundler).toEqual('nft')
expect(netlifyConfig.functions[ODB_FUNCTION_NAME].node_bundler).toEqual('nft')
})
Expand Down