Skip to content
AnswerWell geo Documentation

Research · 8 min read · Answerwell

Worker Bundling, Module Rules, and D1 Migration Patterns

How Wrangler bundles Workers, treats non-JS imports and rules, and applies D1 migrations including nested SQL layouts.

This page summarizes Cloudflare documentation on Wrangler bundling for Workers: how code is packaged with esbuild, how module rules handle .sql, Wasm, and text files, and how that connects to D1 migrations—including nested folders from tools like Drizzle and applying migrations in local dev and tests.

Wrangler bundling: how deploy packages your Worker

Section titled “Wrangler bundling: how deploy packages your Worker”

Wrangler bundling turns many source files and npm dependencies into what gets uploaded. By default, Wrangler bundles your Worker code using esbuild, so you can import modules from npm defined in package.json without a separate webpack step.

By default, Wrangler bundles your Worker code using esbuild. This means that Wrangler has built-in support for importing modules from npm defined in your package.json.

— Cloudflare, Bundling

In bundling mode, Wrangler traverses imports and generates a single JavaScript entry-point file; imported source is inlined into that file. To see the bundle before upload, run:

npx wrangler deploy --dry-run --outdir dist

Migrate from webpack: remove type and webpack_config

Section titled “Migrate from webpack: remove type and webpack_config”

If you still use legacy type = webpack and webpack_config for JSX, TypeScript, or Wasm, Wrangler v2 and later handle that with built-in bundling instead.

As of Wrangler v2, Wrangler has built-in support for this use case. Refer to Bundling for more details. The Workers runtime handles JSX and TypeScript. You should remove the type and webpack_config keys from your Wrangler file.

— Cloudflare, Migrate webpack projects

–no-bundle and Custom Builds: when not to let Wrangler bundle

Section titled “–no-bundle and Custom Builds: when not to let Wrangler bundle”

Disabling bundling is for code already built by another tool. Use npx wrangler deploy --no-bundle only when artifacts are ready to deploy; you lose Wrangler-provided minification and polyfills. For more control while still using Wrangler, use Custom Builds or configuration options below instead of turning bundling off entirely.

Disabling bundling is not recommended in most scenarios. Use this option only when deploying code pre-processed by other tooling.

— Cloudflare, Bundling

Module rules: import .sql, Wasm, text, and binary in Worker code

Section titled “Module rules: import .sql, Wasm, text, and binary in Worker code”

Bundling merges JavaScript, but some files must ship as separate modules uploaded beside the entry point—for example Wasm instantiated at runtime, or SQL read as a string inside Durable Objects.

Wrangler (and the Vite plugin) apply these defaults:

Module extension Imported type
.txt string
.html string
.sql string
.bin ArrayBuffer
.wasm, .wasm?module WebAssembly.Module

The .sql file extension is now automatically configured to be importable in your Worker code when using Wrangler or the Cloudflare Vite plugin. SQL files are imported as JavaScript strings.

— Cloudflare, Import SQL files as additional modules by default

Example:

import example from "./example.sql";
// example is a JavaScript string

For Wasm, import the module and instantiate in module scope; Workers do not support WebAssembly.instantiateStreaming().

Cloudflare Workers does not support WebAssembly.instantiateStreaming().

— Cloudflare, Bundling — Including non-JavaScript modules

rules in wrangler.jsonc: customize module types

Section titled “rules in wrangler.jsonc: customize module types”

Configure extra patterns with the top-level rules array (this replaces legacy build.upload.rules). Each rule needs type (ESModule, CommonJS, CompiledWasm, Text, or Data) and globs; optional fallthrough: true allows multiple rules for the same type.

See Wrangler configuration — Bundling for JSON and TOML examples, and Deprecations for main and rules as the current fields.

Wrangler supports import attributes for typed imports, for example:

import data from "./data.json" with { type: "json" };

Import attributes allow you to denote the type or other attributes of the module that your code imports.

— Cloudflare, Import attributes

find_additional_modules: lazy loads and dynamic import paths

Section titled “find_additional_modules: lazy loads and dynamic import paths”

By default, only statically imported additional modules are included. Setting find_additional_modules to true makes Wrangler walk the tree under base_dir (default: directory of the main entry) and upload matching files as unbundled external modules—useful for large await import("./large-dep.mjs") and for variable dynamic imports that Wrangler cannot infer at build time.

If matching rule is added to rules, then this file would only be loaded and executed at runtime when it is actually imported. Providing a rule that matches all these files, such as { "type": "EsModule", "globs": ["./lang/**/*.mjs"], "fallthrough": true }, will ensure this module is available at runtime.

— Cloudflare, Bundling — Find additional modules

Partial bundling applies when find_additional_modules is true and a file matches a rule: Wrangler treats it as external and does not inline it into the entry point. Details also appear in Wrangler configuration.

When bundling npm packages, Wrangler respects package.json conditional exports and tries the workerd condition for isomorphic libraries. See Bundling — Conditional exports.

alias: fix bundling resolution without forking the package

Section titled “alias: fix bundling resolution without forking the package”

If bundling fails to resolve a dependency, confirm it is installed, then consider an alias in Wrangler config: a Worker-compatible replacement, a no-op file, or a module that throws if the dependency must never run. See Wrangler configuration — Bundling issues.

D1 migrations: folders, migrations_pattern, and Wrangler commands

Section titled “D1 migrations: folders, migrations_pattern, and Wrangler commands”

D1 migrations version your database as .sql files. Wrangler tracks applied migrations in the d1_migrations table (name overridable via migrations_table on the binding).

Database migrations are a way of versioning your database. Each migration is stored as an .sql file in your migrations folder.

— Cloudflare, Migrations

Create and apply migrations with wrangler d1

Section titled “Create and apply migrations with wrangler d1”

Create a new versioned file under the migrations folder:

npx wrangler d1 migrations create <DATABASE> <MESSAGE>

Filenames look like 0000_create_user_table.sql. When applying, you can pass the binding name or database name; Cloudflare notes the database name is stable while the binding name can change—using the database name avoids applying to the wrong binding. Command reference: d1 migrations create and D1 Wrangler commands.

Binding-level options in Wrangler include migrations_dir, migrations_table, and migrations_pattern. See Migrations — Wrangler customizations.

Nested migration layouts (Drizzle migrations_pattern)

Section titled “Nested migration layouts (Drizzle migrations_pattern)”

By default, wrangler d1 migrations apply expects top-level .sql files in migrations_dir. ORMs such as Drizzle often use migrations/0001_init/migration.sql. Set migrations_pattern to a glob relative to the Wrangler config file, starting with migrations_dir:

{
"d1_databases": [
{
"binding": "DB",
"database_name": "my-database",
"database_id": "<UUID>",
"migrations_dir": "migrations",
"migrations_pattern": "migrations/*/migration.sql"
}
]
}

migrations_pattern is a glob (relative to your Wrangler config file) used to discover migration files. It defaults to ${migrations_dir}/*.sql, so existing projects keep working unchanged. Each migration’s name is recorded in the migrations table as a path relative to migrations_dir.

— Cloudflare, D1 migrations support nested layouts via migrations pattern

Rules from Migrations — Nested migration layouts:

  • If migrations_pattern is set, migrations_dir must also be set.
  • The pattern must start with whatever migrations_dir is set to.
  • * matches one path segment; ** matches any depth (e.g. migrations/**/*.sql).
  • wrangler d1 migrations create only writes top-level files in migrations_dir; for Drizzle-style nested layouts, generate SQL with your ORM (e.g. drizzle-kit) instead.

Importing .sql in Worker code for Durable Object migrations no longer needs custom rules when using Wrangler or the Vite plugin (changelog). Drizzle ORM is a common choice in the D1 community projects list.

Local dev and CI: apply D1 migrations with –local

Section titled “Local dev and CI: apply D1 migrations with –local”

For local databases, set preview_database_id on the D1 binding and run:

wrangler d1 migrations apply your-database --local

By default in Wrangler v3+, wrangler dev persists D1 data across runs; use DROP TABLE in migrations or a fresh persist path if you need an empty database. See Local development — Persist data.

If you use cf migrate instead of Wrangler for apply, pass migrations_dir as --dir, migrations_pattern as --pattern, and migrations_table as --table; cf d1 migrations apply targets the remote database unless you add --local. See Migrate a Wrangler project — D1 migrations.

Testing: apply the same migration layout in Vitest and the test harness

Section titled “Testing: apply the same migration layout in Vitest and the test harness”

Tests should use the same migrations_dir and migrations_pattern as production Wrangler config.

Vitest: readD1Migrations(migrationsPath) from @cloudflare/vitest-plugin/config reads migrations, splits SQL into statements, and pairs with applyD1Migrations(). See Vitest integration configuration and Test APIs — D1.

Test harness: worker.applyD1Migrations(bindingName) reads migration settings from Wrangler configuration, including migrations_pattern; without those options it reads .sql files from the migrations directory relative to the config file. Call it after resetting storage, then seed data. See Prepare test state — Apply D1 migrations.

Pages Functions support similar module types as Workers (Wasm, text, binary) for edge functions that share assets with static Pages.

  • Leaving type = webpack after upgrading Wrangler instead of removing it per eject-webpack.
  • Using wrangler d1 migrations create while migrations_pattern only matches nested paths—generate nested SQL with your ORM instead (nested layouts).
  • Expecting variable dynamic imports without find_additional_modules and matching rules (Find additional modules).
  • Assuming an empty local D1 on every wrangler dev run when persistence is on (local development).
  • Using WebAssembly.instantiateStreaming() in Workers (Wasm bundling).
  1. Run npx wrangler deploy --dry-run --outdir dist and inspect the bundled entry and additional modules against Bundling.

  2. Confirm rules, main, and optional find_additional_modules in Wrangler configuration match how you import .sql, Wasm, and lazy-loaded chunks.

  3. List pending D1 migrations with Wrangler D1 commands (Wrangler commands), then apply locally with --local if you use preview_database_id (local development).

  4. Re-run Vitest or your test harness after changing migrations_pattern so readD1Migrations or applyD1Migrations still matches your on-disk layout (Vitest configuration, test harness).

  5. You can check automatically in Answerwell (our tool): https://answerwell.org/workspace

If something on this page is wrong or unclear, email yimasynthetic@gmail.com with the page URL and the exact statement you are asking about so it can be corrected.