diff --git a/packages/wrangler/src/__tests__/deploy.test.ts b/packages/wrangler/src/__tests__/deploy.test.ts index 8c2f844a9f02..bf2c55637307 100644 --- a/packages/wrangler/src/__tests__/deploy.test.ts +++ b/packages/wrangler/src/__tests__/deploy.test.ts @@ -12605,6 +12605,11 @@ export default{ "# Python vendor module 2\nprint('hello')" ); + await fs.promises.writeFile( + "python_modules/test.pyc", + "this shouldn't be deployed" + ); + // Create a regular Python module await fs.promises.writeFile( "src/helper.py", @@ -12614,12 +12619,15 @@ export default{ const expectedModules = { "index.py": mainPython, "helper.py": "# Helper module\ndef helper(): pass", + "python_modules/module1.so": "binary content for module 1", + "python_modules/module2.py": "# Python vendor module 2\nprint('hello')", }; mockSubDomainRequest(); mockUploadWorkerRequest({ expectedMainModule: "index.py", expectedModules, + excludedModules: ["python_modules/test.pyc"], }); await runWrangler("deploy"); diff --git a/packages/wrangler/src/__tests__/helpers/mock-upload-worker.ts b/packages/wrangler/src/__tests__/helpers/mock-upload-worker.ts index f9ef5d266f21..d6595af8c282 100644 --- a/packages/wrangler/src/__tests__/helpers/mock-upload-worker.ts +++ b/packages/wrangler/src/__tests__/helpers/mock-upload-worker.ts @@ -23,6 +23,7 @@ export function mockUploadWorkerRequest( expectedType?: "esm" | "sw" | "none"; expectedBindings?: unknown; expectedModules?: Record; + excludedModules?: string[]; expectedCompatibilityDate?: string; expectedCompatibilityFlags?: string[]; expectedMigrations?: CfWorkerInit["migrations"]; @@ -135,6 +136,9 @@ export function mockUploadWorkerRequest( for (const [name, content] of Object.entries(expectedModules)) { expect(await serialize(formBody.get(name))).toEqual(content); } + for (const name of excludedModules) { + expect(formBody.get(name)).toBeNull(); + } if (useOldUploadApi) { return HttpResponse.json( @@ -173,6 +177,7 @@ export function mockUploadWorkerRequest( expectedType = "esm", expectedBindings, expectedModules = {}, + excludedModules = [], expectedCompatibilityDate, expectedCompatibilityFlags, env = undefined, diff --git a/packages/wrangler/src/deployment-bundle/find-additional-modules.ts b/packages/wrangler/src/deployment-bundle/find-additional-modules.ts index 77a4641225fa..956583120d01 100644 --- a/packages/wrangler/src/deployment-bundle/find-additional-modules.ts +++ b/packages/wrangler/src/deployment-bundle/find-additional-modules.ts @@ -176,13 +176,30 @@ export async function findAdditionalModules( pythonModulesDir, parseRules(vendoredRules) ) - ).map((m) => { - const prefixedPath = path.join("python_modules", m.name); - return { - ...m, - name: prefixedPath, - }; - }); + ) + .filter( + // Exclude certain file extensions to avoid bundling files that aren't + // necessary and which increase the size of the bundle considerably. + (m) => + !m.name.endsWith(".pyc") && + // Translation files + !m.name.endsWith(".po") && + // Static files shouldn't be included + !m.name.endsWith(".svg") && + !m.name.endsWith(".png") && + !m.name.endsWith(".jpg") && + !m.name.endsWith(".css") && + !m.name.endsWith(".gz") && + !m.name.endsWith(".txt") && + !m.name.endsWith(".js") + ) + .map((m) => { + const prefixedPath = path.join("python_modules", m.name); + return { + ...m, + name: prefixedPath, + }; + }); modules.push(...vendoredModules); } else {