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: NPM bundling should use ESM format #494

Merged
merged 4 commits into from
Oct 9, 2023
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
7 changes: 7 additions & 0 deletions node/bundler.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ test('Handles imports with the `node:` prefix', async () => {
})

test('Loads npm modules from bare specifiers', async () => {
const systemLogger = vi.fn()
const { basePath, cleanup, distPath } = await useFixture('imports_npm_module')
const sourceDirectory = join(basePath, 'functions')
const declarations: Declaration[] = [
Expand All @@ -501,7 +502,13 @@ test('Loads npm modules from bare specifiers', async () => {
featureFlags: { edge_functions_npm_modules: true },
importMapPaths: [join(basePath, 'import_map.json')],
vendorDirectory: vendorDirectory.path,
systemLogger,
})

expect(
systemLogger.mock.calls.find((call) => call[0] === 'Could not track dependencies in edge function:'),
).toBeUndefined()

const manifestFile = await readFile(resolve(distPath, 'manifest.json'), 'utf8')
const manifest = JSON.parse(manifestFile)
const bundlePath = join(distPath, manifest.bundles[0].asset)
Expand Down
1 change: 1 addition & 0 deletions node/npm_dependencies.ts
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ export const vendorNPMSpecifiers = async ({
platform: 'node',
plugins: [getDependencyTrackerPlugin(specifiers, importMap.getContentsWithURLObjects(), pathToFileURL(basePath))],
write: false,
format: 'esm',
Copy link
Contributor Author

Choose a reason for hiding this comment

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

The more general problem is that ESBuild performs all kinds of syntax checks on the code that it parses. For example, it will stumble over this snippet:

function deadCodeThatIsNeverCalled() {
  const foo = ""
  foo = "hello"
}

In my REPL, this didn't throw until calling it - so if it's really dead code, then it's OK to include it in the bundle. ESBuild will throw a warning, though.

This might be a problem, because it prevents code with false-positives like this from using NPM modules 🤔

Copy link
Contributor Author

Choose a reason for hiding this comment

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

(it's not a made-up scenario, i've seen instances of this error in Humio. stuff for a follow-up though)

})
} catch (error) {
logger.system('Could not track dependencies in edge function:', error)
Expand Down
2 changes: 2 additions & 0 deletions test/fixtures/imports_npm_module/functions/func1.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ import parent2 from 'parent-2'
import parent3 from './lib/util.ts'
import { echo } from 'alias:helper'

await Promise.resolve()

export default async () => {
const text = [parent1('JavaScript'), parent2('APIs'), parent3('Markup')].join(', ')

Expand Down