Skip to content

Commit

Permalink
feat!: add importMapPaths to bundle and serve (#265)
Browse files Browse the repository at this point in the history
BREAKING CHANGE: Both `bundle` and `serve` now expect an `importMapPaths` array containing a list of paths to any user-defined import map files
  • Loading branch information
eduardoboucas authored Dec 19, 2022
1 parent 349051e commit dcbd7f7
Show file tree
Hide file tree
Showing 19 changed files with 200 additions and 315 deletions.
16 changes: 11 additions & 5 deletions node/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import tmp from 'tmp-promise'
import { test, expect } from 'vitest'

import { importMapSpecifier } from '../shared/consts.js'
import { useFixture } from '../test/util.js'
import { runESZIP, useFixture } from '../test/util.js'

import { BundleError } from './bundle_error.js'
import { bundle, BundleOptions } from './bundler.js'
Expand All @@ -27,13 +27,11 @@ test('Produces an ESZIP bundle', async () => {
const result = await bundle([userDirectory, internalDirectory], distPath, declarations, {
basePath,
configPath: join(internalDirectory, 'config.json'),
featureFlags: {
edge_functions_read_deno_config: true,
},
importMapPaths: [join(userDirectory, 'import_map.json')],
})
const generatedFiles = await fs.readdir(distPath)

expect(result.functions.length).toBe(2)
expect(result.functions.length).toBe(3)
expect(generatedFiles.length).toBe(2)

const manifestFile = await fs.readFile(resolve(distPath, 'manifest.json'), 'utf8')
Expand All @@ -47,6 +45,14 @@ test('Produces an ESZIP bundle', async () => {

expect(importMapURL).toBe(importMapSpecifier)

const bundlePath = join(distPath, bundles[0].asset)

const { func1, func2, func3 } = await runESZIP(bundlePath)

expect(func1).toBe('HELLO, JANE DOE!')
expect(func2).toBe('Jane Doe')
expect(func3).toBe('hello, netlify!')

await cleanup()
})

Expand Down
22 changes: 4 additions & 18 deletions node/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,11 @@ import { DenoBridge, DenoOptions, OnAfterDownloadHook, OnBeforeDownloadHook } fr
import type { Bundle } from './bundle.js'
import { FunctionConfig, getFunctionConfig } from './config.js'
import { Declaration, getDeclarationsFromConfig } from './declaration.js'
import { getConfig as getDenoConfig } from './deno_config.js'
import { load as loadDeployConfig } from './deploy_config.js'
import { FeatureFlags, getFlags } from './feature_flags.js'
import { findFunctions } from './finder.js'
import { bundle as bundleESZIP } from './formats/eszip.js'
import { ImportMap, readFile as readImportMapFile } from './import_map.js'
import { ImportMap } from './import_map.js'
import { getLogger, LogFunction } from './logger.js'
import { writeManifest } from './manifest.js'
import { ensureLatestTypes } from './types.js'
Expand All @@ -27,6 +26,7 @@ interface BundleOptions {
debug?: boolean
distImportMapPath?: string
featureFlags?: FeatureFlags
importMapPaths?: (string | undefined)[]
onAfterDownload?: OnAfterDownloadHook
onBeforeDownload?: OnBeforeDownloadHook
systemLogger?: LogFunction
Expand All @@ -43,6 +43,7 @@ const bundle = async (
debug,
distImportMapPath,
featureFlags: inputFeatureFlags,
importMapPaths = [],
onAfterDownload,
onBeforeDownload,
systemLogger,
Expand Down Expand Up @@ -81,22 +82,7 @@ const bundle = async (
const externals = deployConfig.layers.map((layer) => layer.name)
const importMap = new ImportMap()

if (deployConfig.importMap) {
importMap.add(deployConfig.importMap)
}

if (featureFlags.edge_functions_read_deno_config) {
// Look for a Deno config file and read it if one exists.
const denoConfig = await getDenoConfig(logger, basePath)

// If the Deno config file defines an import map, read the file and add the
// imports to the global import map.
if (denoConfig?.importMap) {
const importMapFile = await readImportMapFile(denoConfig.importMap)

importMap.add(importMapFile)
}
}
await importMap.addFiles([deployConfig?.importMap, ...importMapPaths])

const functions = await findFunctions(sourceDirectories)
const functionBundle = await bundleESZIP({
Expand Down
122 changes: 0 additions & 122 deletions node/deno_config.test.ts

This file was deleted.

63 changes: 0 additions & 63 deletions node/deno_config.ts

This file was deleted.

9 changes: 4 additions & 5 deletions node/deploy_config.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { promises as fs } from 'fs'
import { join } from 'path'
import { cwd } from 'process'
import { pathToFileURL } from 'url'

import tmp from 'tmp-promise'
import { test, expect } from 'vitest'
Expand All @@ -20,7 +19,7 @@ test('Returns an empty config object if there is no file at the given path', asy
})

test('Returns a config object with declarations, layers, and import map', async () => {
const importMapFile = await tmp.file()
const importMapFile = await tmp.file({ postfix: '.json' })
const importMap = {
imports: {
'https://deno.land/': 'https://black.hole/',
Expand Down Expand Up @@ -52,9 +51,9 @@ test('Returns a config object with declarations, layers, and import map', async

const parsedConfig = await load(configFile.path, logger)

await importMapFile.cleanup()

expect(parsedConfig.declarations).toEqual(config.functions)
expect(parsedConfig.layers).toEqual(config.layers)
expect(parsedConfig.importMap).toBeTruthy()
expect(parsedConfig.importMap?.baseURL).toEqual(pathToFileURL(importMapFile.path))
expect(parsedConfig.importMap?.imports).toEqual(importMap.imports)
expect(parsedConfig.importMap).toBe(importMapFile.path)
})
13 changes: 4 additions & 9 deletions node/deploy_config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ import { promises as fs } from 'fs'
import { dirname, resolve } from 'path'

import type { Declaration } from './declaration.js'
import { ImportMapFile, readFile as readImportMap } from './import_map.js'
import type { Layer } from './layer.js'
import type { Logger } from './logger.js'
import { isNodeError } from './utils/error.js'
Expand All @@ -18,7 +17,7 @@ interface DeployConfigFile {

export interface DeployConfig {
declarations: Declaration[]
importMap?: ImportMapFile
importMap?: string
layers: Layer[]
}

Expand Down Expand Up @@ -47,24 +46,20 @@ export const load = async (path: string | undefined, logger: Logger): Promise<De
}
}

const parse = async (data: DeployConfigFile, path: string): Promise<DeployConfig> => {
const parse = (data: DeployConfigFile, path: string) => {
if (data.version !== 1) {
throw new Error(`Unsupported file version: ${data.version}`)
}

const config = {
const config: DeployConfig = {
declarations: data.functions ?? [],
layers: data.layers ?? [],
}

if (data.import_map) {
const importMapPath = resolve(dirname(path), data.import_map)
const importMap = await readImportMap(importMapPath)

return {
...config,
importMap,
}
config.importMap = importMapPath
}

return config
Expand Down
1 change: 0 additions & 1 deletion node/feature_flags.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
const defaultFlags: Record<string, boolean> = {
edge_functions_cache_deno_dir: false,
edge_functions_config_export: false,
edge_functions_read_deno_config: false,
}

type FeatureFlag = keyof typeof defaultFlags
Expand Down
Loading

0 comments on commit dcbd7f7

Please sign in to comment.