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

fix: take repositoryRoot into account when computing base path with esbuild #754

Merged
merged 3 commits into from
Oct 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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