部署 Web 应用
Deploying a Dart web app is similar to deploying any other web app:you first need to compile the app to JavaScript.This page describes how to compile your app—withtips for making it smaller and faster—andpoints you to resources for serving the app.
Building your app
Use the webdev tool to build your app,compiling it to JavaScript and generating all the assetsyou need for deployment.When you build using dart2js,you get a JavaScript file that’s reasonably small,thanks to the dart2js compiler’s support for tree shaking.
With a little extra work, you can make your deployable appsmaller, faster, and more reliable.
Compile using webdev
Use the webdev build command to createa deployable version of your app.Here’s what happens when you use webdev with dart2jsand the —output build
option:
- Deployable files appear under your app’s
build/web
directory. - dart2js compiles your app to JavaScript, saving the resultin the file
build/web/main.dart.js
.
For more information, see the documentation for webdev.
Use dart2js flags to produce better JavaScript
Google’s apps often use the following dart2js options:
—fast-startup
—minify
—trust-primitives
—trust-type-annotations
Test your apps before deploying with these options!
- Build your app both with and without
—fast-startup
,so you can judge whether the speedup is worth the increase in JavaScript size. - The
—trust-primitives
option can have unexpected results(even in well-typed code) if your data isn’t always valid.
For more information, see the documentation for dart2js.
Important: Make sure your app has good test coverage before you use either of the —trust-*
options. If some code paths aren’t tested, your app might run in dartdevc but misbehave when compiled using dart2js.
You can specify dart2js options in a build config file,as described in the build_web_compilers README.
Make your app smaller, faster, and more reliable
The following steps are optional,but they can help make your app more reliable and responsive.
- Use the pwa package to make your app work offline
- Use deferred loading to reduce your app’s initial size
- Follow best practices for web apps
- Remove unneeded build files
Use the pwa package to make your app work offline
The pwa package simplifies the task ofmaking your app work with limited or no connectivity.For information on using this package, seeMaking a Dart web app offline-capable: 3 lines of code.
Use deferred loading to reduce your app’s initial size
You can use Dart’s support for deferred loading toreduce your app’s initial download size.For details, see the language tour’s coverage ofdeferred loadingand the dart-lang/angular pageImperative Component Loading.
Follow best practices for web apps
The usual advice for web apps applies to Dart web apps.Here are a few resources:
Remove unneeded build files
Web compilers can produce files that are useful during development,such as Dart-to-JavaScript map files,but unnecessary in production.
To remove these files, you can run a command like the following:
# From the root directory of your app:
find build -type f -name "*.js.map" -exec rm {} +
Serving your app
You can serve your Dart Web app just like you’d serve any other web app.This section points to tips for serving Dart Web apps,as well as Dart-specific resources to help you use GitHub Pages or Firebaseto serve your app.
GitHub Pages
If your app doesn’t use routing or require server-side support,you can serve the app using GitHub Pages.The peanut package isan easy way to automatically produce a gh-pages branch for any Dart web app.
The startup_namer exampleis hosted using GitHub Pages.Its files are in the gh-pages branch of thefiliph/startup_namer repoand were built using peanut.
Firebase
For a walk-through of using Firebase to serve a chat app, seeBuild a Real-Time Chat Web App with Dart, Angular 2, and Firebase 3.
Other resources:
- The Google I/O 2017 codelabBuild an AngularDart & Firebase Web Appwalks through using Firebase for server-side communication,but doesn’t include instructions for serving the app.
- The Firebase Hosting documentationdescribes how to deploy web apps with Firebase.
- In the Firebase Hosting documentation,Customize Hosting Behaviorcovers redirects, rewrites, and more.