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

fix: make empty-import-meta warning more actionable #1561

Merged
merged 2 commits into from
Sep 6, 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
13 changes: 12 additions & 1 deletion src/runtimes/node/bundlers/esbuild/bundler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,17 @@ var __glob = (map) => (path) => {
const cleanTempFiles = getCleanupFunction([...bundlePaths.keys()])
const additionalPaths = includedFilesFromModuleDetection

const updatedWarnings = warnings.map((warning) => {
if (warning.id === 'empty-import-meta') {
return {
...warning,
text: `"import.meta" is not available and will be empty, use __dirname instead`,
}
}

return warning
})

return {
additionalPaths,
bundlePaths,
Expand All @@ -215,7 +226,7 @@ var __glob = (map) => (path) => {
moduleFormat,
nativeNodeModules,
outputExtension,
warnings,
warnings: updatedWarnings,
}
} catch (error) {
throw FunctionBundlingUserError.addCustomErrorInfo(error, {
Expand Down
10 changes: 10 additions & 0 deletions tests/fixtures/import-meta-warning/function.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
export const handler = async () => ({
statusCode: 200,
body: JSON.stringify({
msg: "Hello from .ts",
v: 2,
// @ts-ignore Error expected
importMetaURL: import.meta.url,
dirname: typeof __dirname === "undefined" ? undefined : __dirname,
}),
});
13 changes: 13 additions & 0 deletions tests/main.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2765,4 +2765,17 @@ describe('zip-it-and-ship-it', () => {
expect(handler()).toBe(true)
}
})

testMany('esbuild hides unactionable import.meta warning', ['bundler_esbuild'], async (options) => {
const {
files: [{ bundlerWarnings }],
} = await zipFixture('import-meta-warning', {
length: 1,
opts: options,
})
expect(bundlerWarnings).toHaveLength(1)
expect((bundlerWarnings?.[0] as any).text).toEqual(
`"import.meta" is not available and will be empty, use __dirname instead`,
)
})
})