static-web-apps-cli

Azure Static Web Apps CLI ✨
授权协议 MIT License
开发语言 JavaScript
所属分类 应用工具、 终端/远程登录
软件类型 开源软件
地区 不详
投 递 者 濮阳烨然
操作系统 跨平台
开源组织
适用人群 未知
 软件概览

Azure Static Web Apps CLI (preview)

The Static Web Apps CLI, also known as SWA CLI, serves as a local development tool for Azure Static Web Apps. It can:

  • Serve static app assets, or proxy to your app dev server
  • Serve API requests, or proxy to APIs running in Azure Functions Core Tools
  • Emulate authentication and authorization
  • Emulate Static Web Apps configuration, including routing

Static Web Apps CLI is in preview. If you have suggestions or you encounter issues, please report them or help us fix them. Your contributions are very much appreciated. ��

The CLI emulates commonly used capabilities of the Azure Static Web Apps cloud service. Some differences are expected. Always deploy and test your apps in Azure to confirm behavior.

Quickstart

Using npm or yarn:

  • Install the cli
    npm install -g @azure/static-web-apps-cli
  • Open a SWA app folder at the root (outside any /api or /app folders):
    cd my-awesome-swa-app
  • Start the emulator:
    swa start
  • Access your SWA app from http://localhost:4280

Note: The cli can also be installed locally as a devDependency: npm install -D @azure/static-web-apps-cli

Using npx:

  • Open a SWA app folder at the root (outside any /api or /app folders): cd my-awesome-swa-app
  • Start the emulator: npx @azure/static-web-apps-cli start
  • Access your SWA app from http://localhost:4280

Start the emulator

Serve from a folder

By default, CLI starts and serves any the static content from the current working directory ./:

swa start

However, you can override this behavior. If the artifact folder of your static app is under a different folder (e.g. ./my-dist), then run the CLI and provide that folder:

swa start ./my-dist

Serve from a dev server

When developing your frontend app locally, it's often useful to use the dev server that comes with your frontend framework's CLI to serve your app content. Using the framework CLI allows you to use built-in features like the livereload and HMR (hot module replacement).

To use SWA CLI with your local dev server, follow these two steps:

  1. Start your local dev server (as usual). For example, if you are using Angular: ng serve
  2. In a separate terminal, run swa start with the URI provided by the dev server, in the following format:
    swa start http://<APP_DEV_SERVER_HOST>:<APP_DEV_SERVER_PORT>

Here is a list of the default ports used by some popular dev servers:

Tool Port Command
Angular 4200 swa start http://localhost:4200
Vue 8080 swa start http://localhost:8080
React (Create React App) 3000 swa start http://localhost:3000
Blazor WebAssembly 5000 swa start http://localhost:5000
Hugo 1313 swa start http://localhost:1313
Svelte (sirv-cli) 5000 swa start http://localhost:5000
Gatsby 8000 swa start http://localhost:8000
Next.js 3000 swa start http://localhost:3000

Instead of starting a dev server separately, you can provide the startup command to the CLI.

# npm start script (React)
swa start http://localhost:3000 --run "npm start"

# dotnet watch (Blazor)
swa start http://localhost:5000 --run "dotnet watch run"

# Jekyll
swa start http://localhost:4000 --run "jekyll serve"

# custom script
swa start http://localhost:4200 --run "./startup.sh"

Go to 4280 (http://localhost:4280) to access the application with the emulated services.

Serve both the static app and API

If your project includes API functions, install Azure Functions Core Tools:

npm install -g azure-functions-core-tools@3 --unsafe-perm true

Start API server automatically

Run the CLI and provide the folder that contains the API backend (a valid Azure Functions App project):

# static content plus API
swa start ./my-dist --api-location ./api-folder

# frontend dev server plus API
swa start http://localhost:3000 --api-location ./api-folder

Start API server manually

When developing your backend locally, sometimes it's useful to run Azure Functions Core Tools separately to serve your API. This allows you to use built-in features like debugging and rich editor support.

To use the CLI with your local API backend dev server, follow these two steps:

  1. Start your API using Azure Functions Core Tools: func host start or start debugging in VS Code.
  2. In a separate terminal, run the SWA CLI with the --api-location flag and the URI of the local API server, in the following format:
    swa start ./my-dist --api-location http://localhost:7071

Use a configuration file (staticwebapp.config.json)

Azure Static Web Apps can be configured with an optional staticwebapp.config.json file. For more information, see Configure Static Web Apps documentation.

If you are serving static files from a folder, the CLI will search this folder for staticwebapp.config.json.

# ./my-dist is searched for staticwebapp.config.json
swa start ./my-dist

If you are using a frontend dev server, the CLI will search the current directory for staticwebapp.config.json.

# current working directory is searched for staticwebapp.config.json
swa start http://localhost:3000

To control where the CLI searches for staticwebapp.config.json, use --swa-config-location.

# static files
swa start ./my-dist --swa-config-location ./my-app-source

# frontend dev server
swa start http://localhost:3000 --swa-config-location ./my-app-source

Configuration

If you need to override the default values, provide the following options:

Options Description Default Example
--app-location set location for the static app source code ./ --app-location="./my-project"
--output-location set app artifact (dist) folder or dev server ./ --output-location="./my-dist" or --output-location=http://localhost:4200
--api-location set the API folder or dev server --api-location="./api" or --api-location=http://localhost:8083
--swa-config-location set the directory of the staticwebapp.config.json file. --swa-config-location=./my-project-folder
--api-port set the API server port 7071 --api-port=8082
--host set the emulator host address 0.0.0.0 --host=192.168.68.80
--port set the emulator port value 4280 --port=8080
--ssl serving the app and API over HTTPS (default: false) false --ssl or --ssl=true
--ssl-cert SSL certificate to use for serving HTTPS --ssl-cert="/home/user/ssl/example.crt"
--ssl-key SSL key to use for serving HTTPS --ssl-key="/home/user/ssl/example.key"
--run Run a command at startup --run="cd app & npm start"
--devserver-timeout The time to wait(in ms) for the dev server to start 30000 --devserver-timeout=60000
--func-args Additional arguments to pass to func start --func-args="--javascript"
--config Path to swa-cli.config.json file to use. ./swa-cli.config.json --config ./config/swa-cli.config.json
--print-config Print all resolved options. Useful for debugging. --print-config or --print-config=true

swa-cli.config.json file

The CLI can also load options from a swa-cli.config.json file.

{
  "configurations": {
    "app": {
      "context": "http://localhost:3000",
      "apiLocation": "api",
      "run": "npm run start",
      "swaConfigLocation": "./my-app-source"
    }
  }
}

If only a single configuration is present in the swa-cli.config.json file, running swa start will use it by default. If options are loaded from a config file, no options passed in via command line will be respected. For example swa start app --ssl=true. The --ssl=true option will not be picked up by the CLI.

Example

We can simplify these commands by putting the options into a config file.

# static configuration
swa start ./my-dist --swa-config-location ./my-app-source

# devserver configuration
swa start http://localhost:3000 --swa-config-location ./my-app-source
{
  "configurations": {
    "static": {
      "context": "./my-dist",
      "swaConfigLocation": "./my-app-source"
    },
    "devserver": {
      "context": "http://localhost:3000",
      "swaConfigLocation": "./my-app-source"
    }
  }
}

These configurations can be run with swa start static and swa start devserver.

Validation

You can validate your swa-cli.config.json with a JSON Schema like so:

{
  "$schema": "https://raw.githubusercontent.com/Azure/static-web-apps-cli/main/schema/swa-cli.config.schema.json",
  "configurations": {
    ...
  }
}

Local authentication & authorization emulation

The CLI allows you to mock and read authentication and authorization credentials.

Mocking credentials

When requesting the Static Web Apps login endpoints (http://localhost:4280/.auth/login/<PROVIDER_NAME>), you have access to a local authentication UI. This interface is served locally from the emulator and allows you to set fake user information for the current user from the provider supplied.

Reading credentials

The frontend application can request the http://localhost:4280/.auth/me endpoint and a clientPrincipal containing the fake information will be returned by the authentication API.

Here is an example:

{
  "clientPrincipal": {
    "identityProvider": "twitter",
    "userId": "<USER-UUID>",
    "userDetails": "<USER_NAME>",
    "userRoles": ["anonymous", "authenticated"]
  }
}

The API functions can access user information using the x-ms-client-principal header.

See Accessing user information documentation for more details.

High-level architecture

The SWA CLI is built on top of the following components:

  • A Reverse Proxy is the heart of the SWA CLI; it's the piece that forwards all HTTP requests to the appropriate components:
    • /.auth/** requests are forwarded to the Auth emulator server.
    • /api/** requests are forwarded to the localhost API function (if available).
    • /** all other requests are forwarded to the static assets server (serving the front-end app).
  • The Auth emulator server emulates the whole authentication flow.
  • The Static content server serves the local app static content.
  • The Serverless API server is served by Azure Functions Core Tools.

Want to help?

Want to file a bug, contribute some code, or improve the documentation? Excellent! Read up on our guidelines for contributing and then check out one of our issues in the list: community-help.

  • 前言 vue-cli和webpack结合的脚手架挺好用的,但是初次使用对于其中的配置和npm包的引用总是会一脸懵逼,这篇文章是对其中一些相关模块的简单分析。 主要目的是加深我自己对webpack和node.js的认知。 正文 1.项目结构 vue-cli的配置文件主要在build和config文件夹中,其中config文件夹主要是放一些环境变量,webpack的路径等等一些参数。 | -- bui

  • 我在撰写《Vue2实践揭秘》时采用的 vue-cli 版本是 v2.5.1,由于实体书的出版周期比电子书的要长,所以到全书出版vue-cli已经更新到 v2.8.2 了,我在书中曾经对 vue-cli 的全部官方模板进行过一个全面的解释,今天看了到这个更新版本算一算日子也已经与我写书时相距接近半年了,所以也特意重新来对每个模板过了一遍,作为对《Vue2实践揭秘》的补充更新吧。 先来看看有啥不同:

 相关资料
  • 调试 Progressive Web Apps 使用Application面板检查、修改和调试Web应用程序的manifests,service workers 和 service worker 缓存。 相关指南: Progressive Web Apps 本指南仅讨论Application面板的功能。如果你正在寻找其他面板的帮助文档,请查阅本指南中最后一部分,其他 Application面板指南

  • A web honeypot that created by ILX RPC with static files on BIG-IP HW/VE platform. The web honeypot can accept any sub path, the welcome page is trick submit page. Set up Assume you have Telemetry Str

  • Data-driven web apps with Course demo code and other hand-out materials for our data driven web apps in Flask course. Visit the full course page at training.talkpython.fm Course Summary One of the are

  • 静态资源缓存与更新 这是一个非常有趣的 非主流前端领域,这个领域要探索的是如何用工程手段解决前端开发和部署优化的综合问题,入行到现在一直在学习和实践中。 在我的印象中,facebook是这个领域的鼻祖,有兴趣、有梯子的同学可以去看看facebook的页面源代码,体会一下什么叫工程化。 接下来,我想从原理展开讲述,多图,较长,希望能有耐心看完。 让我们返璞归真,从原始的前端开发讲起。上图是一个“可爱

  • Given that my iPhone seems to be able to connect to Google Apps mail (ie: gmail, but with my own domain name) and use the calendar and contacts, I presumed that I'd be able to connect programmatically

  • 这里推荐的 apps 在开发者圈子内普遍评价不错,能便利的处理日常的开发和使用的任务。以下推荐分为四类: 开发者工具 生产力工具 办公工具 其他 Developer Tools Google Chrome Webstorm Sketch Dash Sequel Pro Parallels Github Productivity 1Password : 跨平台的密码管理工具 Alfred 2 : 搜