2.2.0 • Published 11 days ago

@naxodev/nx-cloudflare v2.2.0

Weekly downloads
-
License
MIT
Repository
github
Last release
11 days ago

MIT commitizen PRs styled with prettier

Nx plugin for Cloudflare.

Features

  • ✅ Generate Cloudflare Worker Application
    • ✅ Include Fetch Handler template
    • ✅ Include Scheduled Handler template
    • ✅ Vitest tests support
    • ✅ Serve and Deploy executors for workers.
  • ✅ Generate Cloudflare Worker Library
  • Experimental Cloudflare Pages builder for Next.JS projects.

Installation

Nx Cloudflare is published as the @naxodev/nx-cloudflare package.

ToolchainCommand
NPM CLInpm install @naxodev/nx-cloudflare
PNPM CLIpnpm add @naxodev/nx-cloudflare
Yarn CLIyarn add @naxodev/nx-cloudflare

Compatibility

Nx Cloudflare is compatible with the following versions of Nx:

Nx VersionNx Cloudflare Version
17.x1.x
18.x2.x

Usage

Cloudflare Worker Application

Generating a new Cloudflare Worker Application

nx g @naxodev/nx-cloudflare:application my-worker-app

Available options:

OptionTypeDefaultDescription
namestring*requiredWhat name would you like to use?
templatefetch-handler, scheduled-handler, hono, nonefetch-handlerWhich worker template do you want to use?
projectNameAndRootFormatas-provided, derivedas-providedWhether to generate the project name and root directory as provided (as-provided) or generate them composing their values and taking the configured layout into account (derived).
portnumber8787The port in which the worker will be run on development mode
accountIdstringnullThe Cloudflare account identifier where the worker will be deployed
jsbooleanfalseUse JavaScript instead of TypeScript
tagsstringnullAdd tags to the application (used for linting).
frontendProjectstringnullFrontend project that needs to access this application. This sets up proxy configuration.
unitTestRunnervitest, nonevitestTest runner to use for unit tests.
directorystringnullThe directory of the new application.
rootProjectbooleanfalseCreate worker application at the root of the workspace
skipFormatbooleanfalseSkip formatting files.

Serve worker on dev mode

nx serve <my-app>

The available options are the following:

OptionTypeDefaultDescription
scriptstring-The path to an entry point for your Worker.
namestring-Name of the Worker.
noBundlebooleanfalseSkip Wrangler’s build steps and show a preview of the script without modification. Particularly useful when using custom builds.
envstring-Perform on a specific environment.
compatibilityDatestring-A date in the form yyyy-mm-dd, which will be used to determine which version of the Workers runtime is used.
compatibilityFlags, compatibilityFlagstring[]-Flags to use for compatibility checks.
latestbooleantrueUse the latest version of the Workers runtime.
ipstring-IP address to listen on, defaults to "localhost."
portnumber-Port to listen on.
inspectorPortnumber-Port for devtools to connect to.
routes, routestring[]-Routes to upload. For example: --route example.com/*.
hoststring-Host to forward requests to, defaults to the zone of the project.
localProtocol"http" or "https""http"Protocol to listen to requests on.
localUpstreamstring-Host to act as origin in local mode, defaults to dev.host or route.
assetsstring-Root folder of static assets to be served. Unlike --site, --assets does not require a Worker script to serve your assets. Use in combination with --name and --latest for basic static file hosting. For example: wrangler dev --name personal_blog --assets dist/ --latest.
sitestring-Root folder of static assets for Workers Sites.
siteIncludestring[]-Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.
siteExcludestring[]-Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.
upstreamProtocol"http" or "https""https"Protocol to forward requests to host on.
varkey:value[]-Array of key:value pairs to inject as variables into your code. The value will always be passed as a string to your Worker. For example, --var git_hash:$(git rev-parse HEAD) test:123 makes the git_hash and test variables available in your Worker’s env. This flag is an alternative to defining vars in your wrangler.toml. If defined in both places, this flag’s values will be used.
definekey:value[]-Array of key:value pairs to replace global identifiers in your code. For example, --define GIT_HASH:$(git rev-parse HEAD) will replace all uses of GIT_HASH with the actual value at build time. This flag is an alternative to defining define in your wrangler.toml. If defined in both places, this flag’s values will be used.
tsconfigstring-Path to a custom tsconfig.json file.
minifyboolean-Minify the script.
nodeCompatboolean-Enable node.js compatibility.
persistTostring-Specify directory to use for local persistence.
remotebooleanfalseDevelop against remote resources and data stored on Cloudflare’s network.
testScheduledbooleanfalseExposes a /scheduled fetch route which will trigger a scheduled event (cron trigger) for testing during development. To simulate different cron patterns, a cron query parameter can be passed in: /scheduled?cron=*+*+*+*+*.
logLevel"debug," "info," "log," "warn," "error," "none""log"Specify Wrangler’s logging level.

Deploy worker to Cloudflare

nx deploy <my-app>

The available options are the following:

OptionTypeDefaultDescription
namestring-Name of the Worker.
noBundlebooleanfalseSkip Wrangler’s build steps and directly deploy script without modification. Particularly useful when using custom builds.
envstring-Perform on a specific environment.
outdirstring-Path to directory where Wrangler will write the bundled Worker files.
compatibilityDatestring-A date in the form yyyy-mm-dd, which will be used to determine which version of the Workers runtime is used.
compatibilityFlagsstring[]-Flags to use for compatibility checks.
latestbooleantrueUse the latest version of the Workers runtime.
assetsstring-Root folder of static assets to be served. Unlike --site, --assets does not require a Worker script to serve your assets.
sitestring-Root folder of static assets for Workers Sites.
siteIncludestring[]-Array of .gitignore-style patterns that match file or directory names from the sites directory. Only matched items will be uploaded.
siteExcludestring[]-Array of .gitignore-style patterns that match file or directory names from the sites directory. Matched items will not be uploaded.
varstring[]-Array of key:value pairs to inject as variables into your code. The value will always be passed as a string to your Worker.
definestring[]-Array of key:value pairs to replace global identifiers in your code.
triggersstring[]-Cron schedules to attach to the deployed Worker. Refer to Cron Trigger Examples.
routesstring[]-Routes where this Worker will be deployed.
tsconfigstring-Path to a custom tsconfig.json file.
minifyboolean-Minify the bundled script before deploying.
nodeCompatboolean-Enable node.js compatibility.
dryRunbooleanfalseCompile a project without actually deploying to live servers. Combined with --outdir, this is also useful for testing the output of npx wrangler deploy. It also gives developers a chance to upload our generated sourcemap to a service like Sentry, so that errors from the Worker can be mapped against source code, but before the service goes live.
keepVarsbooleanfalseIt is recommended best practice to treat your Wrangler developer environment as a source of truth for your Worker configuration, and avoid making changes via the Cloudflare dashboard. If you change your environment variables or bindings in the Cloudflare dashboard, Wrangler will override them the next time you deploy. If you want to disable this behavior set keepVars to true.

Cloudflare Worker Library

Generating a new Cloudflare Worker Library

nx g @naxodev/nx-cloudflare:library my-worker-lib

Available options:

OptionTypeDefaultDescription
namestring*requiredWhat name would you like to use?
directorystringnullThe directory of the new application.
projectNameAndRootFormatas-provided, derivedas-providedWhether to generate the project name and root directory as provided (as-provided) or generate them composing their values and taking the configured layout into account (derived).
lintereslint, noneeslintThe tool to use for running lint checks.
unitTestRunnervitest, nonevitestTest runner to use for unit tests.
tagsstringnullAdd tags to the application (used for linting).
skipFormatbooleanfalseSkip formatting files.
jsbooleanfalseUse JavaScript instead of TypeScript
strictbooleantrueWhether to enable tsconfig strict mode or not.
publishablebooleanfalseGenerate a publishable library.
importPathstringnullThe library name used to import it, like @myorg/my-awesome-lib. Required for publishable library.
bundlerswc, tsc, rollup, vite, esbuild, nonetscWhich bundler would you like to use to build the library? Choose 'none' to skip build setup.
minimalbooleanfalseGenerate a library with a minimal setup. No README.md generated.
simpleNamebooleanfalseDon't include the directory in the generated file name.

NextJS on Cloudflare

Build Next application.

This feature is ⚠️ experimental. We appreciate that you let us know of any issues that you find.

To start using the Cloudflare Pages Next builder, start by replacing the builder executor in your application project.json.

  ...
  "targets": {
    ...
    "build": {
      "executor": "@naxodev/nx-cloudflare:next-build",
      ...
    },
    ...

Notice that you can use the same configurations as in the @nx/next:build executor.

Now you can run the build command.

nx build <app-name>

And upload the content of the folder .vercel/output/static contained in the folder matching your configured output path.

Acknowledgement

This project is heavily inspired in the work done by other Nx Champions, check out their projects.

Contributors

Thanks goes to these wonderful people (emoji key):

  <td align="center" valign="top" width="14.28%
2.2.0

11 days ago

2.1.0

19 days ago

2.0.1

2 months ago

2.0.0

2 months ago

1.0.2

5 months ago

1.0.1

5 months ago

1.0.1-preview

5 months ago

1.0.0

6 months ago

0.8.1

6 months ago

0.8.0

7 months ago

0.7.1

7 months ago

0.7.0

8 months ago

0.6.0

8 months ago

0.5.0

8 months ago

0.4.0

8 months ago

0.3.0

9 months ago

0.2.0

9 months ago

0.1.0

9 months ago

0.0.1-alpha-4

9 months ago

0.0.1-alpha-3

9 months ago

0.0.1-alpha-2

9 months ago

0.0.4-alpha

9 months ago

0.0.3-alpha

9 months ago

0.0.2-alpha

9 months ago

0.0.1-alpha

9 months ago