Skip to content

Commit

Permalink
fix: support npm modules with relative exports
Browse files Browse the repository at this point in the history
  • Loading branch information
Skn0tt committed Dec 5, 2023
1 parent c06e234 commit 6ed3725
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 7 deletions.
28 changes: 21 additions & 7 deletions node/npm_dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,18 @@ import { pathsBetween } from './utils/fs.js'

const TYPESCRIPT_EXTENSIONS = new Set(['.ts', '.tsx', '.cts', '.ctsx', '.mts', '.mtsx'])

const parseSpecifier = (specifier: string): { scope?: string; pkg: string; path?: string } => {
if (specifier.startsWith('@')) {
const [scope, pkg, path] = specifier.slice(1).split('/', 3)
return { scope, pkg, path }
}
const [pkg, path] = specifier.split('/', 2)
return { pkg, path }
}

const slugifyPackageName = (specifier: string) => {
if (!specifier.startsWith('@')) return specifier
const [scope, pkg] = specifier.split('/')
return `${scope.replace('@', '')}__${pkg}`
const { scope, pkg, path } = parseSpecifier(specifier)
return [scope, pkg, path?.replace(/\//g, '')].filter(Boolean).join('__')
}

/**
Expand Down Expand Up @@ -190,10 +198,16 @@ const getNPMSpecifiers = async ({ basePath, functions, importMap, environment, r
// dependencies. Because we'll bundle all modules in a subsequent step,
// any transitive dependencies will be handled then.
if (isDirectDependency) {
npmSpecifiers.push({
specifier: packageName,
types: environment === 'development' ? await safelyDetectTypes(path.join(basePath, filePath)) : undefined,
})
const packJson = JSON.parse(await fs.readFile(path.join(basePath, filePath), { encoding: 'utf-8' }))

const subpaths = packJson.exports ? Object.keys(packJson.exports) : ['.']
// eslint-disable-next-line max-depth
for (const subpath of subpaths) {
npmSpecifiers.push({
specifier: path.join(packageName, subpath),
types: environment === 'development' ? await safelyDetectTypes(path.join(basePath, filePath)) : undefined,
})
}
}
}

Expand Down
2 changes: 2 additions & 0 deletions node/npm_import_error.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ class NPMImportError extends Error {
`There was an error when loading the '${moduleName}' npm module. Support for npm modules in edge functions is an experimental feature. Refer to https://ntl.fyi/edge-functions-npm for more information.`,
)

console.error(originalError)

this.name = 'NPMImportError'
this.stack = originalError.stack

Expand Down
6 changes: 6 additions & 0 deletions test/fixtures/imports_npm_module/functions/func1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,12 @@ import parent1 from 'parent-1'
import parent3 from './lib/util.ts'
import { echo, parent2 } from 'alias:helper'
import { HTMLRewriter } from 'html-rewriter'
import withSubpathIndex from "with-subpath"
import withSubpathFoo from "with-subpath/foo"
import assert from 'node:assert'

assert(withSubpathIndex === "with-subpath/index")
assert(withSubpathFoo === "with-subpath/foo")

await Promise.resolve()

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.

0 comments on commit 6ed3725

Please sign in to comment.