Skip to content
This repository has been archived by the owner on May 22, 2024. It is now read-only.

Commit

Permalink
fix: take repositoryRoot into account when computing base path with…
Browse files Browse the repository at this point in the history
… esbuild (#754)

* chore: add failing case to test

* fix: take `repositoryRoot` into account in esbuild base path computation
  • Loading branch information
eduardoboucas committed Oct 20, 2021
1 parent f09a480 commit 8b2f8ea
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 8 deletions.
17 changes: 14 additions & 3 deletions src/runtimes/node/bundlers/esbuild/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { dirname, normalize } from 'path'
import type { BundleFunction } from '..'
import type { FunctionConfig } from '../../../../config'
import { getPathWithExtension } from '../../../../utils/fs'
import { nonNullable } from '../../../../utils/non_nullable'
import { getBasePath } from '../../utils/base_path'

import { bundleJsFile } from './bundler'
Expand All @@ -12,15 +13,19 @@ import { getSrcFiles } from './src_files'
const getFunctionBasePath = ({
basePathFromConfig,
mainFile,
repositoryRoot,
supportingSrcFiles,
}: {
basePathFromConfig?: string
mainFile: string
repositoryRoot?: string
supportingSrcFiles: string[]
}) => {
// If there is a base path defined in the config, we use that.
// If there is a base path defined in the config, we use that. To account for
// paths outside of `basePathFromConfig` but inside `repositoryRoot`, we use
// the common path prefix between the two.
if (basePathFromConfig !== undefined) {
return basePathFromConfig
return getBasePath([basePathFromConfig, repositoryRoot].filter(nonNullable))
}

// If not, the base path is the common path prefix between all the supporting
Expand Down Expand Up @@ -52,6 +57,7 @@ const bundle: BundleFunction = async ({
mainFile,
name,
pluginsModulesPath,
repositoryRoot,
runtime,
srcDir,
srcPath,
Expand Down Expand Up @@ -102,7 +108,12 @@ const bundle: BundleFunction = async ({
// bundled file further below.
const supportingSrcFiles = srcFiles.filter((path) => path !== mainFile)
const normalizedMainFile = getPathWithExtension(mainFile, '.js')
const functionBasePath = getFunctionBasePath({ basePathFromConfig: basePath, mainFile, supportingSrcFiles })
const functionBasePath = getFunctionBasePath({
basePathFromConfig: basePath,
mainFile,
repositoryRoot,
supportingSrcFiles,
})

return {
aliases: bundlePaths,
Expand Down

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

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

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

3 changes: 2 additions & 1 deletion tests/fixtures/node-monorepo/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
"name": "local-node-module",
"version": "1.0.0",
"dependencies": {
"@netlify/mock-package": "1.0.0"
"@netlify/mock-package": "1.0.0",
"@netlify/mock-package-2": "1.0.0"
}
}
Original file line number Diff line number Diff line change
@@ -1 +1,4 @@
module.exports = require('@netlify/mock-package')
const mock1 = require('@netlify/mock-package')
const mock2 = require('@netlify/mock-package-2')

module.exports = { mock1, mock2 }
10 changes: 8 additions & 2 deletions tests/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -2175,15 +2175,21 @@ testMany(
const basePath = join(fixtureDir, 'packages', 'site-1', 'netlify', 'functions')
const opts = merge(options, {
basePath,
config: {
'*': {
externalNodeModules: ['@netlify/mock-package-2'],
},
},
repositoryRoot: fixtureDir,
})
const result = await zipFunction(`${basePath}/function-1.js`, tmpDir, opts)

await unzipFiles([result])

const { mock } = require(`${tmpDir}/function-1.js`)
const { mock1, mock2 } = require(`${tmpDir}/function-1.js`)

t.true(mock)
t.true(mock1)
t.true(mock2)
},
)

Expand Down

1 comment on commit 8b2f8ea

@github-actions
Copy link
Contributor

Choose a reason for hiding this comment

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

⏱ Benchmark results

largeDepsEsbuild: 8.8s

largeDepsNft: 55.1s

largeDepsZisi: 1m 11.6s

Please sign in to comment.