Skip to content
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

fix: read other import map paths when one doesn't exist #268

Merged
merged 2 commits into from
Dec 19, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 35 additions & 14 deletions node/bundler.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { promises as fs } from 'fs'
import { join, resolve } from 'path'
import process from 'process'
import { pathToFileURL } from 'url'

import { deleteAsync } from 'del'
import tmp from 'tmp-promise'
Expand Down Expand Up @@ -346,25 +347,45 @@ test('Loads declarations and import maps from the deploy configuration', async (
test("Ignores entries in `importMapPaths` that don't point to an existing import map file", async () => {
const systemLogger = vi.fn()
const { basePath, cleanup, distPath } = await useFixture('with_import_maps')
const sourceDirectory = join(basePath, 'functions')
const importMapPath = join(distPath, 'some-file-that-does-not-exist.json')
const declarations = [
const sourceDirectory = join(basePath, 'user-functions')

// Creating import map file
const importMap = await tmp.file()
const importMapContents = {
imports: {
helper: pathToFileURL(join(basePath, 'helper.ts')).toString(),
},
scopes: {
[pathToFileURL(join(sourceDirectory, 'func3')).toString()]: {
helper: pathToFileURL(join(basePath, 'helper2.ts')).toString(),
},
},
}

await fs.writeFile(importMap.path, JSON.stringify(importMapContents))

const nonExistingImportMapPath = join(distPath, 'some-file-that-does-not-exist.json')
const result = await bundle(
[sourceDirectory],
distPath,
[
{
function: 'func1',
path: '/func1',
},
],
{
function: 'func1',
path: '/func1',
basePath,
importMapPaths: [nonExistingImportMapPath, importMap.path],
systemLogger,
},
]
const result = await bundle([sourceDirectory], distPath, declarations, {
basePath,
configPath: join(sourceDirectory, 'config.json'),
importMapPaths: [importMapPath],
systemLogger,
})
)
const generatedFiles = await fs.readdir(distPath)

expect(result.functions.length).toBe(1)
expect(result.functions.length).toBe(2)
expect(generatedFiles.length).toBe(2)
expect(systemLogger).toHaveBeenCalledWith(`Did not find an import map file at '${importMapPath}'.`)
expect(systemLogger).toHaveBeenCalledWith(`Did not find an import map file at '${nonExistingImportMapPath}'.`)

await cleanup()
await importMap.cleanup()
})
2 changes: 1 addition & 1 deletion node/import_map.ts
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class ImportMap {
async addFiles(paths: (string | undefined)[], logger: Logger) {
for (const path of paths) {
if (path === undefined) {
return
continue
Copy link
Member Author

Choose a reason for hiding this comment

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

🙈

}

await this.addFile(path, logger)
Expand Down
5 changes: 2 additions & 3 deletions test/util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,13 @@ const dirname = fileURLToPath(url)
const fixturesDir = resolve(dirname, '..', 'fixtures')

const useFixture = async (fixtureName: string) => {
const tmpDir = await tmp.dir()
const cleanup = () => fs.rmdir(tmpDir.path, { recursive: true })
const tmpDir = await tmp.dir({ unsafeCleanup: true })
const fixtureDir = resolve(fixturesDir, fixtureName)
const distPath = join(tmpDir.path, '.netlify', 'edge-functions-dist')

return {
basePath: fixtureDir,
cleanup,
cleanup: tmpDir.cleanup,
distPath,
}
}
Expand Down